MRPT  1.9.9
locker.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://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::hal
36 {
37 class Locker
38 {
39  public:
41  {
42  LOCK_OK = 1,
45  };
46 
48  {
49 #ifdef _WIN32
50  _lock = nullptr;
51 #endif
52  init();
53  }
54 
55  ~Locker() { release(); }
56  Locker::LOCK_STATUS lock(unsigned long timeout = 0xFFFFFFFF)
57  {
58 #ifdef _WIN32
59  switch (WaitForSingleObject(
60  _lock, timeout == 0xFFFFFFF ? INFINITE : (DWORD)timeout))
61  {
62  case WAIT_ABANDONED:
63  return LOCK_FAILED;
64  case WAIT_OBJECT_0:
65  return LOCK_OK;
66  case WAIT_TIMEOUT:
67  return LOCK_TIMEOUT;
68  }
69 
70 #else
71 #ifdef _MACOS
72  if (timeout != 0)
73  {
74  if (pthread_mutex_lock(&_lock) == 0) return LOCK_OK;
75  }
76 #else
77  if (timeout == 0xFFFFFFFF)
78  {
79  if (pthread_mutex_lock(&_lock) == 0) return LOCK_OK;
80  }
81 #endif
82  else if (timeout == 0)
83  {
84  if (pthread_mutex_trylock(&_lock) == 0) return LOCK_OK;
85  }
86 #ifndef _MACOS
87  else
88  {
89  timespec wait_time;
90  timeval now;
91  gettimeofday(&now, nullptr);
92 
93  wait_time.tv_sec = timeout / 1000 + now.tv_sec;
94  wait_time.tv_nsec = (timeout % 1000) * 1000000 + now.tv_usec * 1000;
95 
96  if (wait_time.tv_nsec >= 1000000000)
97  {
98  ++wait_time.tv_sec;
99  wait_time.tv_nsec -= 1000000000;
100  }
101  switch (pthread_mutex_timedlock(&_lock, &wait_time))
102  {
103  case 0:
104  return LOCK_OK;
105  case ETIMEDOUT:
106  return LOCK_TIMEOUT;
107  }
108  }
109 #endif
110 #endif
111 
112  return LOCK_FAILED;
113  }
114 
115  void unlock()
116  {
117 #ifdef _WIN32
118  ReleaseMutex(_lock);
119 #else
120  pthread_mutex_unlock(&_lock);
121 #endif
122  }
123 
124 #ifdef _WIN32
125  HANDLE getLockHandle() { return _lock; }
126 #else
127  pthread_mutex_t* getLockHandle() { return &_lock; }
128 #endif
129 
130  protected:
131  void init()
132  {
133 #ifdef _WIN32
134  _lock = CreateMutex(nullptr, FALSE, nullptr);
135 #else
136  pthread_mutex_init(&_lock, nullptr);
137 #endif
138  }
139 
140  void release()
141  {
142  unlock(); // force unlock before release
143 #ifdef _WIN32
144 
145  if (_lock) CloseHandle(_lock);
146  _lock = nullptr;
147 #else
148  pthread_mutex_destroy(&_lock);
149 #endif
150  }
151 
152 #ifdef _WIN32
153  HANDLE _lock;
154 #else
155  pthread_mutex_t _lock;
156 #endif
157 };
158 
160 {
161  public:
163  void forceUnlock() { _binded.unlock(); }
166 };
167 } // namespace rp::hal
void unlock()
Definition: locker.h:115
mrpt::system::TTimeStamp now()
A shortcut for system::getCurrentTime.
Definition: datetime.h:86
Locker & _binded
Definition: locker.h:165
HANDLE getLockHandle()
Definition: locker.h:125
AutoLocker(Locker &l)
Definition: locker.h:162
HANDLE _lock
Definition: locker.h:153
Locker::LOCK_STATUS lock(unsigned long timeout=0xFFFFFFFF)
Definition: locker.h:56
#define FALSE
Definition: xmlParser.h:230
void release()
Definition: locker.h:140
void init()
Definition: locker.h:131
void forceUnlock()
Definition: locker.h:163



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019