locker.h
Go to the documentation of this file.
1 /*
2  * RPLIDAR SDK
3  *
4  * Copyright (c) 2009 - 2014 RoboPeak Team
5  * http://www.robopeak.com
6  * Copyright (c) 2014 - 2020 Shanghai Slamtec Co., Ltd.
7  * http://www.slamtec.com
8  *
9  */
10 /*
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #pragma once
36 namespace rp{ namespace hal{
37 
38 class Locker
39 {
40 public:
42  {
43  LOCK_OK = 1,
46  };
47 
48  Locker(bool recusive = false){
49 #ifdef _WIN32
50  _lock = NULL;
51 #endif
52  init(recusive);
53  }
54 
56  {
57  release();
58  }
59 
60  Locker::LOCK_STATUS lock(unsigned long timeout = 0xFFFFFFFF)
61  {
62 #ifdef _WIN32
63  switch (WaitForSingleObject(_lock, timeout==0xFFFFFFF?INFINITE:(DWORD)timeout))
64  {
65  case WAIT_ABANDONED:
66  return LOCK_FAILED;
67  case WAIT_OBJECT_0:
68  return LOCK_OK;
69  case WAIT_TIMEOUT:
70  return LOCK_TIMEOUT;
71  }
72 
73 #else
74 #ifdef _MACOS
75  if (timeout !=0 ) {
76  if (pthread_mutex_lock(&_lock) == 0) return LOCK_OK;
77  }
78 #else
79  if (timeout == 0xFFFFFFFF){
80  if (pthread_mutex_lock(&_lock) == 0) return LOCK_OK;
81  }
82 #endif
83  else if (timeout == 0)
84  {
85  if (pthread_mutex_trylock(&_lock) == 0) return LOCK_OK;
86  }
87 #ifndef _MACOS
88  else
89  {
90  timespec wait_time;
91  timeval now;
92  gettimeofday(&now,NULL);
93 
94  wait_time.tv_sec = timeout/1000 + now.tv_sec;
95  wait_time.tv_nsec = (timeout%1000)*1000000 + now.tv_usec*1000;
96 
97  if (wait_time.tv_nsec >= 1000000000)
98  {
99  ++wait_time.tv_sec;
100  wait_time.tv_nsec -= 1000000000;
101  }
102  switch (pthread_mutex_timedlock(&_lock,&wait_time))
103  {
104  case 0:
105  return LOCK_OK;
106  case ETIMEDOUT:
107  return LOCK_TIMEOUT;
108  }
109  }
110 #endif
111 #endif
112 
113  return LOCK_FAILED;
114  }
115 
116 
117  void unlock()
118  {
119 #ifdef _WIN32
120  if (_recusive) {
121  ReleaseMutex(_lock);
122  } else {
123  ReleaseSemaphore(_lock, 1, NULL);
124  }
125 #else
126  pthread_mutex_unlock(&_lock);
127 #endif
128  }
129 
130 #ifdef _WIN32
131  HANDLE getLockHandle()
132  {
133  return _lock;
134  }
135 #else
136  pthread_mutex_t *getLockHandle()
137  {
138  return &_lock;
139  }
140 #endif
141 
142 
143 
144 protected:
145  void init(bool recusive)
146  {
147 
148 #ifdef _WIN32
149  if (_recusive = recusive) {
150  _lock = CreateMutex(NULL, FALSE, NULL);
151  } else {
152  _lock = CreateSemaphore(NULL, 1, 1, NULL);
153  }
154 #else
155 
156  if (recusive) {
157  pthread_mutexattr_t attr;
158  pthread_mutexattr_init(&attr);
159  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
160  pthread_mutex_init(&_lock, &attr);
161  } else {
162  pthread_mutex_init(&_lock, NULL);
163  }
164 #endif
165  }
166 
167  void release()
168  {
169  unlock(); //force unlock before release
170 #ifdef _WIN32
171 
172  if (_lock) CloseHandle(_lock);
173  _lock = NULL;
174 #else
175  pthread_mutex_destroy(&_lock);
176 #endif
177  }
178 
179 #ifdef _WIN32
180  HANDLE _lock;
181  bool _recusive;
182 #else
183  pthread_mutex_t _lock;
184 #endif
185 
186 };
187 
189 {
190 public :
192  {
193  _binded.lock();
194  }
195 
196  void forceUnlock() {
197  _binded.unlock();
198  }
201 };
202 
203 
204 }}
205 
rp::hal::Locker::unlock
void unlock()
Definition: locker.h:117
rp::hal::Locker::LOCK_OK
@ LOCK_OK
Definition: locker.h:43
rp::hal::AutoLocker::forceUnlock
void forceUnlock()
Definition: locker.h:196
rp::hal::AutoLocker::AutoLocker
AutoLocker(Locker &l)
Definition: locker.h:191
rp::hal::Locker::LOCK_TIMEOUT
@ LOCK_TIMEOUT
Definition: locker.h:44
rp::hal::Locker::LOCK_FAILED
@ LOCK_FAILED
Definition: locker.h:45
rp::hal::AutoLocker::_binded
Locker & _binded
Definition: locker.h:200
rp::hal::Locker
Definition: locker.h:38
rp::hal::Locker::~Locker
~Locker()
Definition: locker.h:55
rp::hal::AutoLocker
Definition: locker.h:188
rp::hal::Locker::Locker
Locker(bool recusive=false)
Definition: locker.h:48
rp::hal::Locker::lock
Locker::LOCK_STATUS lock(unsigned long timeout=0xFFFFFFFF)
Definition: locker.h:60
rp::hal::Locker::getLockHandle
pthread_mutex_t * getLockHandle()
Definition: locker.h:136
rp::hal::Locker::_lock
pthread_mutex_t _lock
Definition: locker.h:183
rp::hal::Locker::LOCK_STATUS
LOCK_STATUS
Definition: locker.h:41
rp
Definition: rplidar_driver.h:43
rp::hal::AutoLocker::~AutoLocker
~AutoLocker()
Definition: locker.h:199
rp::hal::Locker::release
void release()
Definition: locker.h:167
rp::hal::Locker::init
void init(bool recusive)
Definition: locker.h:145


rplidar_ros
Author(s):
autogenerated on Fri Aug 2 2024 08:42:14