YDLIDAR X2 SDK  V1.4.1
locker.h
1 #pragma once
2 #ifdef _WIN32
3 #include <conio.h>
4 #include <windows.h>
5 #include <process.h>
6 #include <tlhelp32.h>
7 #include <sys/utime.h>
8 #include <io.h>
9 #include <direct.h>
10 #else
11 #include <assert.h>
12 #include <pthread.h>
13 #include <sys/time.h>
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <errno.h>
17 #endif
18 
19 
20 class Locker
21 {
22 public:
23  enum LOCK_STATUS {
24  LOCK_OK = 0,
25  LOCK_TIMEOUT = -1,
26  LOCK_FAILED = -2
27  };
28 
29  Locker(){
30 #ifdef _WIN32
31  _lock = NULL;
32 #endif
33  init();
34  }
35 
36  ~Locker(){
37  release();
38  }
39 
40  Locker::LOCK_STATUS lock(unsigned long timeout = 0xFFFFFFFF) {
41 #ifdef _WIN32
42  switch (WaitForSingleObject(_lock, timeout==0xFFFFFFF?INFINITE:(DWORD)timeout)) {
43  case WAIT_ABANDONED:
44  return LOCK_FAILED;
45  case WAIT_OBJECT_0:
46  return LOCK_OK;
47  case WAIT_TIMEOUT:
48  return LOCK_TIMEOUT;
49  }
50 
51 #else
52 #ifdef _MACOS
53  if (timeout !=0 ) {
54  if (pthread_mutex_lock(&_lock) == 0) return LOCK_OK;
55  }
56 #else
57  if (timeout == 0xFFFFFFFF){
58  if (pthread_mutex_lock(&_lock) == 0) return LOCK_OK;
59  }
60 #endif
61  else if (timeout == 0) {
62  if (pthread_mutex_trylock(&_lock) == 0) return LOCK_OK;
63  }
64 #ifndef _MACOS
65  else{
66  timespec wait_time;
67  timeval now;
68  gettimeofday(&now,NULL);
69 
70  wait_time.tv_sec = timeout/1000 + now.tv_sec;
71  wait_time.tv_nsec = (timeout%1000)*1000000 + now.tv_usec*1000;
72 
73  if (wait_time.tv_nsec >= 1000000000){
74  ++wait_time.tv_sec;
75  wait_time.tv_nsec -= 1000000000;
76  }
77 
78 #if !defined(__ANDROID__)
79 
80  switch (pthread_mutex_timedlock(&_lock,&wait_time)){
81  case 0:
82  return LOCK_OK;
83  case ETIMEDOUT:
84  return LOCK_TIMEOUT;
85  }
86 
87 #else
88  struct timeval timenow;
89  struct timespec sleepytime;
90  /* This is just to avoid a completely busy wait */
91  sleepytime.tv_sec = 0;
92  sleepytime.tv_nsec = 10000000; /* 10ms */
93 
94  while (pthread_mutex_trylock (&_lock) == EBUSY) {
95  gettimeofday (&timenow, NULL);
96 
97  if (timenow.tv_sec >= wait_time.tv_sec &&
98  (timenow.tv_usec * 1000) >= wait_time.tv_nsec) {
99  return LOCK_TIMEOUT;
100  }
101 
102  nanosleep (&sleepytime, NULL);
103  }
104 
105  return LOCK_OK;
106 
107 #endif
108 
109  }
110 #endif
111 #endif
112 
113  return LOCK_FAILED;
114  }
115 
116 
117  void unlock(){
118 #ifdef _WIN32
119  ReleaseMutex(_lock);
120 #else
121  pthread_mutex_unlock(&_lock);
122 #endif
123  }
124 
125 #ifdef _WIN32
126  HANDLE getLockHandle(){
127  return _lock;
128  }
129 #else
130  pthread_mutex_t *getLockHandle(){
131  return &_lock;
132  }
133 #endif
134 
135 
136 
137 protected:
138  void init(){
139 #ifdef _WIN32
140  _lock = CreateMutex(NULL,FALSE,NULL);
141 #else
142  pthread_mutex_init(&_lock, NULL);
143 #endif
144  }
145 
146  void release(){
147  unlock();
148 #ifdef _WIN32
149 
150  if(_lock){
151  CloseHandle(_lock);
152  }
153  _lock = NULL;
154 #else
155  pthread_mutex_destroy(&_lock);
156 #endif
157  }
158 
159 #ifdef _WIN32
160  HANDLE _lock;
161 #else
162  pthread_mutex_t _lock;
163 #endif
164 };
165 
166 
167 class Event
168 {
169 public:
170 
171  enum {
172  EVENT_OK = 1,
173  EVENT_TIMEOUT = 2,
174  EVENT_FAILED = 0,
175  };
176 
177  explicit Event(bool isAutoReset = true, bool isSignal = false)
178 #ifdef _WIN32
179  : _event(NULL)
180 #else
181  : _is_signalled(isSignal)
182  , _isAutoReset(isAutoReset)
183 #endif
184  {
185 #ifdef _WIN32
186  _event = CreateEvent(NULL, isAutoReset?FALSE:TRUE, isSignal?TRUE:FALSE, NULL);
187 #else
188  pthread_mutex_init(&_cond_locker, NULL);
189  pthread_cond_init(&_cond_var, NULL);
190 #endif
191  }
192 
193  ~ Event(){
194  release();
195  }
196 
197  void set( bool isSignal = true ){
198  if (isSignal){
199 #ifdef _WIN32
200  SetEvent(_event);
201 #else
202  pthread_mutex_lock(&_cond_locker);
203 
204  if ( _is_signalled == false ){
205  _is_signalled = true;
206  pthread_cond_signal(&_cond_var);
207  }
208  pthread_mutex_unlock(&_cond_locker);
209 #endif
210  }else{
211 #ifdef _WIN32
212  ResetEvent(_event);
213 #else
214  pthread_mutex_lock(&_cond_locker);
215  _is_signalled = false;
216  pthread_mutex_unlock(&_cond_locker);
217 #endif
218  }
219  }
220 
221  unsigned long wait( unsigned long timeout = 0xFFFFFFFF ){
222 #ifdef _WIN32
223  switch (WaitForSingleObject(_event, timeout==0xFFFFFFF?INFINITE:(DWORD)timeout)){
224  case WAIT_FAILED:
225  return EVENT_FAILED;
226  case WAIT_OBJECT_0:
227  return EVENT_OK;
228  case WAIT_TIMEOUT:
229  return EVENT_TIMEOUT;
230  }
231  return EVENT_OK;
232 #else
233  unsigned long ans = EVENT_OK;
234  pthread_mutex_lock( &_cond_locker );
235 
236  if (!_is_signalled){
237  if (timeout == 0xFFFFFFFF){
238  pthread_cond_wait(&_cond_var,&_cond_locker);
239  }else{
240  timespec wait_time;
241  timeval now;
242  gettimeofday(&now,NULL);
243 
244  wait_time.tv_sec = timeout/1000 + now.tv_sec;
245  wait_time.tv_nsec = (timeout%1000)*1000000ULL + now.tv_usec*1000;
246 
247  if (wait_time.tv_nsec >= 1000000000){
248  ++wait_time.tv_sec;
249  wait_time.tv_nsec -= 1000000000;
250  }
251  switch (pthread_cond_timedwait(&_cond_var,&_cond_locker,&wait_time)){
252  case 0:
253  // signalled
254  break;
255  case ETIMEDOUT:
256  // time up
257  ans = EVENT_TIMEOUT;
258  goto _final;
259  break;
260  default:
261  ans = EVENT_FAILED;
262  goto _final;
263  }
264 
265  }
266  }
267 
268  assert(_is_signalled);
269 
270  if (_isAutoReset){
271  _is_signalled = false;
272  }
273 _final:
274  pthread_mutex_unlock( &_cond_locker );
275 
276  return ans;
277 #endif
278 
279  }
280 protected:
281 
282  void release() {
283 #ifdef _WIN32
284  CloseHandle(_event);
285 #else
286  pthread_mutex_destroy(&_cond_locker);
287  pthread_cond_destroy(&_cond_var);
288 #endif
289  }
290 
291 #ifdef _WIN32
292  HANDLE _event;
293 #else
294  pthread_cond_t _cond_var;
295  pthread_mutex_t _cond_locker;
296  bool _is_signalled;
297  bool _isAutoReset;
298 #endif
299 };
300 
302 {
303 public :
304  explicit ScopedLocker(Locker &l): _binded(l) {
305  _binded.lock();
306  }
307 
308  void forceUnlock() {
309  _binded.unlock();
310  }
311  ~ScopedLocker() {
312  _binded.unlock();
313  }
314  Locker & _binded;
315 };
316 
317 
Definition: locker.h:167
Definition: locker.h:301
Definition: locker.h:20