00001 #ifndef _FL_THREAD_W32_H_
00002 #define _FL_THREAD_W32_H_
00003
00004
00005
00006 static int priority_values[] = {
00007 THREAD_PRIORITY_IDLE,
00008 THREAD_PRIORITY_LOWEST,
00009 THREAD_PRIORITY_BELOW_NORMAL,
00010 THREAD_PRIORITY_NORMAL,
00011 THREAD_PRIORITY_ABOVE_NORMAL,
00012 THREAD_PRIORITY_HIGHEST,
00013 THREAD_PRIORITY_TIME_CRITICAL
00014 };
00015 static int NumPriorities = 7;
00016
00017 bool Fl_Thread::create(thread_function function, void* arg)
00018 {
00019 bool result = true;
00020 _function = function;
00021 _arg = arg;
00022 _kill_thread = _th_running = 0;
00023
00024 _threadHandle = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Fl_Thread::st_th_func, this, 0, &_threadId);
00025 if(!_threadHandle)
00026 result = false;
00027 else
00028 set_priority(NORMAL_PR);
00029
00030 return result;
00031 }
00032
00033 void Fl_Thread::destroy(int exitcode)
00034 {
00035 TerminateThread(_threadHandle, exitcode);
00036 }
00037
00038 void Fl_Thread::join(int timeout)
00039 {
00040 int ret = WaitForSingleObject(_threadHandle, timeout);
00041 if( ret == WAIT_TIMEOUT ) destroy(-1);
00042 }
00043
00044
00045 int Fl_Thread::get_priority() const
00046 {
00047 int priority = GetThreadPriority(_threadHandle);
00048 for(int i=0; i < NumPriorities; i++) {
00049 if(priority_values[i] == priority) {
00050 priority = i;
00051 break;
00052 }
00053 }
00054 return priority;
00055 }
00056
00057 int Fl_Thread::set_priority(unsigned int priority)
00058 {
00059 int old = GetThreadPriority(_threadHandle);
00060 if(SetThreadPriority(_threadHandle, priority_values[priority])==0) return -1;
00061 for(int i=0; i < NumPriorities; i++) {
00062 if(priority_values[i] == old) {
00063 old = i;
00064 break;
00065 }
00066 }
00067 return old;
00068 }
00069
00070 void Fl_Thread::destroy() {
00071 if(_threadHandle) CloseHandle(_threadHandle);
00072 }
00073
00075
00076
00077 void Fl_Mutex::init() {
00078 InitializeCriticalSection(&cs);
00079 }
00080
00081 void Fl_Mutex::destroy() {
00082 DeleteCriticalSection(&cs);
00083 }
00084
00085 void Fl_Mutex::lock() {
00086 EnterCriticalSection(&cs);
00087 }
00088
00089 void Fl_Mutex::unlock() {
00090 LeaveCriticalSection(&cs);
00091 }
00092
00093 #endif