vdr  1.7.31
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.2 2012/09/20 08:46:27 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;
84  cMutex mutex;
85  char *description;
86  static tThreadId mainThreadId;
87  static void *StartThread(cThread *Thread);
88 protected:
89  void SetPriority(int Priority);
90  void SetIOPriority(int Priority);
91  void Lock(void) { mutex.Lock(); }
92  void Unlock(void) { mutex.Unlock(); }
93  virtual void Action(void) = 0;
98  bool Running(void) { return running; }
101  void Cancel(int WaitSeconds = 0);
108 public:
109  cThread(const char *Description = NULL);
114  virtual ~cThread();
115  void SetDescription(const char *Description, ...) __attribute__ ((format (printf, 2, 3)));
116  bool Start(void);
119  bool Active(void);
121  static tThreadId ThreadId(void);
122  static tThreadId IsMainThread(void) { return ThreadId() == mainThreadId; }
123  static void SetMainThreadId(void);
124  };
125 
126 // cMutexLock can be used to easily set a lock on mutex and make absolutely
127 // sure that it will be unlocked when the block will be left. Several locks can
128 // be stacked, so a function that makes many calls to another function which uses
129 // cMutexLock may itself use a cMutexLock to make one longer lock instead of many
130 // short ones.
131 
132 class cMutexLock {
133 private:
134  cMutex *mutex;
135  bool locked;
136 public:
137  cMutexLock(cMutex *Mutex = NULL);
138  ~cMutexLock();
139  bool Lock(cMutex *Mutex);
140  };
141 
142 // cThreadLock can be used to easily set a lock in a thread and make absolutely
143 // sure that it will be unlocked when the block will be left. Several locks can
144 // be stacked, so a function that makes many calls to another function which uses
145 // cThreadLock may itself use a cThreadLock to make one longer lock instead of many
146 // short ones.
147 
148 class cThreadLock {
149 private:
150  cThread *thread;
151  bool locked;
152 public:
153  cThreadLock(cThread *Thread = NULL);
154  ~cThreadLock();
155  bool Lock(cThread *Thread);
156  };
157 
158 #define LOCK_THREAD cThreadLock ThreadLock(this)
159 
160 class cIoThrottle {
161 private:
162  static cMutex mutex;
163  static int count;
164  bool active;
165 public:
166  cIoThrottle(void);
167  ~cIoThrottle();
168  void Activate(void);
172  void Release(void);
176  bool Active(void) { return active; }
178  static bool Engaged(void);
180  };
181 
182 
183 // cPipe implements a pipe that closes all unnecessary file descriptors in
184 // the child process.
185 
186 class cPipe {
187 private:
188  pid_t pid;
189  FILE *f;
190 public:
191  cPipe(void);
192  ~cPipe();
193  operator FILE* () { return f; }
194  bool Open(const char *Command, const char *Mode);
195  int Close(void);
196  };
197 
198 // SystemExec() implements a 'system()' call that closes all unnecessary file
199 // descriptors in the child process.
200 // With Detached=true, calls command in background and in a separate session,
201 // with stdin connected to /dev/null.
202 
203 int SystemExec(const char *Command, bool Detached = false);
204 
205 #endif //__THREAD_H