RTAB-Map
0.23.10
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
54
class
UMutex
55
{
56
57
public
:
58
62
UMutex
()
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
164
class
UScopeMutex
165
{
166
public
:
167
UScopeMutex
(
const
UMutex
& mutex) :
168
mutex_(mutex)
169
{
170
mutex_.
lock
();
171
}
172
// backward compatibility
173
UScopeMutex
(
UMutex
* mutex) :
174
mutex_(*mutex)
175
{
176
mutex_.
lock
();
177
}
178
~UScopeMutex
()
179
{
180
mutex_.
unlock
();
181
}
182
private
:
183
const
UMutex
& mutex_;
184
};
185
186
#endif
// UMUTEX_H
UMutex
Definition
UMutex.h:55
UMutex::UMutex
UMutex()
Definition
UMutex.h:62
UMutex::unlock
int unlock() const
Definition
UMutex.h:120
UMutex::lockTry
int lockTry() const
Definition
UMutex.h:110
UMutex::lock
int lock() const
Definition
UMutex.h:88
UScopeMutex
Definition
UMutex.h:165
utilite
include
rtabmap
utilite
UMutex.h
Generated by
1.9.8