|
SASC
DVBSoftCAM
|
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 1.39 2007/02/24 16:13:28 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 Lock(void) { mutex.Lock(); } 00091 void Unlock(void) { mutex.Unlock(); } 00092 virtual void Action(void) = 0; 00097 bool Running(void) { return running; } 00100 void Cancel(int WaitSeconds = 0); 00107 public: 00108 cThread(const char *Description = NULL); 00113 virtual ~cThread(); 00114 void SetDescription(const char *Description, ...) __attribute__ ((format (printf, 2, 3))); 00115 bool Start(void); 00118 bool Active(void); 00120 static tThreadId ThreadId(void); 00121 static tThreadId IsMainThread(void) { return ThreadId() == mainThreadId; } 00122 static void SetMainThreadId(void); 00123 }; 00124 00125 // cMutexLock can be used to easily set a lock on mutex and make absolutely 00126 // sure that it will be unlocked when the block will be left. Several locks can 00127 // be stacked, so a function that makes many calls to another function which uses 00128 // cMutexLock may itself use a cMutexLock to make one longer lock instead of many 00129 // short ones. 00130 00131 class cMutexLock { 00132 private: 00133 cMutex *mutex; 00134 bool locked; 00135 public: 00136 cMutexLock(cMutex *Mutex = NULL); 00137 ~cMutexLock(); 00138 bool Lock(cMutex *Mutex); 00139 }; 00140 00141 // cThreadLock can be used to easily set a lock in a thread and make absolutely 00142 // sure that it will be unlocked when the block will be left. Several locks can 00143 // be stacked, so a function that makes many calls to another function which uses 00144 // cThreadLock may itself use a cThreadLock to make one longer lock instead of many 00145 // short ones. 00146 00147 class cThreadLock { 00148 private: 00149 cThread *thread; 00150 bool locked; 00151 public: 00152 cThreadLock(cThread *Thread = NULL); 00153 ~cThreadLock(); 00154 bool Lock(cThread *Thread); 00155 }; 00156 00157 #define LOCK_THREAD cThreadLock ThreadLock(this) 00158 00159 // cPipe implements a pipe that closes all unnecessary file descriptors in 00160 // the child process. 00161 00162 class cPipe { 00163 private: 00164 pid_t pid; 00165 FILE *f; 00166 public: 00167 cPipe(void); 00168 ~cPipe(); 00169 operator FILE* () { return f; } 00170 bool Open(const char *Command, const char *Mode); 00171 int Close(void); 00172 }; 00173 00174 // SystemExec() implements a 'system()' call that closes all unnecessary file 00175 // descriptors in the child process. 00176 // With Detached=true, calls command in background and in a separate session, 00177 // with stdin connected to /dev/null. 00178 00179 int SystemExec(const char *Command, bool Detached = false); 00180 00181 #endif //__THREAD_H