00001 /* 00002 * Thread.h 00003 * 00004 * Created on: 30.07.2009 00005 * Author: morgenro 00006 */ 00007 00008 #ifndef IBRCOMMON_THREAD_H_ 00009 #define IBRCOMMON_THREAD_H_ 00010 00011 #include <pthread.h> 00012 #include <sys/types.h> 00013 #include "ibrcommon/thread/Mutex.h" 00014 #include "ibrcommon/thread/Conditional.h" 00015 00016 //2 MB is a sensible default, we set this since system defaults vary widely: https://computing.llnl.gov/tutorials/pthreads/#Stack 00017 #define DEFAULT_STACKSIZE 2*1024*1024 00018 00019 namespace ibrcommon 00020 { 00021 class ThreadException : public ibrcommon::Exception 00022 { 00023 public: 00024 ThreadException(int err = 0, string what = "An error occured during a thread operation.") throw() : ibrcommon::Exception(what), error(err) 00025 { 00026 }; 00027 00028 const int error; 00029 }; 00030 00041 class Thread 00042 { 00043 00044 private: 00045 ibrcommon::Mutex _cancelLock; 00046 bool _cancel; 00047 00048 protected: 00049 pthread_t tid; 00050 size_t stack; 00051 int priority; 00052 pthread_attr_t attr; 00053 00054 ibrcommon::Conditional _readycond; 00055 bool _ready; 00056 00062 Thread(size_t stack = DEFAULT_STACKSIZE, bool delete_on_exit = false); 00063 00064 public: 00065 const bool __delete_on_exit; 00066 00071 static void yield(void); 00072 00077 static void sleep(size_t timeout); 00078 00082 virtual void run(void) = 0; 00083 00084 virtual void finally(void) { }; 00085 00089 virtual ~Thread(); 00090 00094 static void exit(void); 00095 00096 void detach(void); 00097 00102 static void concurrency(int level); 00103 00109 bool operator==(const ibrcommon::Thread &other) 00110 { 00111 return (equal(other.tid, tid) != 0); 00112 } 00113 00117 void ready(); 00118 00119 void waitready(); 00120 00121 static int enableCancel(int &state); 00122 static int disableCancel(int &state); 00123 static int restoreCancel(const int &state); 00124 00125 protected: 00126 class CancelProtector 00127 { 00128 public: 00129 CancelProtector(bool enable = false); 00130 ~CancelProtector(); 00131 00132 private: 00133 int _oldstate; 00134 }; 00135 00136 static void testcancel(); 00137 00138 int kill(int sig); 00139 00143 void cancel() throw (ThreadException); 00144 virtual bool __cancellation() { return false; }; 00145 00146 // This sets the cancelation ability of the thread 00147 int setCancel(bool val); 00148 00155 static bool equal(pthread_t thread1, pthread_t thread2); 00156 00160 static void* exec_thread(void *obj); 00161 }; 00162 00173 class JoinableThread : protected Thread 00174 { 00175 private: 00176 ibrcommon::Mutex running_mutex; 00177 ibrcommon::Mutex join_mutex; 00178 volatile bool running; 00179 volatile bool joined; 00180 00181 protected: 00186 JoinableThread(size_t size = DEFAULT_STACKSIZE); 00187 00188 public: 00193 virtual ~JoinableThread(); 00194 00200 void join(void); 00201 00206 inline bool isRunning(void) 00207 {return running;}; 00208 00217 void start(int priority = 0) throw (ThreadException); 00218 00222 void stop() throw (ThreadException); 00223 00228 inline void background(void) 00229 {start(-1);}; 00230 }; 00231 00239 class DetachedThread : protected Thread 00240 { 00241 protected: 00246 DetachedThread(size_t size = DEFAULT_STACKSIZE); 00247 00248 public: 00254 virtual ~DetachedThread(); 00255 00264 void exit(void); 00265 00272 void start(int priority = 0) throw (ThreadException); 00273 00277 void stop() throw (ThreadException); 00278 }; 00279 00280 class ThreadFinalizer 00281 { 00282 public: 00283 ThreadFinalizer(Thread *t); 00284 ~ThreadFinalizer(); 00285 00286 private: 00287 Thread *_thread; 00288 }; 00289 } 00290 00291 #endif /* THREAD_H_ */
1.7.1