RTAB-Map 0.23.13
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
UMutex.h
1/*
2* utilite is a cross-platform library with
3* useful utilities for fast and small developing.
4* Copyright (C) 2010 Mathieu Labbe
5*
6* utilite is free library: you can redistribute it and/or modify
7* it under the terms of the GNU Lesser General Public License as published by
8* the Free Software Foundation, either version 3 of the License, or
9* (at your option) any later version.
10*
11* utilite is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14* GNU Lesser General Public License for more details.
15*
16* You should have received a copy of the GNU Lesser General Public License
17* along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef UMUTEX_H
21#define UMUTEX_H
22
23#include <errno.h>
24
25#ifdef _WIN32
26 #include "rtabmap/utilite/Win32/UWin32.h"
27#else
28 #include <pthread.h>
29#endif
30
31
54class UMutex
55{
56
57public:
58
63 {
64#ifdef _WIN32
65 InitializeCriticalSection(&C);
66#else
67 pthread_mutexattr_t attr;
68 pthread_mutexattr_init(&attr);
69 pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
70 pthread_mutex_init(&M,&attr);
71 pthread_mutexattr_destroy(&attr);
72#endif
73 }
74
75 virtual ~UMutex()
76 {
77#ifdef _WIN32
78 DeleteCriticalSection(&C);
79#else
80 pthread_mutex_unlock(&M); pthread_mutex_destroy(&M);
81#endif
82 }
83
88 int lock() const
89 {
90#ifdef _WIN32
91 EnterCriticalSection(&C); return 0;
92#else
93 return pthread_mutex_lock(&M);
94#endif
95 }
96
102#ifdef _WIN32
103 #if(_WIN32_WINNT >= 0x0400)
104 int lockTry() const
105 {
106 return (TryEnterCriticalSection(&C)?0:EBUSY);
107 }
108 #endif
109#else
110 int lockTry() const
111 {
112 return pthread_mutex_trylock(&M);
113 }
114#endif
115
120 int unlock() const
121 {
122#ifdef _WIN32
123 LeaveCriticalSection(&C); return 0;
124#else
125 return pthread_mutex_unlock(&M);
126#endif
127 }
128
129 private:
130#ifdef _WIN32
131 mutable CRITICAL_SECTION C;
132#else
133 mutable pthread_mutex_t M;
134#endif
135 void operator=(UMutex &) {}
136 UMutex( const UMutex & ) {}
137};
138
194{
195public:
201 UScopeMutex(const UMutex & mutex, bool lockNow = true) :
202 mutex_(mutex),
203 locked_(false)
204 {
205 if(lockNow)
206 {
207 lock();
208 }
209 }
210 // backward compatibility
211 UScopeMutex(UMutex * mutex) :
212 mutex_(*mutex),
213 locked_(false)
214 {
215 lock();
216 }
221 {
222 unlock();
223 }
224
229 int lock() &
230 {
231 if(locked_)
232 {
233 return 0;
234 }
235 int r = mutex_.lock();
236 locked_ = r == 0;
237 return r;
238 }
239
240#if !defined(_WIN32) || (_WIN32_WINNT >= 0x0400)
246 int lockTry() &
247 {
248 if(locked_)
249 {
250 return 0;
251 }
252 int r = mutex_.lockTry();
253 locked_ = r == 0;
254 return r;
255 }
256#endif
257
262 int unlock() &
263 {
264 if(!locked_)
265 {
266 return 0;
267 }
268 locked_ = false;
269 return mutex_.unlock();
270 }
271
275 bool isLocked() const
276 {
277 return locked_;
278 }
279
280private:
281 UScopeMutex(const UScopeMutex &);
282 void operator=(const UScopeMutex &);
283
284private:
285 const UMutex & mutex_;
286 bool locked_;
287};
288
289#endif // UMUTEX_H
UMutex()
Definition UMutex.h:62
int unlock() const
Definition UMutex.h:120
int lockTry() const
Definition UMutex.h:110
int lock() const
Definition UMutex.h:88
int unlock() &
Definition UMutex.h:262
bool isLocked() const
Definition UMutex.h:275
UScopeMutex(const UMutex &mutex, bool lockNow=true)
Definition UMutex.h:201
~UScopeMutex()
Definition UMutex.h:220
int lock() &
Definition UMutex.h:229
int lockTry() &
Definition UMutex.h:246