vdr  2.4.0
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.4 2018/03/04 11:27:55 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 
17 typedef pid_t tThreadId;
18 
19 class cCondWait {
20 private:
21  pthread_mutex_t mutex;
22  pthread_cond_t cond;
23  bool signaled;
24 public:
25  cCondWait(void);
26  ~cCondWait();
27  static void SleepMs(int TimeoutMs);
33  bool Wait(int TimeoutMs = 0);
38  void Signal(void);
40  };
41 
42 class cMutex;
43 
44 class cCondVar {
45 private:
46  pthread_cond_t cond;
47 public:
48  cCondVar(void);
49  ~cCondVar();
50  void Wait(cMutex &Mutex);
51  bool TimedWait(cMutex &Mutex, int TimeoutMs);
52  void Broadcast(void);
53  };
54 
55 class cRwLock {
56 private:
57  pthread_rwlock_t rwlock;
58  int locked;
60 public:
61  cRwLock(bool PreferWriter = false);
62  ~cRwLock();
63  bool Lock(bool Write, int TimeoutMs = 0);
64  void Unlock(void);
65  };
66 
67 class cMutex {
68  friend class cCondVar;
69 private:
70  pthread_mutex_t mutex;
71  int locked;
72 public:
73  cMutex(void);
74  ~cMutex();
75  void Lock(void);
76  void Unlock(void);
77  };
78 
79 class cThread {
80  friend class cThreadLock;
81 private:
82  bool active;
83  bool running;
84  pthread_t childTid;
87  char *description;
90  static void *StartThread(cThread *Thread);
91 protected:
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);
111 public:
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 
141 class cMutexLock {
142 private:
144  bool locked;
145 public:
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 
157 class cThreadLock {
158 private:
160  bool locked;
161 public:
162  cThreadLock(cThread *Thread = NULL);
163  ~cThreadLock();
164  bool Lock(cThread *Thread);
165  };
166 
167 #define LOCK_THREAD cThreadLock ThreadLock(this)
168 
169 class cStateKey;
170 
171 class cStateLock {
172  friend class cStateKey;
173 private:
174  enum { emDisabled = 0, emArmed, emEnabled };
175  const char *name;
178  int state;
181  void Unlock(cStateKey &StateKey, bool IncState = true);
186 public:
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 
233 class cStateKey {
234  friend class cStateLock;
235 private:
237  bool write;
238  int state;
239  bool timedOut;
240 public:
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 
267 class cIoThrottle {
268 private:
269  static cMutex mutex;
270  static int count;
271  bool active;
272 public:
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 
292 class cPipe {
293 private:
294  pid_t pid;
295  FILE *f;
296 public:
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 
306 class cStringList;
307 class cString;
308 
309 class cBackTrace {
310 public:
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 
340 int SystemExec(const char *Command, bool Detached = false);
341 
342 #endif //__THREAD_H
int explicitModify
Definition: thread.h:179
static cMutex mutex
Definition: thread.h:269
void Lock(void)
Definition: thread.c:222
bool Active(void)
Returns true if this I/O throttling object is currently active.
Definition: thread.h:283
int state
Definition: thread.h:238
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
tThreadId threadId
Definition: thread.h:176
FILE * f
Definition: thread.h:295
void Signal(void)
Signals a caller of Wait() that the condition it is waiting for is met.
Definition: thread.c:100
cRwLock rwLock
Definition: thread.h:177
bool timedOut
Definition: thread.h:239
static tThreadId IsMainThread(void)
Definition: thread.h:131
pid_t pid
Definition: thread.h:294
pthread_t childTid
Definition: thread.h:84
static int count
Definition: thread.h:270
tThreadId writeLockThreadId
Definition: thread.h:59
void Unlock(void)
Definition: thread.h:95
bool active
Definition: thread.h:271
pthread_mutex_t mutex
Definition: thread.h:70
~cCondWait()
Definition: thread.c:65
pthread_rwlock_t rwlock
Definition: thread.h:57
bool locked
Definition: thread.h:144
pthread_cond_t cond
Definition: thread.h:22
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
bool locked
Definition: thread.h:160
bool active
Definition: thread.h:82
cCondWait(void)
Definition: thread.c:58
friend class cStateLock
Definition: thread.h:234
cStateLock * stateLock
Definition: thread.h:236
char * description
Definition: thread.h:87
pid_t tThreadId
Definition: thread.h:17
cThread * thread
Definition: thread.h:159
bool Running(void)
Returns false if a derived cThread object shall leave its Action() function.
Definition: thread.h:101
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
Definition: thread.h:67
pthread_cond_t cond
Definition: thread.h:46
tThreadId childThreadId
Definition: thread.h:85
cMutex * mutex
Definition: thread.h:143
cMutex mutex
Definition: thread.h:86
bool signaled
Definition: thread.h:23
Definition: thread.h:292
Definition: thread.h:55
int locked
Definition: thread.h:71
const char * name
Definition: thread.h:175
Definition: thread.h:79
int state
Definition: thread.h:178
int SystemExec(const char *Command, bool Detached=false)
Definition: thread.c:1034
bool lowPriority
Definition: thread.h:88
pthread_mutex_t mutex
Definition: thread.h:21
bool running
Definition: thread.h:83
friend class cStateKey
Definition: thread.h:172
int locked
Definition: thread.h:58
cStateKey * syncStateKey
Definition: thread.h:180
static tThreadId mainThreadId
Definition: thread.h:89
bool InLock(void)
Returns true if this key is currently in a lock.
Definition: thread.h:260
bool write
Definition: thread.h:237
Definition: tools.h:176
void Lock(void)
Definition: thread.h:94
void Unlock(void)
Definition: thread.c:228