Thread.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00023 #ifndef _CLASS_BEE_LANG_THREAD_H
00024 #define _CLASS_BEE_LANG_THREAD_H
00025
00026 #ifdef __cplusplus
00027
00028 #include "beecrypt/c++/lang/String.h"
00029 using beecrypt::lang::String;
00030 #include "beecrypt/c++/lang/Runnable.h"
00031 using beecrypt::lang::Runnable;
00032 #include "beecrypt/c++/lang/IllegalThreadStateException.h"
00033 using beecrypt::lang::IllegalThreadStateException;
00034
00035 #include <map>
00036 using std::map;
00037
00038 namespace beecrypt {
00039 namespace lang {
00042 class BEECRYPTCXXAPI Thread : public Object, public virtual Runnable
00043 {
00044 friend class Object;
00045 friend class beecrypt::util::concurrent::locks::ReentrantLock;
00046
00047 friend void MonitorEnter(const Object*);
00048 friend void MonitorExit(const Object*) throw (IllegalMonitorStateException);
00049
00050 public:
00053 enum BEECRYPTCXXAPI State
00054 {
00055 NEW,
00056 RUNNABLE,
00057 BLOCKED,
00058 WAITING,
00059 TIMED_WAITING,
00060 TERMINATED
00061 };
00062
00063 private:
00064 Runnable* _target;
00065 String _name;
00066
00067 bc_thread_t _thr;
00068 bc_threadid_t _tid;
00069 size_t _stacksize;
00070 bool _daemon;
00071 bool _interrupted;
00072
00073 State _state;
00074 Monitor* _monitoring;
00075 Monitor* _joiner;
00076
00077 #if WIN32
00078 static DWORD WINAPI start_routine(void*);
00079 #else
00080 static void* start_routine(void*);
00081 #endif
00082
00083 void terminate();
00084
00085 private:
00086 typedef map<bc_threadid_t,Thread*> thread_map;
00087 typedef map<bc_threadid_t,Thread*>::iterator thread_map_iterator;
00088
00089 static thread_map _tmap;
00090 static bc_mutex_t _tmap_lock;
00091 static bc_mutex_t init();
00092
00093 static Thread _main;
00094
00095 static Thread* find(bc_threadid_t);
00096
00097 public:
00098 static void sleep(jlong millis) throw (InterruptedException);
00099 static void yield();
00100
00101 static Thread* currentThread();
00102
00103 private:
00104 Thread(const String&, bc_threadid_t);
00105
00106 public:
00107 Thread();
00108 Thread(const String& name);
00109 Thread(Runnable& target);
00110 Thread(Runnable& target, const String& name);
00111 Thread(Runnable& target, const String& name, size_t stacksize);
00112 virtual ~Thread();
00113
00114 virtual void run();
00115 virtual String toString() const throw ();
00116
00117 const String& getName() const throw ();
00118 Thread::State getState() const throw ();
00119 void interrupt();
00120 bool interrupted();
00121 bool isAlive() const throw ();
00122 bool isDaemon() const throw ();
00123 bool isInterrupted() const throw ();
00124 void join() throw (InterruptedException);
00125 void setDaemon(bool on) throw (IllegalThreadStateException);
00126 void start() throw (IllegalThreadStateException);
00127 };
00128 }
00129 }
00130
00131 #endif
00132
00133 #endif