vdr  2.0.5
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 2.4 2013/02/16 15:20:44 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 class cCondWait {
18 private:
19  pthread_mutex_t mutex;
20  pthread_cond_t cond;
21  bool signaled;
22 public:
23  cCondWait(void);
24  ~cCondWait();
25  static void SleepMs(int TimeoutMs);
31  bool Wait(int TimeoutMs = 0);
36  void Signal(void);
38  };
39 
40 class cMutex;
41 
42 class cCondVar {
43 private:
44  pthread_cond_t cond;
45 public:
46  cCondVar(void);
47  ~cCondVar();
48  void Wait(cMutex &Mutex);
49  bool TimedWait(cMutex &Mutex, int TimeoutMs);
50  void Broadcast(void);
51  };
52 
53 class cRwLock {
54 private:
55  pthread_rwlock_t rwlock;
56 public:
57  cRwLock(bool PreferWriter = false);
58  ~cRwLock();
59  bool Lock(bool Write, int TimeoutMs = 0);
60  void Unlock(void);
61  };
62 
63 class cMutex {
64  friend class cCondVar;
65 private:
66  pthread_mutex_t mutex;
67  int locked;
68 public:
69  cMutex(void);
70  ~cMutex();
71  void Lock(void);
72  void Unlock(void);
73  };
74 
75 typedef pid_t tThreadId;
76 
77 class cThread {
78  friend class cThreadLock;
79 private:
80  bool active;
81  bool running;
82  pthread_t childTid;
85  char *description;
88  static void *StartThread(cThread *Thread);
89 protected:
90  void SetPriority(int Priority);
91  void SetIOPriority(int Priority);
92  void Lock(void) { mutex.Lock(); }
93  void Unlock(void) { mutex.Unlock(); }
94  virtual void Action(void) = 0;
99  bool Running(void) { return running; }
102  void Cancel(int WaitSeconds = 0);
109 public:
110  cThread(const char *Description = NULL, bool LowPriority = false);
117  virtual ~cThread();
118  void SetDescription(const char *Description, ...) __attribute__ ((format (printf, 2, 3)));
119  bool Start(void);
122  bool Active(void);
124  static tThreadId ThreadId(void);
125  static tThreadId IsMainThread(void) { return ThreadId() == mainThreadId; }
126  static void SetMainThreadId(void);
127  };
128 
129 // cMutexLock can be used to easily set a lock on mutex and make absolutely
130 // sure that it will be unlocked when the block will be left. Several locks can
131 // be stacked, so a function that makes many calls to another function which uses
132 // cMutexLock may itself use a cMutexLock to make one longer lock instead of many
133 // short ones.
134 
135 class cMutexLock {
136 private:
138  bool locked;
139 public:
140  cMutexLock(cMutex *Mutex = NULL);
141  ~cMutexLock();
142  bool Lock(cMutex *Mutex);
143  };
144 
145 // cThreadLock can be used to easily set a lock in a thread and make absolutely
146 // sure that it will be unlocked when the block will be left. Several locks can
147 // be stacked, so a function that makes many calls to another function which uses
148 // cThreadLock may itself use a cThreadLock to make one longer lock instead of many
149 // short ones.
150 
151 class cThreadLock {
152 private:
154  bool locked;
155 public:
156  cThreadLock(cThread *Thread = NULL);
157  ~cThreadLock();
158  bool Lock(cThread *Thread);
159  };
160 
161 #define LOCK_THREAD cThreadLock ThreadLock(this)
162 
163 class cIoThrottle {
164 private:
165  static cMutex mutex;
166  static int count;
167  bool active;
168 public:
169  cIoThrottle(void);
170  ~cIoThrottle();
171  void Activate(void);
175  void Release(void);
179  bool Active(void) { return active; }
181  static bool Engaged(void);
183  };
184 
185 
186 // cPipe implements a pipe that closes all unnecessary file descriptors in
187 // the child process.
188 
189 class cPipe {
190 private:
191  pid_t pid;
192  FILE *f;
193 public:
194  cPipe(void);
195  ~cPipe();
196  operator FILE* () { return f; }
197  bool Open(const char *Command, const char *Mode);
198  int Close(void);
199  };
200 
201 // SystemExec() implements a 'system()' call that closes all unnecessary file
202 // descriptors in the child process.
203 // With Detached=true, calls command in background and in a separate session,
204 // with stdin connected to /dev/null.
205 
206 int SystemExec(const char *Command, bool Detached = false);
207 
208 #endif //__THREAD_H
static cMutex mutex
Definition: thread.h:165
void Lock(void)
Definition: thread.c:191
virtual void Action(void)=0
A derived cThread class must implement the code it wants to execute as a separate thread in this func...
bool Active(void)
Returns true if this I/O throttling object is currently active.
Definition: thread.h:179
void SetDescription(const char *Description,...) __attribute__((format(printf
Definition: thread.c:236
~cIoThrottle()
Definition: thread.c:416
FILE * f
Definition: thread.h:192
void Signal(void)
Signals a caller of Wait() that the condition it is waiting for is met.
Definition: thread.c:85
~cThreadLock()
Definition: thread.c:389
bool Lock(cMutex *Mutex)
Definition: thread.c:369
void SetPriority(int Priority)
Definition: thread.c:224
cThreadLock(cThread *Thread=NULL)
Definition: thread.c:382
static tThreadId IsMainThread(void)
Definition: thread.h:125
virtual ~cThread()
Definition: thread.c:218
pid_t pid
Definition: thread.h:191
pthread_t childTid
Definition: thread.h:82
static int count
Definition: thread.h:166
cRwLock(bool PreferWriter=false)
Definition: thread.c:142
cIoThrottle(void)
Definition: thread.c:411
void Unlock(void)
Definition: thread.c:170
~cCondVar()
Definition: thread.c:100
cMutexLock(cMutex *Mutex=NULL)
Definition: thread.c:356
~cMutex()
Definition: thread.c:186
void Unlock(void)
Definition: thread.h:93
bool active
Definition: thread.h:167
pthread_mutex_t mutex
Definition: thread.h:66
~cCondWait()
Definition: thread.c:50
pthread_rwlock_t rwlock
Definition: thread.h:55
bool locked
Definition: thread.h:138
void Broadcast(void)
Definition: thread.c:135
pthread_cond_t cond
Definition: thread.h:20
static void SleepMs(int TimeoutMs)
Creates a cCondWait object and uses it to sleep for TimeoutMs milliseconds, immediately giving up the...
Definition: thread.c:57
bool locked
Definition: thread.h:154
static void * StartThread(cThread *Thread)
Definition: thread.c:248
bool active
Definition: thread.h:80
void bool Start(void)
Actually starts the thread.
Definition: thread.c:273
cCondWait(void)
Definition: thread.c:43
bool Open(const char *Command, const char *Mode)
Definition: thread.c:464
char * description
Definition: thread.h:85
bool Lock(bool Write, int TimeoutMs=0)
Definition: thread.c:155
pid_t tThreadId
Definition: thread.h:75
cThread * thread
Definition: thread.h:153
bool Running(void)
Returns false if a derived cThread object shall leave its Action() function.
Definition: thread.h:99
bool Wait(int TimeoutMs=0)
Waits at most TimeoutMs milliseconds for a call to Signal(), or forever if TimeoutMs is 0...
Definition: thread.c:63
Definition: thread.h:63
static tThreadId ThreadId(void)
Definition: thread.c:341
bool TimedWait(cMutex &Mutex, int TimeoutMs)
Definition: thread.c:117
void Activate(void)
Activates the global I/O throttling mechanism.
Definition: thread.c:421
int Close(void)
Definition: thread.c:521
pthread_cond_t cond
Definition: thread.h:44
~cRwLock()
Definition: thread.c:150
tThreadId childThreadId
Definition: thread.h:83
cMutex * mutex
Definition: thread.h:137
cMutex mutex
Definition: thread.h:84
bool signaled
Definition: thread.h:21
~cPipe()
Definition: thread.c:459
Definition: thread.h:189
void SetIOPriority(int Priority)
Definition: thread.c:230
static void SetMainThreadId(void)
Definition: thread.c:346
~cMutexLock()
Definition: thread.c:363
Definition: thread.h:53
bool Active(void)
Checks whether the thread is still alive.
Definition: thread.c:298
int locked
Definition: thread.h:67
Definition: thread.h:77
static bool Engaged(void)
Returns true if any I/O throttling object is currently active.
Definition: thread.c:443
void Wait(cMutex &Mutex)
Definition: thread.c:106
cPipe(void)
Definition: thread.c:453
int SystemExec(const char *Command, bool Detached=false)
Definition: thread.c:560
bool lowPriority
Definition: thread.h:86
pthread_mutex_t mutex
Definition: thread.h:19
cCondVar(void)
Definition: thread.c:95
bool running
Definition: thread.h:81
cThread(const char *Description=NULL, bool LowPriority=false)
Creates a new thread.
Definition: thread.c:207
bool Lock(cThread *Thread)
Definition: thread.c:395
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:323
static tThreadId mainThreadId
Definition: thread.h:87
void Release(void)
Releases the global I/O throttling mechanism.
Definition: thread.c:432
void Lock(void)
Definition: thread.h:92
cMutex(void)
Definition: thread.c:177
void Unlock(void)
Definition: thread.c:197