vdr
1.7.27
|
00001 /* 00002 * thread.h: A simple thread base class 00003 * 00004 * See the main source file 'vdr.c' for copyright information and 00005 * how to reach the author. 00006 * 00007 * $Id: thread.h 2.1 2009/04/13 13:50:39 kls Exp $ 00008 */ 00009 00010 #ifndef __THREAD_H 00011 #define __THREAD_H 00012 00013 #include <pthread.h> 00014 #include <stdio.h> 00015 #include <sys/types.h> 00016 00017 class cCondWait { 00018 private: 00019 pthread_mutex_t mutex; 00020 pthread_cond_t cond; 00021 bool signaled; 00022 public: 00023 cCondWait(void); 00024 ~cCondWait(); 00025 static void SleepMs(int TimeoutMs); 00031 bool Wait(int TimeoutMs = 0); 00036 void Signal(void); 00038 }; 00039 00040 class cMutex; 00041 00042 class cCondVar { 00043 private: 00044 pthread_cond_t cond; 00045 public: 00046 cCondVar(void); 00047 ~cCondVar(); 00048 void Wait(cMutex &Mutex); 00049 bool TimedWait(cMutex &Mutex, int TimeoutMs); 00050 void Broadcast(void); 00051 }; 00052 00053 class cRwLock { 00054 private: 00055 pthread_rwlock_t rwlock; 00056 public: 00057 cRwLock(bool PreferWriter = false); 00058 ~cRwLock(); 00059 bool Lock(bool Write, int TimeoutMs = 0); 00060 void Unlock(void); 00061 }; 00062 00063 class cMutex { 00064 friend class cCondVar; 00065 private: 00066 pthread_mutex_t mutex; 00067 int locked; 00068 public: 00069 cMutex(void); 00070 ~cMutex(); 00071 void Lock(void); 00072 void Unlock(void); 00073 }; 00074 00075 typedef pid_t tThreadId; 00076 00077 class cThread { 00078 friend class cThreadLock; 00079 private: 00080 bool active; 00081 bool running; 00082 pthread_t childTid; 00083 tThreadId childThreadId; 00084 cMutex mutex; 00085 char *description; 00086 static tThreadId mainThreadId; 00087 static void *StartThread(cThread *Thread); 00088 protected: 00089 void SetPriority(int Priority); 00090 void SetIOPriority(int Priority); 00091 void Lock(void) { mutex.Lock(); } 00092 void Unlock(void) { mutex.Unlock(); } 00093 virtual void Action(void) = 0; 00098 bool Running(void) { return running; } 00101 void Cancel(int WaitSeconds = 0); 00108 public: 00109 cThread(const char *Description = NULL); 00114 virtual ~cThread(); 00115 void SetDescription(const char *Description, ...) __attribute__ ((format (printf, 2, 3))); 00116 bool Start(void); 00119 bool Active(void); 00121 static tThreadId ThreadId(void); 00122 static tThreadId IsMainThread(void) { return ThreadId() == mainThreadId; } 00123 static void SetMainThreadId(void); 00124 }; 00125 00126 // cMutexLock can be used to easily set a lock on mutex and make absolutely 00127 // sure that it will be unlocked when the block will be left. Several locks can 00128 // be stacked, so a function that makes many calls to another function which uses 00129 // cMutexLock may itself use a cMutexLock to make one longer lock instead of many 00130 // short ones. 00131 00132 class cMutexLock { 00133 private: 00134 cMutex *mutex; 00135 bool locked; 00136 public: 00137 cMutexLock(cMutex *Mutex = NULL); 00138 ~cMutexLock(); 00139 bool Lock(cMutex *Mutex); 00140 }; 00141 00142 // cThreadLock can be used to easily set a lock in a thread and make absolutely 00143 // sure that it will be unlocked when the block will be left. Several locks can 00144 // be stacked, so a function that makes many calls to another function which uses 00145 // cThreadLock may itself use a cThreadLock to make one longer lock instead of many 00146 // short ones. 00147 00148 class cThreadLock { 00149 private: 00150 cThread *thread; 00151 bool locked; 00152 public: 00153 cThreadLock(cThread *Thread = NULL); 00154 ~cThreadLock(); 00155 bool Lock(cThread *Thread); 00156 }; 00157 00158 #define LOCK_THREAD cThreadLock ThreadLock(this) 00159 00160 // cPipe implements a pipe that closes all unnecessary file descriptors in 00161 // the child process. 00162 00163 class cPipe { 00164 private: 00165 pid_t pid; 00166 FILE *f; 00167 public: 00168 cPipe(void); 00169 ~cPipe(); 00170 operator FILE* () { return f; } 00171 bool Open(const char *Command, const char *Mode); 00172 int Close(void); 00173 }; 00174 00175 // SystemExec() implements a 'system()' call that closes all unnecessary file 00176 // descriptors in the child process. 00177 // With Detached=true, calls command in background and in a separate session, 00178 // with stdin connected to /dev/null. 00179 00180 int SystemExec(const char *Command, bool Detached = false); 00181 00182 #endif //__THREAD_H