• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ibrcommon/ibrcommon/thread/Thread.h

Go to the documentation of this file.
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         protected:
00044                 enum THREAD_STATE
00045                 {
00046                         THREAD_INITIALIZED,
00047                         THREAD_PREPARE,
00048                         THREAD_RUNNING,
00049                         THREAD_CANCELLED,
00050                         THREAD_FINALIZED,
00051                         THREAD_JOINED,
00052                         THREAD_ERROR
00053                 } _state;
00054                 
00055                 ibrcommon::Conditional _state_lock;
00056 
00057                 // thread's id
00058                 pthread_t tid;
00059 
00060                 // thread's stack size
00061                 size_t stack;
00062 
00063                 // thread's priority
00064                 int priority;
00065 
00066                 // thread's attributes
00067                 pthread_attr_t attr;
00068 
00074                 Thread(size_t stack = DEFAULT_STACKSIZE, bool delete_on_exit = false);
00075 
00076         public:
00080                 virtual ~Thread() = 0;
00081 
00086                 static void yield(void);
00087 
00092                 static void sleep(size_t timeout);
00093 
00097                 virtual void setup(void) { };
00098 
00102                 virtual void run(void) = 0;
00103 
00107                 virtual void finally(void) { };
00108 
00112                 static void exit(void);
00113 
00117                 void detach(void);
00118 
00123                 static void concurrency(int level);
00124 
00130                 bool operator==(const ibrcommon::Thread &other)
00131                 {
00132                         return (equal(other.tid, tid) != 0);
00133                 }
00134 
00139                 void interrupt();
00140 
00141         protected:
00142                 class CancelProtector
00143                 {
00144                 public:
00145                         CancelProtector(bool enable = false);
00146                         virtual ~CancelProtector();
00147 
00148                 private:
00149                         int _oldstate;
00150                 };
00151 
00157                 int kill(int sig);
00158 
00163                 void cancellation_point();
00164 
00170                 void enable_interruption();
00171 
00175                 void disable_interruption();
00176 
00180                 void cancel() throw (ThreadException);
00181                 virtual bool __cancellation() { return false; };
00182 
00189                 static bool equal(pthread_t thread1, pthread_t thread2);
00190 
00194                 static void* exec_thread(void *obj);
00195 
00200                 static void finalize_thread(void *obj);
00201 
00202         private:
00206                 const bool __delete_on_exit;
00207 
00211                 sigset_t mask, orig_mask;
00212         };
00213 
00224         class JoinableThread : protected Thread
00225         {
00226         protected:
00231                 JoinableThread(size_t size = DEFAULT_STACKSIZE);
00232 
00233         public:
00238                 virtual ~JoinableThread() = 0;
00239 
00245                 void join(void);
00246 
00251                 inline bool isRunning(void)
00252                         { return _state == THREAD_RUNNING; };
00253 
00262                 void start(int priority = 0) throw (ThreadException);
00263 
00267                 void stop() throw (ThreadException);
00268         };
00269 
00277         class DetachedThread : protected Thread
00278         {
00279         protected:
00284                 DetachedThread(size_t size = DEFAULT_STACKSIZE);
00285 
00286         public:
00292                 virtual ~DetachedThread() = 0;
00293 
00300                 void start(int priority = 0) throw (ThreadException);
00301 
00305                 void stop() throw (ThreadException);
00306         };
00307 }
00308 
00309 #endif /* THREAD_H_ */

Generated on Wed Mar 30 2011 11:11:49 for IBR-DTNSuite by  doxygen 1.7.1