vdr 2.8.2
thread.h
Go to the documentation of this file.
1/*
2 * thread.h: A simple thread base class
3 *
4 * See the main source file 'vdr.c' for copyright information and
5 * how to reach the author.
6 *
7 * $Id: thread.h 5.1 2026/05/23 21:03:38 kls Exp $
8 */
9
10#ifndef __THREAD_H
11#define __THREAD_H
12
13#include <pthread.h>
14#include <stdio.h>
15#include <sys/types.h>
16
17typedef pid_t tThreadId;
18
19class cCondWait {
20private:
21 pthread_mutex_t mutex;
22 pthread_cond_t cond;
24public:
25 cCondWait(void);
26 ~cCondWait();
27 static void SleepMs(int TimeoutMs);
33 bool Wait(int TimeoutMs = 0);
38 void Signal(void);
40 };
41
42class cMutex;
43
44class cCondVar {
45private:
46 pthread_cond_t cond;
47public:
48 cCondVar(void);
49 ~cCondVar();
50 void Wait(cMutex &Mutex);
51 bool TimedWait(cMutex &Mutex, int TimeoutMs);
52 void Broadcast(void);
53 };
54
55class cRwLock {
56private:
57 pthread_rwlock_t rwlock;
58 int locked;
60public:
61 cRwLock(bool PreferWriter = false);
62 ~cRwLock();
63 bool Lock(bool Write, int TimeoutMs = 0);
64 void Unlock(void);
65 };
66
67class cMutex {
68 friend class cCondVar;
69private:
70 pthread_mutex_t mutex;
71 tThreadId lockThreadId; // current holder, 0 == unlocked; lets Lock() short-circuit recursive entry
72 int locked;
73public:
74 cMutex(void);
75 ~cMutex();
76 void Lock(void);
77 void Unlock(void);
78 };
79
80class cThread {
81 friend class cThreadLock;
82private:
83 bool active;
84 bool running;
85 pthread_t childTid;
91 static void *StartThread(cThread *Thread);
92protected:
93 void SetPriority(int Priority);
94 void SetIOPriority(int Priority);
95 void Lock(void) { mutex.Lock(); }
96 void Unlock(void) { mutex.Unlock(); }
97 virtual void Action(void) = 0;
102 bool Running(void) { return running; }
105 void Cancel(int WaitSeconds = 0);
112public:
113 cThread(const char *Description = NULL, bool LowPriority = false);
120 virtual ~cThread();
121 void SetDescription(const char *Description, ...) __attribute__ ((format (printf, 2, 3)));
126 bool Start(void);
129 bool Active(void);
131 static tThreadId ThreadId(void);
132 static tThreadId IsMainThread(void) { return ThreadId() == mainThreadId; }
133 static void SetMainThreadId(void);
134 };
135
136// cMutexLock can be used to easily set a lock on mutex and make absolutely
137// sure that it will be unlocked when the block will be left. Several locks can
138// be stacked, so a function that makes many calls to another function which uses
139// cMutexLock may itself use a cMutexLock to make one longer lock instead of many
140// short ones.
141
143private:
145 bool locked;
146public:
147 cMutexLock(cMutex *Mutex = NULL);
148 ~cMutexLock();
149 bool Lock(cMutex *Mutex);
150 };
151
152// cThreadLock can be used to easily set a lock in a thread and make absolutely
153// sure that it will be unlocked when the block will be left. Several locks can
154// be stacked, so a function that makes many calls to another function which uses
155// cThreadLock may itself use a cThreadLock to make one longer lock instead of many
156// short ones.
157
159private:
161 bool locked;
162public:
163 cThreadLock(cThread *Thread = NULL);
164 ~cThreadLock();
165 bool Lock(cThread *Thread);
166 };
167
168#define LOCK_THREAD cThreadLock ThreadLock(this)
169
170class cStateKey;
171
173 friend class cStateKey;
174private:
176 const char *name;
179 int state;
182 void Unlock(cStateKey &StateKey, bool IncState = true);
187public:
188 cStateLock(const char *Name = NULL);
189 bool Lock(cStateKey &StateKey, bool Write = false, int TimeoutMs = 0);
217 void SetSyncStateKey(cStateKey &StateKey);
223 void SetExplicitModify(void);
228 void SetModified(void);
232 };
233
235 friend class cStateLock;
236private:
238 bool write;
239 int state;
241public:
242 cStateKey(bool IgnoreFirst = false);
246 ~cStateKey();
247 void Reset(void);
251 void Remove(bool IncState = true);
256 bool StateChanged(void);
261 bool InLock(void) { return stateLock; }
263 bool TimedOut(void) const { return timedOut; }
266 };
267
269private:
270 static cMutex mutex;
271 static int count;
272 bool active;
273public:
274 cIoThrottle(void);
275 ~cIoThrottle();
276 void Activate(void);
280 void Release(void);
284 bool Active(void) { return active; }
286 static bool Engaged(void);
288 };
289
290// cPipe implements a pipe that closes all unnecessary file descriptors in
291// the child process.
292
293class cPipe {
294private:
295 pid_t pid;
296 FILE *f;
297public:
298 cPipe(void);
299 ~cPipe();
300 operator FILE* () { return f; }
301 bool Open(const char *Command, const char *Mode);
302 int Close(void);
303 };
304
305// cBackTrace can be used for debugging.
306
307class cStringList;
308class cString;
309
311public:
312 static cString Demangle(char *s);
319 static void BackTrace(cStringList &StringList, int Level = 0, bool Mangled = false);
324 static void BackTrace(FILE *f = NULL, int Level = 0, bool Mangled = false);
329 static cString GetCaller(int Level = 0, bool Mangled = false);
334 };
335
336// SystemExec() implements a 'system()' call that closes all unnecessary file
337// descriptors in the child process.
338// With Detached=true, calls command in background and in a separate session,
339// with stdin connected to /dev/null.
340
341int SystemExec(const char *Command, bool Detached = false);
342
343#endif //__THREAD_H
static void BackTrace(cStringList &StringList, int Level=0, bool Mangled=false)
Produces a backtrace and stores it in the given StringList.
Definition thread.c:536
static cString GetCaller(int Level=0, bool Mangled=false)
Returns the caller at the given Level (or the immediate caller, if Level is 0).
Definition thread.c:559
static cString Demangle(char *s)
Demangles the function name in the given string and returns the converted version of s.
Definition thread.c:458
void Wait(cMutex &Mutex)
Definition thread.c:122
cCondVar(void)
Definition thread.c:111
bool TimedWait(cMutex &Mutex, int TimeoutMs)
Definition thread.c:135
void Broadcast(void)
Definition thread.c:155
pthread_cond_t cond
Definition thread.h:46
~cCondVar()
Definition thread.c:116
pthread_cond_t cond
Definition thread.h:22
bool signaled
Definition thread.h:23
cCondWait(void)
Definition thread.c:59
~cCondWait()
Definition thread.c:66
bool Wait(int TimeoutMs=0)
Waits at most TimeoutMs milliseconds for a call to Signal(), or forever if TimeoutMs is 0.
Definition thread.c:79
void Signal(void)
Signals a caller of Wait() that the condition it is waiting for is met.
Definition thread.c:101
static void SleepMs(int TimeoutMs)
Creates a cCondWait object and uses it to sleep for TimeoutMs milliseconds, immediately giving up the...
Definition thread.c:73
pthread_mutex_t mutex
Definition thread.h:21
cIoThrottle(void)
Definition thread.c:912
bool Active(void)
Returns true if this I/O throttling object is currently active.
Definition thread.h:284
static int count
Definition thread.h:271
void Activate(void)
Activates the global I/O throttling mechanism.
Definition thread.c:922
~cIoThrottle()
Definition thread.c:917
void Release(void)
Releases the global I/O throttling mechanism.
Definition thread.c:933
bool active
Definition thread.h:272
static bool Engaged(void)
Returns true if any I/O throttling object is currently active.
Definition thread.c:944
static cMutex mutex
Definition thread.h:270
cMutexLock(cMutex *Mutex=NULL)
Definition thread.c:404
bool Lock(cMutex *Mutex)
Definition thread.c:417
~cMutexLock()
Definition thread.c:411
cMutex * mutex
Definition thread.h:144
bool locked
Definition thread.h:145
void Lock(void)
Definition thread.c:228
tThreadId lockThreadId
Definition thread.h:71
pthread_mutex_t mutex
Definition thread.h:70
cMutex(void)
Definition thread.c:213
~cMutex()
Definition thread.c:223
friend class cCondVar
Definition thread.h:68
int locked
Definition thread.h:72
void Unlock(void)
Definition thread.c:242
pid_t pid
Definition thread.h:295
int Close(void)
Definition thread.c:1019
FILE * f
Definition thread.h:296
bool Open(const char *Command, const char *Mode)
Definition thread.c:965
cPipe(void)
Definition thread.c:954
~cPipe()
Definition thread.c:960
int locked
Definition thread.h:58
tThreadId writeLockThreadId
Definition thread.h:59
pthread_rwlock_t rwlock
Definition thread.h:57
cRwLock(bool PreferWriter=false)
Definition thread.c:162
bool Lock(bool Write, int TimeoutMs=0)
Definition thread.c:177
void Unlock(void)
Definition thread.c:199
~cRwLock()
Definition thread.c:172
cStateLock * stateLock
Definition thread.h:237
cStateKey(bool IgnoreFirst=false)
Sets up a new state key.
Definition thread.c:863
int state
Definition thread.h:239
void Remove(bool IncState=true)
Removes this key from the lock it was previously used with.
Definition thread.c:885
friend class cStateLock
Definition thread.h:235
~cStateKey()
Definition thread.c:872
bool TimedOut(void) const
Returns true if the last lock attempt this key was used with failed due to a timeout.
Definition thread.h:263
void Reset(void)
Resets the state of this key, so that the next call to a lock's Lock() function with this key will re...
Definition thread.c:880
bool timedOut
Definition thread.h:240
bool write
Definition thread.h:238
bool StateChanged(void)
Returns true if this key is used for obtaining a write lock, and the lock's state differs from that o...
Definition thread.c:895
bool InLock(void)
Returns true if this key is currently in a lock.
Definition thread.h:261
tThreadId threadId
Definition thread.h:177
const char * name
Definition thread.h:176
cRwLock rwLock
Definition thread.h:178
int state
Definition thread.h:179
void SetExplicitModify(void)
If you have obtained a write lock on this lock, and you don't want its state to be automatically incr...
Definition thread.c:836
cStateLock(const char *Name=NULL)
Definition thread.c:731
friend class cStateKey
Definition thread.h:173
cStateKey * syncStateKey
Definition thread.h:181
int explicitModify
Definition thread.h:180
void Unlock(cStateKey &StateKey, bool IncState=true)
Releases a lock that has been obtained by a previous call to Lock() with the given StateKey.
Definition thread.c:785
void SetSyncStateKey(cStateKey &StateKey)
Sets the given StateKey to be synchronized to the state of this lock.
Definition thread.c:815
void SetModified(void)
Sets this lock to have its state incremented when the current write lock state key is removed.
Definition thread.c:851
bool Lock(cStateKey &StateKey, bool Write=false, int TimeoutMs=0)
Tries to get a lock and returns true if successful.
Definition thread.c:741
@ emDisabled
Definition thread.h:175
@ emEnabled
Definition thread.h:175
cThreadLock(cThread *Thread=NULL)
Definition thread.c:430
bool Lock(cThread *Thread)
Definition thread.c:443
bool locked
Definition thread.h:161
~cThreadLock()
Definition thread.c:437
cThread * thread
Definition thread.h:160
cMutex mutex
Definition thread.h:87
virtual ~cThread()
Definition thread.c:265
friend class cThreadLock
Definition thread.h:81
void SetIOPriority(int Priority)
Definition thread.c:277
void Unlock(void)
Definition thread.h:96
static void SetMainThreadId(void)
Definition thread.c:394
virtual void Action(void)=0
A derived cThread class must implement the code it wants to execute as a separate thread in this func...
void bool Start(void)
Sets the description of this thread, which will be used when logging starting or stopping of the thre...
Definition thread.c:320
void SetDescription(const char *Description,...) __attribute__((format(printf
Definition thread.c:283
bool Running(void)
Returns false if a derived cThread object shall leave its Action() function.
Definition thread.h:102
void SetPriority(int Priority)
Definition thread.c:271
bool active
Definition thread.h:83
void Lock(void)
Definition thread.h:95
tThreadId childThreadId
Definition thread.h:86
cThread(const char *Description=NULL, bool LowPriority=false)
Creates a new thread.
Definition thread.c:254
bool lowPriority
Definition thread.h:89
bool running
Definition thread.h:84
static void * StartThread(cThread *Thread)
Definition thread.c:295
void Cancel(int WaitSeconds=0)
Cancels the thread by first setting 'running' to false, so that the Action() loop can finish in an or...
Definition thread.c:370
static tThreadId mainThreadId
Definition thread.h:90
static tThreadId IsMainThread(void)
Definition thread.h:132
pthread_t childTid
Definition thread.h:85
bool Active(void)
Checks whether the thread is still alive.
Definition thread.c:345
static tThreadId ThreadId(void)
Definition thread.c:389
char * description
Definition thread.h:88
static cMutex Mutex
Definition epg.c:1439
struct __attribute__((packed))
Definition recording.c:2863
pid_t tThreadId
Definition thread.h:17
int SystemExec(const char *Command, bool Detached=false)
Definition thread.c:1058