USemaphore.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 /*
21  * Originally written by Phillip Sitbon
22  * Copyright 2003
23  */
24 
25 #ifndef USEMAPHORE_H
26 #define USEMAPHORE_H
27 
28 #include <errno.h>
29 
30 #ifdef _WIN32
32 #define SEM_VALUE_MAX ((int) ((~0u) >> 1))
33 #else
34 #include <pthread.h>
35 #include <sys/time.h>
36 #endif
37 
55 {
56 public:
61  USemaphore( int initValue = 0 )
62  {
63 #ifdef _WIN32
64  S = CreateSemaphore(0,initValue,SEM_VALUE_MAX,0);
65 #else
66  _available = initValue;
67  pthread_mutex_init(&_waitMutex, NULL);
68  pthread_cond_init(&_cond, NULL);
69 #endif
70  }
71 
72  virtual ~USemaphore()
73  {
74 #ifdef _WIN32
75  CloseHandle(S);
76 #else
77  pthread_cond_destroy(&_cond);
78  pthread_mutex_destroy(&_waitMutex);
79 #endif
80  }
81 
90 #ifdef _WIN32
91  bool acquire(int n = 1, int ms = 0) const
92  {
93  int rt = 0;
94  while(n-- > 0 && rt==0)
95  {
96  rt = WaitForSingleObject((HANDLE)S, ms<=0?INFINITE:ms);
97  }
98  return rt == 0;
99  }
100 #else
101  bool acquire(int n = 1, int ms = 0)
102  {
103  int rt = 0;
104  pthread_mutex_lock(&_waitMutex);
105  while (n > _available && rt == 0)
106  {
107  if(ms > 0)
108  {
109  struct timespec timeToWait;
110  struct timeval now;
111 
112  gettimeofday(&now,NULL);
113 
114  timeToWait.tv_sec = now.tv_sec + ms/1000;
115  timeToWait.tv_nsec = (now.tv_usec+1000UL*(ms%1000))*1000UL;
116 
117  rt = pthread_cond_timedwait(&_cond, &_waitMutex, &timeToWait);
118  }
119  else
120  {
121  rt = pthread_cond_wait(&_cond, &_waitMutex);
122  }
123  }
124  if(rt == 0)
125  {
126  // only remove them if waiting did not fail
127  _available -= n;
128  }
129  pthread_mutex_unlock(&_waitMutex);
130  return rt == 0;
131  }
132 #endif
133 
134  /*
135  * Try to acquire the semaphore, not a blocking call.
136  * @return false if the semaphore can't be taken without waiting (value <= 0), true otherwise
137  */
138 #ifdef _WIN32
139  int acquireTry() const
140  {
141  return ((WaitForSingleObject((HANDLE)S,INFINITE)==WAIT_OBJECT_0)?0:EAGAIN);
142  }
143 #else
144  int acquireTry(int n)
145  {
146  pthread_mutex_lock(&_waitMutex);
147  if(n > _available)
148  {
149  pthread_mutex_unlock(&_waitMutex);
150  return false;
151  }
152  _available -= n;
153  pthread_mutex_unlock(&_waitMutex);
154  return true;
155  }
156 #endif
157 
162 #ifdef _WIN32
163  int release(int n = 1) const
164  {
165  return (ReleaseSemaphore((HANDLE)S,n,0)?0:ERANGE);
166  }
167 #else
168  void release(int n = 1)
169  {
170  pthread_mutex_lock(&_waitMutex);
171  _available += n;
172  pthread_cond_broadcast(&_cond);
173  pthread_mutex_unlock(&_waitMutex);
174  }
175 #endif
176 
181 #ifdef _WIN32
182  int value() const
183  {
184  LONG V = -1; ReleaseSemaphore((HANDLE)S,0,&V); return V;
185  }
186 #else
187  int value()
188  {
189  int value = 0;
190  pthread_mutex_lock(&_waitMutex);
191  value = _available;
192  pthread_mutex_unlock(&_waitMutex);
193  return value;
194  }
195 #endif
196 
197 #ifdef _WIN32
198  /*
199  * Reset the semaphore count.
200  * @param init the initial value
201  * TODO implement on posix ?
202  */
203  void reset( int init = 0 )
204  {
205  CloseHandle(S);
206  S = CreateSemaphore(0,init,SEM_VALUE_MAX,0);
207  }
208 #endif
209 
210 private:
211  void operator=(const USemaphore &S){}
212 #ifdef _WIN32
213  USemaphore(const USemaphore &S){}
214  HANDLE S;
215 #else
217  pthread_mutex_t _waitMutex;
218  pthread_cond_t _cond;
220 #endif
221 };
222 
223 #endif // USEMAPHORE_H
#define NULL
USemaphore(int initValue=0)
Definition: USemaphore.h:61
void release(int n=1)
Definition: USemaphore.h:168
bool acquire(int n=1, int ms=0)
Definition: USemaphore.h:101
USemaphore(const USemaphore &S)
Definition: USemaphore.h:216
void operator=(const USemaphore &S)
Definition: USemaphore.h:211
int value()
Definition: USemaphore.h:187
int acquireTry(int n)
Definition: USemaphore.h:144
virtual ~USemaphore()
Definition: USemaphore.h:72
pthread_cond_t _cond
Definition: USemaphore.h:218
pthread_mutex_t _waitMutex
Definition: USemaphore.h:217
int _available
Definition: USemaphore.h:219


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:43:40