UMutex.h
Go to the documentation of this file.
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
27 #else
28  #include <pthread.h>
29 #endif
30 
31 
54 class UMutex
55 {
56 
57 public:
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 
87  int lock() const
88  {
89 #ifdef _WIN32
90  EnterCriticalSection(&C); return 0;
91 #else
92  return pthread_mutex_lock(&M);
93 #endif
94  }
95 
96 #ifdef _WIN32
97  #if(_WIN32_WINNT >= 0x0400)
98  int lockTry() const
99  {
100  return (TryEnterCriticalSection(&C)?0:EBUSY);
101  }
102  #endif
103 #else
104  int lockTry() const
105  {
106  return pthread_mutex_trylock(&M);
107  }
108 #endif
109 
113  int unlock() const
114  {
115 #ifdef _WIN32
116  LeaveCriticalSection(&C); return 0;
117 #else
118  return pthread_mutex_unlock(&M);
119 #endif
120  }
121 
122  private:
123 #ifdef _WIN32
124  mutable CRITICAL_SECTION C;
125 #else
126  mutable pthread_mutex_t M;
127 #endif
128  void operator=(UMutex &) {}
129  UMutex( const UMutex & ) {}
130 };
131 
158 {
159 public:
160  UScopeMutex(const UMutex & mutex) :
161  mutex_(mutex)
162  {
163  mutex_.lock();
164  }
165  // backward compatibility
166  UScopeMutex(UMutex * mutex) :
167  mutex_(*mutex)
168  {
169  mutex_.lock();
170  }
172  {
173  mutex_.unlock();
174  }
175 private:
176  const UMutex & mutex_;
177 };
178 
179 #endif // UMUTEX_H
~UScopeMutex()
Definition: UMutex.h:171
int lock() const
Definition: UMutex.h:87
pthread_mutex_t M
Definition: UMutex.h:126
UMutex(const UMutex &)
Definition: UMutex.h:129
const UMutex & mutex_
Definition: UMutex.h:176
virtual ~UMutex()
Definition: UMutex.h:75
Definition: UMutex.h:54
void operator=(UMutex &)
Definition: UMutex.h:128
UMutex()
Definition: UMutex.h:62
int unlock() const
Definition: UMutex.h:113
UScopeMutex(UMutex *mutex)
Definition: UMutex.h:166
int lockTry() const
Definition: UMutex.h:104
UScopeMutex(const UMutex &mutex)
Definition: UMutex.h:160


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:37:06