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