vdr  2.0.2
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
209