00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef IXE_Thread_h
00025 #define IXE_Thread_h
00026
00027
00028 #include "Common/Lock.h"
00029 #include "Common/Runnable.h"
00030 #include "include/error.h"
00031
00032
00033 #include <string>
00034 #include <vector>
00035
00036 namespace IXE {
00037
00038 class ThreadGroup;
00039
00040 #ifdef Yield
00041 #undef Yield // _WIN32
00042 #endif
00043
00044
00045 class InvalidThreadStateError : public RuntimeError {
00046 public:
00047 InvalidThreadStateError(std::string const& msg) : RuntimeError(msg) {}
00048 };
00049
00068 class Thread : public Runnable, public Lock
00069 {
00070 public:
00071 enum State
00072 {
00073 Created,
00074 Running,
00075 Terminated,
00076 Detached,
00077 Destroying
00078 };
00079
00081 Thread(char const* name = "");
00082
00084 Thread(Runnable* target, char const* name = "");
00085
00089 Thread(ThreadGroup* group, Runnable* target = 0, char const* name = "");
00090
00091 virtual ~Thread();
00092
00098 virtual void Join(int millis = -1, int nanos = 0);
00099
00100
00101
00102
00103 std::string Name() { return name; }
00104 virtual void Name(std::string str) { name = str; }
00108 virtual int Priority();
00112 virtual void Priority(int newPriority);
00113 ThreadGroup* Group() { return group; }
00114
00115
00116 bool IsAlive();
00117
00122 virtual void Start();
00123
00129 virtual void Run();
00130
00134 int ActiveCount();
00135
00140 static void Yield();
00141
00146 static void Sleep(int ms, int ns = 0);
00147
00148 static Thread* CurrentThread();
00149
00150 private:
00151 bool inCurrentThread();
00152
00153 # ifdef _WIN32
00154 HANDLE thread;
00155 static HANDLE CurrentThreadId();
00156 # else
00157 pthread_t thread;
00158 static pthread_t CurrentThreadId();
00159 # endif
00160 std::string name;
00161 Runnable* runnable;
00162 State state;
00163 ThreadGroup* group;
00164 # ifndef _WIN32
00165
00168 Lock starting;
00169 # endif
00170 Condition termination;
00171
00172 static TRESULT Main(void*);
00173 };
00174
00175 }
00176
00177 #endif // IXE_Thread_h