vdr  2.2.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 3.2 2015/01/14 11:39: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 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)));
123  bool Start(void);
126  bool Active(void);
128  static tThreadId ThreadId(void);
129  static tThreadId IsMainThread(void) { return ThreadId() == mainThreadId; }
130  static void SetMainThreadId(void);
131  };
132 
133 // cMutexLock can be used to easily set a lock on mutex and make absolutely
134 // sure that it will be unlocked when the block will be left. Several locks can
135 // be stacked, so a function that makes many calls to another function which uses
136 // cMutexLock may itself use a cMutexLock to make one longer lock instead of many
137 // short ones.
138 
139 class cMutexLock {
140 private:
142  bool locked;
143 public:
144  cMutexLock(cMutex *Mutex = NULL);
145  ~cMutexLock();
146  bool Lock(cMutex *Mutex);
147  };
148 
149 // cThreadLock can be used to easily set a lock in a thread and make absolutely
150 // sure that it will be unlocked when the block will be left. Several locks can
151 // be stacked, so a function that makes many calls to another function which uses
152 // cThreadLock may itself use a cThreadLock to make one longer lock instead of many
153 // short ones.
154 
155 class cThreadLock {
156 private:
158  bool locked;
159 public:
160  cThreadLock(cThread *Thread = NULL);
161  ~cThreadLock();
162  bool Lock(cThread *Thread);
163  };
164 
165 #define LOCK_THREAD cThreadLock ThreadLock(this)
166 
167 class cIoThrottle {
168 private:
169  static cMutex mutex;
170  static int count;
171  bool active;
172 public:
173  cIoThrottle(void);
174  ~cIoThrottle();
175  void Activate(void);
179  void Release(void);
183  bool Active(void) { return active; }
185  static bool Engaged(void);
187  };
188 
189 // cPipe implements a pipe that closes all unnecessary file descriptors in
190 // the child process.
191 
192 class cPipe {
193 private:
194  pid_t pid;
195  FILE *f;
196 public:
197  cPipe(void);
198  ~cPipe();
199  operator FILE* () { return f; }
200  bool Open(const char *Command, const char *Mode);
201  int Close(void);
202  };
203 
204 // SystemExec() implements a 'system()' call that closes all unnecessary file
205 // descriptors in the child process.
206 // With Detached=true, calls command in background and in a separate session,
207 // with stdin connected to /dev/null.
208 
209 int SystemExec(const char *Command, bool Detached = false);
210 
211 #endif //__THREAD_H
static cMutex mutex
Definition: thread.h:169
void Lock(void)
Definition: thread.c:191
bool Active(void)
Returns true if this I/O throttling object is currently active.
Definition: thread.h:183
FILE * f
Definition: thread.h:195
void Signal(void)
Signals a caller of Wait() that the condition it is waiting for is met.
Definition: thread.c:85
static tThreadId IsMainThread(void)
Definition: thread.h:129
pid_t pid
Definition: thread.h:194
pthread_t childTid
Definition: thread.h:82
static int count
Definition: thread.h:170
void Unlock(void)
Definition: thread.h:93
bool active
Definition: thread.h:171
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:142
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:158
bool active
Definition: thread.h:80
cCondWait(void)
Definition: thread.c:43
char * description
Definition: thread.h:85
pid_t tThreadId
Definition: thread.h:75
cThread * thread
Definition: thread.h:157
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
pthread_cond_t cond
Definition: thread.h:44
tThreadId childThreadId
Definition: thread.h:83
cMutex * mutex
Definition: thread.h:141
cMutex mutex
Definition: thread.h:84
bool signaled
Definition: thread.h:21
Definition: thread.h:192
Definition: thread.h:53
int locked
Definition: thread.h:67
Definition: thread.h:77
int SystemExec(const char *Command, bool Detached=false)
Definition: thread.c:559
bool lowPriority
Definition: thread.h:86
pthread_mutex_t mutex
Definition: thread.h:19
bool running
Definition: thread.h:81
static tThreadId mainThreadId
Definition: thread.h:87
void Lock(void)
Definition: thread.h:92
void Unlock(void)
Definition: thread.c:197