Main MRPT website > C++ reference for MRPT 1.5.6
event.h
Go to the documentation of this file.
1 /*
2  * RPLIDAR SDK
3  *
4  * Copyright (c) 2009 - 2014 RoboPeak Team
5  * http://www.robopeak.com
6  * Copyright (c) 2014 - 2016 Shanghai Slamtec Co., Ltd.
7  * http://www.slamtec.com
8  *
9  */
10 /*
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #pragma once
36 namespace rp{ namespace hal{
37 
38 class Event
39 {
40 public:
41 
42  enum
43  {
44  EVENT_OK = 1,
47  };
48 
49  Event(bool isAutoReset = true, bool isSignal = false)
50 #ifdef _WIN32
51  : _event(NULL)
52 #else
53  : _is_signalled(isSignal)
54  , _isAutoReset(isAutoReset)
55 #endif
56  {
57 #ifdef _WIN32
58  _event = CreateEvent(NULL, isAutoReset?FALSE:TRUE, isSignal?TRUE:FALSE, NULL);
59 #else
60  pthread_mutex_init(&_cond_locker, NULL);
61  pthread_cond_init(&_cond_var, NULL);
62 #endif
63  }
64 
66  {
67  release();
68  }
69 
70  void set( bool isSignal = true )
71  {
72  if (isSignal){
73 #ifdef _WIN32
74  SetEvent(_event);
75 #else
76  pthread_mutex_lock(&_cond_locker);
77 
78  if ( _is_signalled == false )
79  {
80  _is_signalled = true;
81  pthread_cond_signal(&_cond_var);
82  }
83  pthread_mutex_unlock(&_cond_locker);
84 #endif
85  }
86  else
87  {
88 #ifdef _WIN32
89  ResetEvent(_event);
90 #else
91  pthread_mutex_lock(&_cond_locker);
92  _is_signalled = false;
93  pthread_mutex_unlock(&_cond_locker);
94 #endif
95  }
96  }
97 
98  unsigned long wait( unsigned long timeout = 0xFFFFFFFF )
99  {
100 #ifdef _WIN32
101  switch (WaitForSingleObject(_event, timeout==0xFFFFFFF?INFINITE:(DWORD)timeout))
102  {
103  case WAIT_FAILED:
104  return EVENT_FAILED;
105  case WAIT_OBJECT_0:
106  return EVENT_OK;
107  case WAIT_TIMEOUT:
108  return EVENT_TIMEOUT;
109  }
110  return EVENT_OK;
111 #else
112  unsigned long ans = EVENT_OK;
113  pthread_mutex_lock( &_cond_locker );
114 
115  if ( !_is_signalled )
116  {
117 
118  if (timeout == 0xFFFFFFFF){
119  pthread_cond_wait(&_cond_var,&_cond_locker);
120  }else
121  {
122  timespec wait_time;
123  timeval now;
124  gettimeofday(&now,NULL);
125 
126  wait_time.tv_sec = timeout/1000 + now.tv_sec;
127  wait_time.tv_nsec = (timeout%1000)*1000000ULL + now.tv_usec*1000;
128 
129  if (wait_time.tv_nsec >= 1000000000)
130  {
131  ++wait_time.tv_sec;
132  wait_time.tv_nsec -= 1000000000;
133  }
134  switch (pthread_cond_timedwait(&_cond_var,&_cond_locker,&wait_time))
135  {
136  case 0:
137  // signalled
138  break;
139  case ETIMEDOUT:
140  // time up
141  ans = EVENT_TIMEOUT;
142  goto _final;
143  break;
144  default:
145  ans = EVENT_FAILED;
146  goto _final;
147  }
148 
149  }
150  }
151 
152  assert(_is_signalled);
153 
154  if ( _isAutoReset )
155  {
156  _is_signalled = false;
157  }
158 _final:
159  pthread_mutex_unlock( &_cond_locker );
160 
161  return ans;
162 #endif
163 
164  }
165 protected:
166 
167  void release()
168  {
169 #ifdef _WIN32
170  CloseHandle(_event);
171 #else
172  pthread_mutex_destroy(&_cond_locker);
173  pthread_cond_destroy(&_cond_var);
174 #endif
175  }
176 
177 #ifdef _WIN32
178  HANDLE _event;
179 #else
180  pthread_cond_t _cond_var;
181  pthread_mutex_t _cond_locker;
182  bool _is_signalled;
183  bool _isAutoReset;
184 #endif
185 };
186 }}
Event(bool isAutoReset=true, bool isSignal=false)
Definition: event.h:49
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:70
#define FALSE
Definition: jmorecfg.h:227
void release()
Definition: event.h:167
#define TRUE
Definition: jmorecfg.h:230
unsigned long wait(unsigned long timeout=0xFFFFFFFF)
Definition: event.h:98
HANDLE _event
Definition: event.h:178



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019