vdr 2.7.4
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 4.6 2020/09/16 13:48:33 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 int locked;
72public:
73 cMutex(void);
74 ~cMutex();
75 void Lock(void);
76 void Unlock(void);
77 };
78
79class cThread {
80 friend class cThreadLock;
81private:
82 bool active;
83 bool running;
84 pthread_t childTid;
90 static void *StartThread(cThread *Thread);
91protected:
92 void SetPriority(int Priority);
93 void SetIOPriority(int Priority);
94 void Lock(void) { mutex.Lock(); }
95 void Unlock(void) { mutex.Unlock(); }
96 virtual void Action(void) = 0;
101 bool Running(void) { return running; }
104 void Cancel(int WaitSeconds = 0);
111public:
112 cThread(const char *Description = NULL, bool LowPriority = false);
119 virtual ~cThread();
120 void SetDescription(const char *Description, ...) __attribute__ ((format (printf, 2, 3)));
125 bool Start(void);
128 bool Active(void);
130 static tThreadId ThreadId(void);
131 static tThreadId IsMainThread(void) { return ThreadId() == mainThreadId; }
132 static void SetMainThreadId(void);
133 };
134
135// cMutexLock can be used to easily set a lock on mutex and make absolutely
136// sure that it will be unlocked when the block will be left. Several locks can
137// be stacked, so a function that makes many calls to another function which uses
138// cMutexLock may itself use a cMutexLock to make one longer lock instead of many
139// short ones.
140
142private:
144 bool locked;
145public:
146 cMutexLock(cMutex *Mutex = NULL);
147 ~cMutexLock();
148 bool Lock(cMutex *Mutex);
149 };
150
151// cThreadLock can be used to easily set a lock in a thread and make absolutely
152// sure that it will be unlocked when the block will be left. Several locks can
153// be stacked, so a function that makes many calls to another function which uses
154// cThreadLock may itself use a cThreadLock to make one longer lock instead of many
155// short ones.
156
158private:
160 bool locked;
161public:
162 cThreadLock(cThread *Thread = NULL);
163 ~cThreadLock();
164 bool Lock(cThread *Thread);
165 };
166
167#define LOCK_THREAD cThreadLock ThreadLock(this)
168
169class cStateKey;
170
172 friend class cStateKey;
173private:
175 const char *name;
178 int state;
181 void Unlock(cStateKey &StateKey, bool IncState = true);
186public:
187 cStateLock(const char *Name = NULL);
188 bool Lock(cStateKey &StateKey, bool Write = false, int TimeoutMs = 0);
216 void SetSyncStateKey(cStateKey &StateKey);
222 void SetExplicitModify(void);
227 void SetModified(void);
231 };
232
234 friend class cStateLock;
235private:
237 bool write;
238 int state;
240public:
241 cStateKey(bool IgnoreFirst = false);
245 ~cStateKey();
246 void Reset(void);
250 void Remove(bool IncState = true);
255 bool StateChanged(void);
260 bool InLock(void) { return stateLock; }
262 bool TimedOut(void) const { return timedOut; }
265 };
266
268private:
269 static cMutex mutex;
270 static int count;
271 bool active;
272public:
273 cIoThrottle(void);
274 ~cIoThrottle();
275 void Activate(void);
279 void Release(void);
283 bool Active(void) { return active; }
285 static bool Engaged(void);
287 };
288
289// cPipe implements a pipe that closes all unnecessary file descriptors in
290// the child process.
291
292class cPipe {
293private:
294 pid_t pid;
295 FILE *f;
296public:
297 cPipe(void);
298 ~cPipe();
299 operator FILE* () { return f; }
300 bool Open(const char *Command, const char *Mode);
301 int Close(void);
302 };
303
304// cBackTrace can be used for debugging.
305
306class cStringList;
307class cString;
308
310public:
311 static cString Demangle(char *s);
318 static void BackTrace(cStringList &StringList, int Level = 0, bool Mangled = false);
323 static void BackTrace(FILE *f = NULL, int Level = 0, bool Mangled = false);
328 static cString GetCaller(int Level = 0, bool Mangled = false);
333 };
334
335// SystemExec() implements a 'system()' call that closes all unnecessary file
336// descriptors in the child process.
337// With Detached=true, calls command in background and in a separate session,
338// with stdin connected to /dev/null.
339
340int SystemExec(const char *Command, bool Detached = false);
341
342#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:519
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:542
static cString Demangle(char *s)
Demangles the function name in the given string and returns the converted version of s.
Definition thread.c:441
void Wait(cMutex &Mutex)
Definition thread.c:121
cCondVar(void)
Definition thread.c:110
bool TimedWait(cMutex &Mutex, int TimeoutMs)
Definition thread.c:132
void Broadcast(void)
Definition thread.c:150
pthread_cond_t cond
Definition thread.h:46
~cCondVar()
Definition thread.c:115
pthread_cond_t cond
Definition thread.h:22
bool signaled
Definition thread.h:23
cCondWait(void)
Definition thread.c:58
~cCondWait()
Definition thread.c:65
bool Wait(int TimeoutMs=0)
Waits at most TimeoutMs milliseconds for a call to Signal(), or forever if TimeoutMs is 0.
Definition thread.c:78
void Signal(void)
Signals a caller of Wait() that the condition it is waiting for is met.
Definition thread.c:100
static void SleepMs(int TimeoutMs)
Creates a cCondWait object and uses it to sleep for TimeoutMs milliseconds, immediately giving up the...
Definition thread.c:72
pthread_mutex_t mutex
Definition thread.h:21
cIoThrottle(void)
Definition thread.c:895
bool Active(void)
Returns true if this I/O throttling object is currently active.
Definition thread.h:283
static int count
Definition thread.h:270
void Activate(void)
Activates the global I/O throttling mechanism.
Definition thread.c:905
~cIoThrottle()
Definition thread.c:900
void Release(void)
Releases the global I/O throttling mechanism.
Definition thread.c:916
bool active
Definition thread.h:271
static bool Engaged(void)
Returns true if any I/O throttling object is currently active.
Definition thread.c:927
static cMutex mutex
Definition thread.h:269
cMutexLock(cMutex *Mutex=NULL)
Definition thread.c:387
bool Lock(cMutex *Mutex)
Definition thread.c:400
~cMutexLock()
Definition thread.c:394
cMutex * mutex
Definition thread.h:143
bool locked
Definition thread.h:144
void Lock(void)
Definition thread.c:222
pthread_mutex_t mutex
Definition thread.h:70
cMutex(void)
Definition thread.c:208
~cMutex()
Definition thread.c:217
friend class cCondVar
Definition thread.h:68
int locked
Definition thread.h:71
void Unlock(void)
Definition thread.c:228
pid_t pid
Definition thread.h:294
int Close(void)
Definition thread.c:1002
FILE * f
Definition thread.h:295
bool Open(const char *Command, const char *Mode)
Definition thread.c:948
cPipe(void)
Definition thread.c:937
~cPipe()
Definition thread.c:943
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:157
bool Lock(bool Write, int TimeoutMs=0)
Definition thread.c:172
void Unlock(void)
Definition thread.c:194
~cRwLock()
Definition thread.c:167
cStateLock * stateLock
Definition thread.h:236
cStateKey(bool IgnoreFirst=false)
Sets up a new state key.
Definition thread.c:846
int state
Definition thread.h:238
void Remove(bool IncState=true)
Removes this key from the lock it was previously used with.
Definition thread.c:868
friend class cStateLock
Definition thread.h:234
~cStateKey()
Definition thread.c:855
bool TimedOut(void) const
Returns true if the last lock attempt this key was used with failed due to a timeout.
Definition thread.h:262
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:863
bool timedOut
Definition thread.h:239
bool write
Definition thread.h:237
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:878
bool InLock(void)
Returns true if this key is currently in a lock.
Definition thread.h:260
tThreadId threadId
Definition thread.h:176
const char * name
Definition thread.h:175
cRwLock rwLock
Definition thread.h:177
int state
Definition thread.h:178
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:819
cStateLock(const char *Name=NULL)
Definition thread.c:714
friend class cStateKey
Definition thread.h:172
cStateKey * syncStateKey
Definition thread.h:180
int explicitModify
Definition thread.h:179
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:768
void SetSyncStateKey(cStateKey &StateKey)
Sets the given StateKey to be synchronized to the state of this lock.
Definition thread.c:798
void SetModified(void)
Sets this lock to have its state incremented when the current write lock state key is removed.
Definition thread.c:834
bool Lock(cStateKey &StateKey, bool Write=false, int TimeoutMs=0)
Tries to get a lock and returns true if successful.
Definition thread.c:724
@ emDisabled
Definition thread.h:174
@ emEnabled
Definition thread.h:174
cThreadLock(cThread *Thread=NULL)
Definition thread.c:413
bool Lock(cThread *Thread)
Definition thread.c:426
bool locked
Definition thread.h:160
~cThreadLock()
Definition thread.c:420
cThread * thread
Definition thread.h:159
cMutex mutex
Definition thread.h:86
virtual ~cThread()
Definition thread.c:249
friend class cThreadLock
Definition thread.h:80
void SetIOPriority(int Priority)
Definition thread.c:261
void Unlock(void)
Definition thread.h:95
static void SetMainThreadId(void)
Definition thread.c:377
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:304
void SetDescription(const char *Description,...) __attribute__((format(printf
Definition thread.c:267
bool Running(void)
Returns false if a derived cThread object shall leave its Action() function.
Definition thread.h:101
void SetPriority(int Priority)
Definition thread.c:255
bool active
Definition thread.h:82
void Lock(void)
Definition thread.h:94
tThreadId childThreadId
Definition thread.h:85
cThread(const char *Description=NULL, bool LowPriority=false)
Creates a new thread.
Definition thread.c:238
bool lowPriority
Definition thread.h:88
bool running
Definition thread.h:83
static void * StartThread(cThread *Thread)
Definition thread.c:279
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:354
static tThreadId mainThreadId
Definition thread.h:89
static tThreadId IsMainThread(void)
Definition thread.h:131
pthread_t childTid
Definition thread.h:84
bool Active(void)
Checks whether the thread is still alive.
Definition thread.c:329
static tThreadId ThreadId(void)
Definition thread.c:372
char * description
Definition thread.h:87
struct __attribute__((packed))
Definition recording.c:2728
pid_t tThreadId
Definition thread.h:17
int SystemExec(const char *Command, bool Detached=false)
Definition thread.c:1041