vdr 2.8.3
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.2 2026/08/22 11:23:08 kls Exp $
8 */
9
10#ifndef __THREAD_H
11#define __THREAD_H
12
13#include <atomic>
14#include <pthread.h>
15#include <stdio.h>
16#include <sys/types.h>
17
18typedef pid_t tThreadId;
19
20class cCondWait {
21private:
22 pthread_mutex_t mutex;
23 pthread_cond_t cond;
25public:
26 cCondWait(void);
27 ~cCondWait();
28 static void SleepMs(int TimeoutMs);
34 bool Wait(int TimeoutMs = 0);
39 void Signal(void);
41 };
42
43class cMutex;
44
45class cCondVar {
46private:
47 pthread_cond_t cond;
48public:
49 cCondVar(void);
50 ~cCondVar();
51 void Wait(cMutex &Mutex);
52 bool TimedWait(cMutex &Mutex, int TimeoutMs);
53 void Broadcast(void);
54 };
55
56class cRwLock {
57private:
58 pthread_rwlock_t rwlock;
59 int locked;
60 std::atomic<tThreadId> writeLockThreadId; // Lock() reads it without holding the lock
61public:
62 cRwLock(bool PreferWriter = false);
63 ~cRwLock();
64 bool Lock(bool Write, int TimeoutMs = 0);
65 void Unlock(void);
66 };
67
68class cMutex {
69 friend class cCondVar;
70private:
71 pthread_mutex_t mutex;
72 std::atomic<tThreadId> lockThreadId; // current holder, 0 == unlocked; lets Lock() short-circuit recursive entry (read without holding the mutex)
73 int locked;
74public:
75 cMutex(void);
76 ~cMutex();
77 bool Lock(int TimeoutMs = 0, bool * TimedOut = NULL);
83 void Unlock(void);
84 };
85
86class cThread {
87 friend class cThreadLock;
88private:
89 // Shared by the thread and those that start, poll or cancel it; whoever
90 // sees 'active' false must also see everything Action() did:
91 std::atomic<bool> active;
92 std::atomic<bool> running;
93 pthread_t childTid;
94 std::atomic<tThreadId> childThreadId; // set by the thread itself, logged by Cancel()
99 static void *StartThread(cThread *Thread);
100protected:
101 void SetPriority(int Priority);
102 void SetIOPriority(int Priority);
103 void Lock(void) { mutex.Lock(); }
104 void Unlock(void) { mutex.Unlock(); }
105 virtual void Action(void) = 0;
110 bool Running(void) { return running; }
113 void Cancel(int WaitSeconds = 0);
120public:
121 cThread(const char *Description = NULL, bool LowPriority = false);
128 virtual ~cThread();
129 void SetDescription(const char *Description, ...) __attribute__ ((format (printf, 2, 3)));
134 bool Start(void);
137 bool Active(void);
139 static tThreadId ThreadId(void);
140 static tThreadId IsMainThread(void) { return ThreadId() == mainThreadId; }
141 static void SetMainThreadId(void);
142 };
143
144// cMutexLock can be used to easily set a lock on mutex and make absolutely
145// sure that it will be unlocked when the block will be left. Several locks can
146// be stacked, so a function that makes many calls to another function which uses
147// cMutexLock may itself use a cMutexLock to make one longer lock instead of many
148// short ones.
149
151private:
153 bool locked;
154public:
155 cMutexLock(cMutex *Mutex = NULL);
156 ~cMutexLock();
157 bool Lock(cMutex *Mutex, int TimeoutMs = 0, bool *TimedOut = NULL);
163 };
164
165// cThreadLock can be used to easily set a lock in a thread and make absolutely
166// sure that it will be unlocked when the block will be left. Several locks can
167// be stacked, so a function that makes many calls to another function which uses
168// cThreadLock may itself use a cThreadLock to make one longer lock instead of many
169// short ones.
170
172private:
174 bool locked;
175public:
176 cThreadLock(cThread *Thread = NULL);
177 ~cThreadLock();
178 bool Lock(cThread *Thread);
179 };
180
181#define LOCK_THREAD cThreadLock ThreadLock(this)
182
183class cStateKey;
184
186 friend class cStateKey;
187private:
189 const char *name;
192 int state;
195 void Unlock(cStateKey &StateKey, bool IncState = true);
200public:
201 cStateLock(const char *Name = NULL);
202 bool Lock(cStateKey &StateKey, bool Write = false, int TimeoutMs = 0);
230 void SetSyncStateKey(cStateKey &StateKey);
236 void SetExplicitModify(void);
241 void SetModified(void);
245 };
246
248 friend class cStateLock;
249private:
251 bool write;
252 int state;
254public:
255 cStateKey(bool IgnoreFirst = false);
259 ~cStateKey();
260 void Reset(void);
264 void Remove(bool IncState = true);
269 bool StateChanged(void);
274 bool InLock(void) { return stateLock; }
276 bool TimedOut(void) const { return timedOut; }
279 };
280
282private:
283 static cMutex mutex;
284 static std::atomic<int> count; // Engaged() reads it without the mutex
285 bool active;
286public:
287 cIoThrottle(void);
288 ~cIoThrottle();
289 void Activate(void);
293 void Release(void);
297 bool Active(void) { return active; }
299 static bool Engaged(void);
301 };
302
303// cPipe implements a pipe that closes all unnecessary file descriptors in
304// the child process.
305
306class cPipe {
307private:
308 pid_t pid;
309 FILE *f;
310public:
311 cPipe(void);
312 ~cPipe();
313 operator FILE* () { return f; }
314 bool Open(const char *Command, const char *Mode);
315 int Close(void);
316 };
317
318// cBackTrace can be used for debugging.
319
320class cStringList;
321class cString;
322
324public:
325 static cString Demangle(char *s);
332 static void BackTrace(cStringList &StringList, int Level = 0, bool Mangled = false);
337 static void BackTrace(FILE *f = NULL, int Level = 0, bool Mangled = false);
342 static cString GetCaller(int Level = 0, bool Mangled = false);
347 };
348
349// SystemExec() implements a 'system()' call that closes all unnecessary file
350// descriptors in the child process.
351// With Detached=true, calls command in background and in a separate session,
352// with stdin connected to /dev/null.
353
354int SystemExec(const char *Command, bool Detached = false);
355
356#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:555
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:578
static cString Demangle(char *s)
Demangles the function name in the given string and returns the converted version of s.
Definition thread.c:477
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:47
pthread_cond_t cond
Definition thread.h:23
bool signaled
Definition thread.h:24
cCondWait(void)
Definition thread.c:59
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:22
cIoThrottle(void)
Definition thread.c:931
bool Active(void)
Returns true if this I/O throttling object is currently active.
Definition thread.h:297
static std::atomic< int > count
Definition thread.h:284
void Activate(void)
Activates the global I/O throttling mechanism.
Definition thread.c:941
void Release(void)
Releases the global I/O throttling mechanism.
Definition thread.c:952
bool active
Definition thread.h:285
static bool Engaged(void)
Returns true if any I/O throttling object is currently active.
Definition thread.c:963
static cMutex mutex
Definition thread.h:283
cMutexLock(cMutex *Mutex=NULL)
Definition thread.c:422
cMutex * mutex
Definition thread.h:152
bool Lock(cMutex *Mutex, int TimeoutMs=0, bool *TimedOut=NULL)
Returns true if the mutex was successfully locked.
Definition thread.c:435
bool locked
Definition thread.h:153
bool Lock(int TimeoutMs=0, bool *TimedOut=NULL)
Returns true if the mutex was successfully locked.
Definition thread.c:228
std::atomic< tThreadId > lockThreadId
Definition thread.h:72
pthread_mutex_t mutex
Definition thread.h:71
cMutex(void)
Definition thread.c:213
friend class cCondVar
Definition thread.h:69
int locked
Definition thread.h:73
void Unlock(void)
Definition thread.c:252
pid_t pid
Definition thread.h:308
int Close(void)
Definition thread.c:1038
FILE * f
Definition thread.h:309
bool Open(const char *Command, const char *Mode)
Definition thread.c:984
cPipe(void)
Definition thread.c:973
int locked
Definition thread.h:59
pthread_rwlock_t rwlock
Definition thread.h:58
cRwLock(bool PreferWriter=false)
Definition thread.c:162
std::atomic< tThreadId > writeLockThreadId
Definition thread.h:60
bool Lock(bool Write, int TimeoutMs=0)
Definition thread.c:177
void Unlock(void)
Definition thread.c:199
cStateLock * stateLock
Definition thread.h:250
cStateKey(bool IgnoreFirst=false)
Sets up a new state key.
Definition thread.c:882
int state
Definition thread.h:252
void Remove(bool IncState=true)
Removes this key from the lock it was previously used with.
Definition thread.c:904
friend class cStateLock
Definition thread.h:248
bool TimedOut(void) const
Returns true if the last lock attempt this key was used with failed due to a timeout.
Definition thread.h:276
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:899
bool timedOut
Definition thread.h:253
bool write
Definition thread.h:251
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:914
bool InLock(void)
Returns true if this key is currently in a lock.
Definition thread.h:274
tThreadId threadId
Definition thread.h:190
const char * name
Definition thread.h:189
cRwLock rwLock
Definition thread.h:191
int state
Definition thread.h:192
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:855
cStateLock(const char *Name=NULL)
Definition thread.c:750
friend class cStateKey
Definition thread.h:186
cStateKey * syncStateKey
Definition thread.h:194
int explicitModify
Definition thread.h:193
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:804
void SetSyncStateKey(cStateKey &StateKey)
Sets the given StateKey to be synchronized to the state of this lock.
Definition thread.c:834
void SetModified(void)
Sets this lock to have its state incremented when the current write lock state key is removed.
Definition thread.c:870
bool Lock(cStateKey &StateKey, bool Write=false, int TimeoutMs=0)
Tries to get a lock and returns true if successful.
Definition thread.c:760
@ emDisabled
Definition thread.h:188
@ emEnabled
Definition thread.h:188
cThreadLock(cThread *Thread=NULL)
Definition thread.c:449
bool Lock(cThread *Thread)
Definition thread.c:462
bool locked
Definition thread.h:174
cThread * thread
Definition thread.h:173
cMutex mutex
Definition thread.h:95
friend class cThreadLock
Definition thread.h:87
void SetIOPriority(int Priority)
Definition thread.c:287
void Unlock(void)
Definition thread.h:104
std::atomic< tThreadId > childThreadId
Definition thread.h:94
static void SetMainThreadId(void)
Definition thread.c:412
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:330
void SetDescription(const char *Description,...) __attribute__((format(printf
Definition thread.c:293
std::atomic< bool > active
Definition thread.h:91
bool Running(void)
Returns false if a derived cThread object shall leave its Action() function.
Definition thread.h:110
void SetPriority(int Priority)
Definition thread.c:281
void Lock(void)
Definition thread.h:103
cThread(const char *Description=NULL, bool LowPriority=false)
Creates a new thread.
Definition thread.c:264
bool lowPriority
Definition thread.h:97
static void * StartThread(cThread *Thread)
Definition thread.c:305
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:380
std::atomic< bool > running
Definition thread.h:92
static tThreadId mainThreadId
Definition thread.h:98
static tThreadId IsMainThread(void)
Definition thread.h:140
pthread_t childTid
Definition thread.h:93
bool Active(void)
Checks whether the thread is still alive.
Definition thread.c:355
static tThreadId ThreadId(void)
Definition thread.c:399
char * description
Definition thread.h:96
struct __attribute__((packed))
Definition pes2ts.c:138
int SystemExec(const char *Command, bool Detached)
Definition thread.c:1077
pid_t tThreadId
Definition thread.h:18