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