vdr 2.6.1
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;
23 bool signaled;
24public:
25 cCondWait(void);
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);
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);
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);
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;
87 char *description;
88 bool lowPriority;
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
141class cMutexLock {
142private:
143 cMutex *mutex;
144 bool locked;
145public:
146 cMutexLock(cMutex *Mutex = NULL);
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
157class cThreadLock {
158private:
160 bool locked;
161public:
162 cThreadLock(cThread *Thread = NULL);
164 bool Lock(cThread *Thread);
165 };
166
167#define LOCK_THREAD cThreadLock ThreadLock(this)
168
169class cStateKey;
170
171class cStateLock {
172 friend class cStateKey;
173private:
174 enum { emDisabled = 0, emArmed, emEnabled };
175 const char *name;
178 int state;
179 int explicitModify;
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);
227 void SetModified(void);
231 };
232
233class cStateKey {
234 friend class cStateLock;
235private:
237 bool write;
238 int state;
239 bool timedOut;
240public:
241 cStateKey(bool IgnoreFirst = false);
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
267class cIoThrottle {
268private:
269 static cMutex mutex;
270 static int count;
271 bool active;
272public:
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);
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
309class cBackTrace {
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.
static cString GetCaller(int Level=0, bool Mangled=false)
Returns the caller at the given Level (or the immediate caller, if Level is 0).
static cString Demangle(char *s)
Demangles the function name in the given string and returns the converted version of s.
static void BackTrace(FILE *f=NULL, int Level=0, bool Mangled=false)
Produces a backtrace beginning at the given Level, and writes it to the given file.
void Wait(cMutex &Mutex)
cCondVar(void)
bool TimedWait(cMutex &Mutex, int TimeoutMs)
void Broadcast(void)
pthread_cond_t cond
pthread_cond_t cond
cCondWait(void)
static void SleepMs(int TimeoutMs)
Creates a cCondWait object and uses it to sleep for TimeoutMs milliseconds, immediately giving up the...
bool Wait(int TimeoutMs=0)
Waits at most TimeoutMs milliseconds for a call to Signal(), or forever if TimeoutMs is 0.
void Signal(void)
Signals a caller of Wait() that the condition it is waiting for is met.
pthread_mutex_t mutex
cIoThrottle(void)
bool Active(void)
Returns true if this I/O throttling object is currently active.
Definition: thread.h:283
void Activate(void)
Activates the global I/O throttling mechanism.
void Release(void)
Releases the global I/O throttling mechanism.
static bool Engaged(void)
Returns true if any I/O throttling object is currently active.
static int count
static cMutex mutex
cMutexLock(cMutex *Mutex=NULL)
bool Lock(cMutex *Mutex)
void Lock(void)
pthread_mutex_t mutex
cMutex(void)
void Unlock(void)
int Close(void)
bool Open(const char *Command, const char *Mode)
cPipe(void)
tThreadId writeLockThreadId
pthread_rwlock_t rwlock
cRwLock(bool PreferWriter=false)
bool Lock(bool Write, int TimeoutMs=0)
void Unlock(void)
cStateKey(bool IgnoreFirst=false)
Sets up a new state key.
cStateLock * stateLock
void Remove(bool IncState=true)
Removes this key from the lock it was previously used with.
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...
bool StateChanged(void)
Returns true if this key is used for obtaining a write lock, and the lock's state differs from that o...
bool InLock(void)
Returns true if this key is currently in a lock.
Definition: thread.h:260
tThreadId threadId
const char * name
void SetExplicitModify(void)
If you have obtained a write lock on this lock, and you don't want its state to be automatically incr...
cStateLock(const char *Name=NULL)
void Unlock(cStateKey &StateKey, bool IncState=true)
Releases a lock that has been obtained by a previous call to Lock() with the given StateKey.
void SetSyncStateKey(cStateKey &StateKey)
Sets the given StateKey to be synchronized to the state of this lock.
void SetModified(void)
Sets this lock to have its state incremented when the current write lock state key is removed.
cStateKey * syncStateKey
bool Lock(cStateKey &StateKey, bool Write=false, int TimeoutMs=0)
Tries to get a lock and returns true if successful.
cThreadLock(cThread *Thread=NULL)
bool Lock(cThread *Thread)
char * description
void SetIOPriority(int Priority)
void Unlock(void)
Definition: thread.h:95
virtual ~cThread()
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
bool Running(void)
Returns false if a derived cThread object shall leave its Action() function.
Definition: thread.h:101
void SetPriority(int Priority)
void Lock(void)
Definition: thread.h:94
static void * StartThread(cThread *Thread)
tThreadId childThreadId
cThread(const char *Description=NULL, bool LowPriority=false)
Creates a new thread.
static tThreadId mainThreadId
static void SetMainThreadId(void)
void Cancel(int WaitSeconds=0)
Cancels the thread by first setting 'running' to false, so that the Action() loop can finish in an or...
static tThreadId IsMainThread(void)
pthread_t childTid
bool Active(void)
Checks whether the thread is still alive.
Definition: thread.c:329
static tThreadId ThreadId(void)
Definition: thread.c:372
pid_t tThreadId
struct __attribute__((packed))
Definition: recording.c:2534
pid_t tThreadId
Definition: thread.h:17
int SystemExec(const char *Command, bool Detached=false)
Definition: thread.c:1034