RTAB-Map 0.25.2
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
USemaphore.h
1/*
2Copyright (c) 2010, Mathieu Labbe
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holders nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28/*
29 * Originally written by Phillip Sitbon
30 * Copyright 2003
31 */
32
33#ifndef USEMAPHORE_H
34#define USEMAPHORE_H
35
36#include <errno.h>
37
38#ifdef _WIN32
39#include "rtabmap/utilite/Win32/UWin32.h"
40#include <atomic>
41#define SEM_VALUE_MAX ((int) ((~0u) >> 1))
42#else
43#include <pthread.h>
44#include <sys/time.h>
45#endif
46
64{
65public:
70 USemaphore( int initValue = 0 )
71#ifdef _WIN32
72 : _count(initValue)
73#endif
74 {
75#ifdef _WIN32
76 S = CreateSemaphore(0,initValue,SEM_VALUE_MAX,0);
77#else
78 _available = initValue;
79 pthread_mutex_init(&_waitMutex, NULL);
80 pthread_cond_init(&_cond, NULL);
81#endif
82 }
83
84 virtual ~USemaphore()
85 {
86#ifdef _WIN32
87 CloseHandle(S);
88#else
89 pthread_cond_destroy(&_cond);
90 pthread_mutex_destroy(&_waitMutex);
91#endif
92 }
93
102#ifdef _WIN32
103 bool acquire(int n = 1, int ms = 0) const
104 {
105 int rt = 0;
106 while(n-- > 0 && rt==0)
107 {
108 rt = WaitForSingleObject((HANDLE)S, ms<=0?INFINITE:ms);
109 if(rt == 0) {
110 --_count;
111 }
112 }
113 return rt == 0;
114 }
115#else
116 bool acquire(int n = 1, int ms = 0)
117 {
118 int rt = 0;
119 pthread_mutex_lock(&_waitMutex);
120 while (n > _available && rt == 0)
121 {
122 if(ms > 0)
123 {
124 struct timespec timeToWait;
125 struct timeval now;
126
127 gettimeofday(&now,NULL);
128
129 timeToWait.tv_sec = now.tv_sec + ms/1000;
130 timeToWait.tv_nsec = (now.tv_usec+1000UL*(ms%1000))*1000UL;
131
132 rt = pthread_cond_timedwait(&_cond, &_waitMutex, &timeToWait);
133 }
134 else
135 {
136 rt = pthread_cond_wait(&_cond, &_waitMutex);
137 }
138 }
139 if(rt == 0)
140 {
141 // only remove them if waiting did not fail
142 _available -= n;
143 }
144 pthread_mutex_unlock(&_waitMutex);
145 return rt == 0;
146 }
147#endif
148
149 /*
150 * Try to acquire the semaphore, not a blocking call.
151 * @return 0 if the semaphore was acquired, EAGAIN if it couldn't be taken
152 * without waiting.
153 * @note The Windows overload takes no argument (it always acquires 1).
154 */
155#ifdef _WIN32
156 int acquireTry() const
157 {
158 // Non-blocking try-acquire: timeout 0, not INFINITE. INFINITE here
159 // turned this into a blocking acquire and hung tests when the count
160 // reached 0.
161 if(WaitForSingleObject((HANDLE)S, 0) == WAIT_OBJECT_0)
162 {
163 --_count;
164 return 0;
165 }
166 return EAGAIN;
167 }
168#else
172 int acquireTry(int n)
173 {
174 pthread_mutex_lock(&_waitMutex);
175 if(n > _available)
176 {
177 pthread_mutex_unlock(&_waitMutex);
178 return EAGAIN;
179 }
180 _available -= n;
181 pthread_mutex_unlock(&_waitMutex);
182 return 0;
183 }
184#endif
185
194#ifdef _WIN32
195 int release(int n = 1) const
196 {
197 if(ReleaseSemaphore((HANDLE)S, n, 0))
198 {
199 _count += n;
200 return 0;
201 }
202 return ERANGE;
203 }
204#else
205 int release(int n = 1)
206 {
207 pthread_mutex_lock(&_waitMutex);
208 _available += n;
209 pthread_cond_broadcast(&_cond);
210 pthread_mutex_unlock(&_waitMutex);
211 return 0;
212 }
213#endif
214
219#ifdef _WIN32
220 int value() const
221 {
222 // ReleaseSemaphore(S, 0, &V) returns FALSE on Windows (release count
223 // must be >= 1) and never writes V, so we mirror the count in an atomic
224 // instead. The value is eventually-consistent under concurrent
225 // acquire/release but accurate at quiescence.
226 return (int)_count.load();
227 }
228#else
229 int value()
230 {
231 int value = 0;
232 pthread_mutex_lock(&_waitMutex);
233 value = _available;
234 pthread_mutex_unlock(&_waitMutex);
235 return value;
236 }
237#endif
238
239#ifdef _WIN32
240 /*
241 * Reset the semaphore count.
242 * @param init the initial value
243 * TODO implement on posix ?
244 */
245 void reset( int init = 0 )
246 {
247 CloseHandle(S);
248 S = CreateSemaphore(0,init,SEM_VALUE_MAX,0);
249 _count = init;
250 }
251#endif
252
253private:
254 void operator=(const USemaphore &){}
255#ifdef _WIN32
256 USemaphore(const USemaphore &S){}
257 HANDLE S;
258 mutable std::atomic<long> _count;
259#else
260 USemaphore(const USemaphore &):_available(0){}
261 pthread_mutex_t _waitMutex;
262 pthread_cond_t _cond;
263 int _available;
264#endif
265};
266
267#endif // USEMAPHORE_H
bool acquire(int n=1, int ms=0)
Definition USemaphore.h:116
int acquireTry(int n)
Definition USemaphore.h:172
int release(int n=1)
Definition USemaphore.h:205
int value()
Definition USemaphore.h:229
USemaphore(int initValue=0)
Definition USemaphore.h:70