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
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #pragma once
00039 namespace rp{ namespace hal{
00040
00041 class Event
00042 {
00043 public:
00044
00045 enum
00046 {
00047 EVENT_OK = 1,
00048 EVENT_TIMEOUT = -1,
00049 EVENT_FAILED = 0,
00050 };
00051
00052 Event(bool isAutoReset = true, bool isSignal = false)
00053 #ifdef _WIN32
00054 : _event(NULL)
00055 #else
00056 : _is_signalled(isSignal)
00057 , _isAutoReset(isAutoReset)
00058 #endif
00059 {
00060 #ifdef _WIN32
00061 _event = CreateEvent(NULL, isAutoReset?FALSE:TRUE, isSignal?TRUE:FALSE, NULL);
00062 #else
00063 pthread_mutex_init(&_cond_locker, NULL);
00064 pthread_cond_init(&_cond_var, NULL);
00065 #endif
00066 }
00067
00068 ~ Event()
00069 {
00070 release();
00071 }
00072
00073 void set( bool isSignal = true )
00074 {
00075 if (isSignal){
00076 #ifdef _WIN32
00077 SetEvent(_event);
00078 #else
00079 pthread_mutex_lock(&_cond_locker);
00080
00081 if ( _is_signalled == false )
00082 {
00083 _is_signalled = true;
00084 pthread_cond_signal(&_cond_var);
00085 }
00086 pthread_mutex_unlock(&_cond_locker);
00087 #endif
00088 }
00089 else
00090 {
00091 #ifdef _WIN32
00092 ResetEvent(_event);
00093 #else
00094 pthread_mutex_lock(&_cond_locker);
00095 _is_signalled = false;
00096 pthread_mutex_unlock(&_cond_locker);
00097 #endif
00098 }
00099 }
00100
00101 unsigned long wait( unsigned long timeout = 0xFFFFFFFF )
00102 {
00103 #ifdef _WIN32
00104 switch (WaitForSingleObject(_event, timeout==0xFFFFFFF?INFINITE:(DWORD)timeout))
00105 {
00106 case WAIT_FAILED:
00107 return EVENT_FAILED;
00108 case WAIT_OBJECT_0:
00109 return EVENT_OK;
00110 case WAIT_TIMEOUT:
00111 return EVENT_TIMEOUT;
00112 }
00113 return EVENT_OK;
00114 #else
00115 unsigned long ans = EVENT_OK;
00116 pthread_mutex_lock( &_cond_locker );
00117
00118 if ( !_is_signalled )
00119 {
00120
00121 if (timeout == 0xFFFFFFFF){
00122 pthread_cond_wait(&_cond_var,&_cond_locker);
00123 }else
00124 {
00125 timespec wait_time;
00126 timeval now;
00127 gettimeofday(&now,NULL);
00128
00129 wait_time.tv_sec = timeout/1000 + now.tv_sec;
00130 wait_time.tv_nsec = (timeout%1000)*1000000ULL + now.tv_usec*1000;
00131
00132 if (wait_time.tv_nsec >= 1000000000)
00133 {
00134 ++wait_time.tv_sec;
00135 wait_time.tv_nsec -= 1000000000;
00136 }
00137 switch (pthread_cond_timedwait(&_cond_var,&_cond_locker,&wait_time))
00138 {
00139 case 0:
00140
00141 break;
00142 case ETIMEDOUT:
00143
00144 ans = EVENT_TIMEOUT;
00145 goto _final;
00146 break;
00147 default:
00148 ans = EVENT_FAILED;
00149 goto _final;
00150 }
00151
00152 }
00153 }
00154
00155 assert(_is_signalled);
00156
00157 if ( _isAutoReset )
00158 {
00159 _is_signalled = false;
00160 }
00161 _final:
00162 pthread_mutex_unlock( &_cond_locker );
00163
00164 return ans;
00165 #endif
00166
00167 }
00168 protected:
00169
00170 void release()
00171 {
00172 #ifdef _WIN32
00173 CloseHandle(_event);
00174 #else
00175 pthread_mutex_destroy(&_cond_locker);
00176 pthread_cond_destroy(&_cond_var);
00177 #endif
00178 }
00179
00180 #ifdef _WIN32
00181 HANDLE _event;
00182 #else
00183 pthread_cond_t _cond_var;
00184 pthread_mutex_t _cond_locker;
00185 bool _is_signalled;
00186 bool _isAutoReset;
00187 #endif
00188 };
00189 }}