Main MRPT website > C++ reference for MRPT 1.9.9
locker.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 /*
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright notice,
17  * this list of conditions and the following disclaimer in the documentation
18  * and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #pragma once
35 namespace rp
36 {
37 namespace hal
38 {
39 class Locker
40 {
41  public:
43  {
44  LOCK_OK = 1,
47  };
48 
50  {
51 #ifdef _WIN32
52  _lock = nullptr;
53 #endif
54  init();
55  }
56 
57  ~Locker() { release(); }
58  Locker::LOCK_STATUS lock(unsigned long timeout = 0xFFFFFFFF)
59  {
60 #ifdef _WIN32
61  switch (WaitForSingleObject(
62  _lock, timeout == 0xFFFFFFF ? INFINITE : (DWORD)timeout))
63  {
64  case WAIT_ABANDONED:
65  return LOCK_FAILED;
66  case WAIT_OBJECT_0:
67  return LOCK_OK;
68  case WAIT_TIMEOUT:
69  return LOCK_TIMEOUT;
70  }
71 
72 #else
73 #ifdef _MACOS
74  if (timeout != 0)
75  {
76  if (pthread_mutex_lock(&_lock) == 0) return LOCK_OK;
77  }
78 #else
79  if (timeout == 0xFFFFFFFF)
80  {
81  if (pthread_mutex_lock(&_lock) == 0) return LOCK_OK;
82  }
83 #endif
84  else if (timeout == 0)
85  {
86  if (pthread_mutex_trylock(&_lock) == 0) return LOCK_OK;
87  }
88 #ifndef _MACOS
89  else
90  {
91  timespec wait_time;
92  timeval now;
93  gettimeofday(&now, nullptr);
94 
95  wait_time.tv_sec = timeout / 1000 + now.tv_sec;
96  wait_time.tv_nsec = (timeout % 1000) * 1000000 + now.tv_usec * 1000;
97 
98  if (wait_time.tv_nsec >= 1000000000)
99  {
100  ++wait_time.tv_sec;
101  wait_time.tv_nsec -= 1000000000;
102  }
103  switch (pthread_mutex_timedlock(&_lock, &wait_time))
104  {
105  case 0:
106  return LOCK_OK;
107  case ETIMEDOUT:
108  return LOCK_TIMEOUT;
109  }
110  }
111 #endif
112 #endif
113 
114  return LOCK_FAILED;
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() { return _lock; }
128 #else
129  pthread_mutex_t* getLockHandle() { return &_lock; }
130 #endif
131 
132  protected:
133  void init()
134  {
135 #ifdef _WIN32
136  _lock = CreateMutex(nullptr, FALSE, nullptr);
137 #else
138  pthread_mutex_init(&_lock, nullptr);
139 #endif
140  }
141 
142  void release()
143  {
144  unlock(); // force unlock before release
145 #ifdef _WIN32
146 
147  if (_lock) CloseHandle(_lock);
148  _lock = nullptr;
149 #else
150  pthread_mutex_destroy(&_lock);
151 #endif
152  }
153 
154 #ifdef _WIN32
155  HANDLE _lock;
156 #else
157  pthread_mutex_t _lock;
158 #endif
159 };
160 
162 {
163  public:
165  void forceUnlock() { _binded.unlock(); }
168 };
169 }
170 }
rp::hal::AutoLocker
Definition: locker.h:161
rp::hal::Locker::Locker
Locker()
Definition: locker.h:49
rp::hal::Locker::getLockHandle
HANDLE getLockHandle()
Definition: locker.h:127
rp::hal::Locker::LOCK_OK
@ LOCK_OK
Definition: locker.h:44
mrpt::system::now
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:75
rp::hal::AutoLocker::forceUnlock
void forceUnlock()
Definition: locker.h:165
rp::hal::Locker::release
void release()
Definition: locker.h:142
rp::hal::Locker::unlock
void unlock()
Definition: locker.h:117
rp::hal::Locker::lock
Locker::LOCK_STATUS lock(unsigned long timeout=0xFFFFFFFF)
Definition: locker.h:58
rp::hal::Locker::LOCK_TIMEOUT
@ LOCK_TIMEOUT
Definition: locker.h:45
rp::hal::AutoLocker::_binded
Locker & _binded
Definition: locker.h:167
rp::hal::Locker
Definition: locker.h:39
rp::hal::Locker::_lock
HANDLE _lock
Definition: locker.h:155
rp::hal::Locker::LOCK_STATUS
LOCK_STATUS
Definition: locker.h:42
FALSE
#define FALSE
Definition: xmlParser.h:231
rp::hal::AutoLocker::AutoLocker
AutoLocker(Locker &l)
Definition: locker.h:164
rp::hal::Locker::init
void init()
Definition: locker.h:133
rp::hal::Locker::~Locker
~Locker()
Definition: locker.h:57
rp
Definition: rplidar_driver.h:40
rp::hal::AutoLocker::~AutoLocker
~AutoLocker()
Definition: locker.h:166
rp::hal::Locker::LOCK_FAILED
@ LOCK_FAILED
Definition: locker.h:46



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST