Debian Codegraph @

3rdparty/rplidar_sdk/sdk/src/hal/event.h

raw symbols SCIP
1 /*
2 * RPLIDAR SDK
3 *
4 * Copyright (c) 2009 - 2014 RoboPeak Team
5 * http://www.robopeak.com
6 * Copyright (c) 2014 - 2020 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,
45 EVENT_TIMEOUT = 0xFFFFFFFF,
46 EVENT_FAILED = 0,
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_condattr_init(&_cond_attr);
62 #ifdef _MACOS
63 // sadly, there is no monotonic clock support for pthread cond variable on MACOS
64 // if time slew is a big issue, try to reimplement it using kqueue/kevent
65 #else
66 pthread_condattr_setclock(&_cond_attr, CLOCK_MONOTONIC);
67 #endif
68 pthread_cond_init(&_cond_var, &_cond_attr);
69
70 #endif
71 }
72
73 ~ Event()
74 {
75 release();
76 }
77
78 void set( bool isSignal = true )
79 {
80 if (isSignal){
81 #ifdef _WIN32
82 SetEvent(_event);
83 #else
84 pthread_mutex_lock(&_cond_locker);
85
86 if ( _is_signalled == false )
87 {
88 _is_signalled = true;
89 pthread_cond_signal(&_cond_var);
90 }
91 pthread_mutex_unlock(&_cond_locker);
92 #endif
93 }
94 else
95 {
96 #ifdef _WIN32
97 ResetEvent(_event);
98 #else
99 pthread_mutex_lock(&_cond_locker);
100 _is_signalled = false;
101 pthread_mutex_unlock(&_cond_locker);
102 #endif
103 }
104 }
105
106 unsigned long wait( unsigned long timeout = 0xFFFFFFFF )
107 {
108 #ifdef _WIN32
109 switch (WaitForSingleObject(_event, timeout==0xFFFFFFF?INFINITE:(DWORD)timeout))
110 {
111 case WAIT_FAILED:
112 return EVENT_FAILED;
113 case WAIT_OBJECT_0:
114 return EVENT_OK;
115 case WAIT_TIMEOUT:
116 return EVENT_TIMEOUT;
117 }
118 return EVENT_OK;
119 #else
120 unsigned long ans = EVENT_OK;
121 pthread_mutex_lock( &_cond_locker );
122
123 if ( !_is_signalled )
124 {
125
126 if (timeout == 0xFFFFFFFF){
127 pthread_cond_wait(&_cond_var,&_cond_locker);
128 }else
129 {
130 int timewaitresult = 0;
131 #ifdef _MACOS
132 timespec wait_time;
133
134 wait_time.tv_sec = timeout / 1000;
135 wait_time.tv_nsec = (timeout%1000)*1000000ULL;
136
137 timewaitresult = pthread_cond_timedwait_relative_np(&_cond_var,&_cond_locker,&wait_time);
138 #else
139 timespec wait_time;
140 clock_gettime(CLOCK_MONOTONIC, &wait_time);
141
142 wait_time.tv_sec += timeout / 1000;
143 wait_time.tv_nsec += (timeout%1000)*1000000ULL;
144
145 if (wait_time.tv_nsec >= 1000000000)
146 {
147 ++wait_time.tv_sec;
148 wait_time.tv_nsec -= 1000000000;
149 }
150 timewaitresult = pthread_cond_timedwait(&_cond_var,&_cond_locker,&wait_time);
151 #endif
152
153 switch (timewaitresult)
154 {
155 case 0:
156 // signalled
157 break;
158 case ETIMEDOUT:
159 // time up
160 ans = EVENT_TIMEOUT;
161 goto _final;
162 break;
163 default:
164 ans = EVENT_FAILED;
165 goto _final;
166 }
167
168 }
169 }
170
171 //assert(_is_signalled);
172
173 if ( _isAutoReset )
174 {
175 _is_signalled = false;
176 }
177 _final:
178 pthread_mutex_unlock( &_cond_locker );
179
180 return ans;
181 #endif
182
183 }
184 protected:
185
186 void release()
187 {
188 #ifdef _WIN32
189 CloseHandle(_event);
190 #else
191 pthread_mutex_destroy(&_cond_locker);
192 pthread_cond_destroy(&_cond_var);
193 #endif
194 }
195
196 #ifdef _WIN32
197 HANDLE _event;
198 #else
199 pthread_cond_t _cond_var;
200 pthread_mutex_t _cond_locker;
201 pthread_condattr_t _cond_attr;
202 bool _is_signalled;
203 bool _isAutoReset;
204 #endif
205 };
206 }}