00001
00002
00003 #ifndef __SYSTEM_ABSTRACTION_HH_INCLUDDED__
00004 #define __SYSTEM_ABSTRACTION_HH_INCLUDDED__
00005
00006 #ifdef WIN32
00007 #include <windows.h>
00008 #include <assert.h>
00009
00010 class SystemLock
00011 {
00012 CRITICAL_SECTION cs;
00013 public:
00014 SystemLock() { InitializeCriticalSection(&cs); }
00015 ~SystemLock() { DeleteCriticalSection(&cs); }
00016
00017 void lock(void) { EnterCriticalSection(&cs); }
00018 void unlock(void) { LeaveCriticalSection(&cs); }
00019 };
00020
00021 class SystemThread
00022 {
00023 HANDLE handle;
00024 void * (*thread_f)(void *);
00025 void *thread_param;
00026
00027 static DWORD WINAPI ThreadProc(LPVOID lpParameter)
00028 {
00029 SystemThread *this_ptr=(SystemThread *)lpParameter;
00030 void * (*f)(void *) = this_ptr->thread_f;
00031 void *param = this_ptr->thread_param;
00032 return (DWORD)f(param);
00033 }
00034 public:
00035 SystemThread(void *(*f)(void *), void *param) : thread_f(f), thread_param(param)
00036 {
00037 DWORD thrid;
00038 handle = CreateThread(NULL, 0, &ThreadProc, (void*)this, 0, &thrid);
00039 assert(handle);
00040 }
00041 ~SystemThread() { join(INFINITE); CloseHandle(handle); }
00042 int join(unsigned milliseconds = INFINITE)
00043 {
00044 switch(WaitForSingleObject(handle, milliseconds))
00045 {
00046 case WAIT_TIMEOUT:
00047 return 1;
00048 case WAIT_OBJECT_0:
00049 return 0;
00050 case WAIT_ABANDONED:
00051 default:
00052 break;
00053 }
00054 return -1;
00055 }
00056 };
00057
00058 inline void SystemSleep(unsigned millisec=0)
00059 {
00060 Sleep(millisec);
00061 };
00062
00063 inline void thread_yield(void)
00064 {
00065 Sleep(0);
00066 }
00067
00068 #else // #ifdef WIN32
00069
00070 #include <unistd.h>
00071 #include <pthread.h>
00072 #include <sched.h>
00073
00074 class SystemLock
00075 {
00076 pthread_mutex_t mutex;
00077 public:
00078 SystemLock() { pthread_mutex_init(&mutex, NULL); }
00079 ~SystemLock() { pthread_mutex_destroy(&mutex); }
00080
00081 void lock(void) { pthread_mutex_lock(&mutex); }
00082 void unlock(void) { pthread_mutex_unlock(&mutex); }
00083 };
00084
00085 class SystemThread
00086 {
00087 pthread_t thread;
00088 void * (*thread_f)(void *);
00089 void *thread_param;
00090
00091 static void * ThreadProc(void *parameter)
00092 {
00093 SystemThread *this_ptr=(SystemThread *)parameter;
00094 return (void*)this_ptr->thread_f(this_ptr->thread_param);
00095 }
00096 public:
00097 SystemThread(void * (*f)(void *), void *param) : thread_f(f), thread_param(param)
00098 {
00099 assert( pthread_create( &thread, NULL, &SystemThread::ThreadProc, (void*)this)==0 );
00100 }
00101 ~SystemThread() { join(); }
00102 int join(unsigned milliseconds = ~(unsigned)0)
00103 {
00104 return pthread_join(thread, NULL);
00105 }
00106 };
00107
00108 inline void SystemSleep(unsigned millisec=0)
00109 {
00110 struct timeval tv;
00111 tv.tv_sec = millisec / 1000;
00112 tv.tv_usec = (millisec % 1000) * 1000;
00113 select(0, NULL, NULL, NULL, &tv);
00114 };
00115
00116 inline void thread_yield(void)
00117 {
00118 sched_yield();
00119 }
00120
00121 #endif // #ifdef WIN32
00122
00123 #endif // #ifndef __SYSTEM_ABSTRACTION_HH_INCLUDDED__