RTAB-Map 0.25.2
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
UMutex.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#ifndef UMUTEX_H
29#define UMUTEX_H
30
31#include <errno.h>
32
33#ifdef _WIN32
34 #include "rtabmap/utilite/Win32/UWin32.h"
35#else
36 #include <pthread.h>
37#endif
38
39
62class UMutex
63{
64
65public:
66
71 {
72#ifdef _WIN32
73 InitializeCriticalSection(&C);
74#else
75 pthread_mutexattr_t attr;
76 pthread_mutexattr_init(&attr);
77 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
78 pthread_mutex_init(&M,&attr);
79 pthread_mutexattr_destroy(&attr);
80#endif
81 }
82
83 virtual ~UMutex()
84 {
85#ifdef _WIN32
86 DeleteCriticalSection(&C);
87#else
88 pthread_mutex_unlock(&M); pthread_mutex_destroy(&M);
89#endif
90 }
91
96 int lock() const
97 {
98#ifdef _WIN32
99 EnterCriticalSection(&C); return 0;
100#else
101 return pthread_mutex_lock(&M);
102#endif
103 }
104
110#ifdef _WIN32
111 #if(_WIN32_WINNT >= 0x0400)
112 int lockTry() const
113 {
114 return (TryEnterCriticalSection(&C)?0:EBUSY);
115 }
116 #endif
117#else
118 int lockTry() const
119 {
120 return pthread_mutex_trylock(&M);
121 }
122#endif
123
128 int unlock() const
129 {
130#ifdef _WIN32
131 LeaveCriticalSection(&C); return 0;
132#else
133 return pthread_mutex_unlock(&M);
134#endif
135 }
136
137 private:
138#ifdef _WIN32
139 mutable CRITICAL_SECTION C;
140#else
141 mutable pthread_mutex_t M;
142#endif
143 void operator=(UMutex &) {}
144 UMutex( const UMutex & ) {}
145};
146
202{
203public:
209 UScopeMutex(const UMutex & mutex, bool lockNow = true) :
210 mutex_(mutex),
211 locked_(false)
212 {
213 if(lockNow)
214 {
215 lock();
216 }
217 }
218 // backward compatibility
219 UScopeMutex(UMutex * mutex) :
220 mutex_(*mutex),
221 locked_(false)
222 {
223 lock();
224 }
229 {
230 unlock();
231 }
232
237 int lock() &
238 {
239 if(locked_)
240 {
241 return 0;
242 }
243 int r = mutex_.lock();
244 locked_ = r == 0;
245 return r;
246 }
247
248#if !defined(_WIN32) || (_WIN32_WINNT >= 0x0400)
254 int lockTry() &
255 {
256 if(locked_)
257 {
258 return 0;
259 }
260 int r = mutex_.lockTry();
261 locked_ = r == 0;
262 return r;
263 }
264#endif
265
270 int unlock() &
271 {
272 if(!locked_)
273 {
274 return 0;
275 }
276 locked_ = false;
277 return mutex_.unlock();
278 }
279
283 bool isLocked() const
284 {
285 return locked_;
286 }
287
288private:
289 UScopeMutex(const UScopeMutex &);
290 void operator=(const UScopeMutex &);
291
292private:
293 const UMutex & mutex_;
294 bool locked_;
295};
296
297#endif // UMUTEX_H
UMutex()
Definition UMutex.h:70
int unlock() const
Definition UMutex.h:128
int lockTry() const
Definition UMutex.h:118
int lock() const
Definition UMutex.h:96
int unlock() &
Definition UMutex.h:270
bool isLocked() const
Definition UMutex.h:283
UScopeMutex(const UMutex &mutex, bool lockNow=true)
Definition UMutex.h:209
~UScopeMutex()
Definition UMutex.h:228
int lock() &
Definition UMutex.h:237
int lockTry() &
Definition UMutex.h:254