Main MRPT website > C++ reference for MRPT 1.5.9
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 - 2016 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(){
49 #ifdef _WIN32
50  _lock = NULL;
51 #endif
52  init();
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  ReleaseMutex(_lock);
121 #else
122  pthread_mutex_unlock(&_lock);
123 #endif
124  }
125 
126 #ifdef _WIN32
127  HANDLE getLockHandle()
128  {
129  return _lock;
130  }
131 #else
132  pthread_mutex_t *getLockHandle()
133  {
134  return &_lock;
135  }
136 #endif
137 
138 
139 
140 protected:
141  void init()
142  {
143 #ifdef _WIN32
144  _lock = CreateMutex(NULL,FALSE,NULL);
145 #else
146  pthread_mutex_init(&_lock, NULL);
147 #endif
148  }
149 
150  void release()
151  {
152  unlock(); //force unlock before release
153 #ifdef _WIN32
154 
155  if (_lock) CloseHandle(_lock);
156  _lock = NULL;
157 #else
158  pthread_mutex_destroy(&_lock);
159 #endif
160  }
161 
162 #ifdef _WIN32
163  HANDLE _lock;
164 #else
165  pthread_mutex_t _lock;
166 #endif
167 };
168 
170 {
171 public :
173  {
174  _binded.lock();
175  }
176 
177  void forceUnlock() {
178  _binded.unlock();
179  }
182 };
183 
184 
185 }}
186 
void unlock()
Definition: locker.h:117
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:70
Locker & _binded
Definition: locker.h:181
HANDLE getLockHandle()
Definition: locker.h:127
#define FALSE
Definition: jmorecfg.h:227
AutoLocker(Locker &l)
Definition: locker.h:172
HANDLE _lock
Definition: locker.h:163
Locker::LOCK_STATUS lock(unsigned long timeout=0xFFFFFFFF)
Definition: locker.h:60
void release()
Definition: locker.h:150
void init()
Definition: locker.h:141
void forceUnlock()
Definition: locker.h:177



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020