EtcPal  HEAD (unstable)
ETC Platform Abstraction Layer (EtcPal)
View other versions:
recursive_mutex.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2022 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
22 
23 #ifndef ETCPAL_CPP_RECURSIVE_MUTEX_H_
24 #define ETCPAL_CPP_RECURSIVE_MUTEX_H_
25 
26 #include <stdexcept>
27 #include "etcpal/cpp/common.h"
28 #include "etcpal/recursive_mutex.h"
29 
30 namespace etcpal
31 {
48 
64 {
65 public:
68 
69  RecursiveMutex(const RecursiveMutex& other) = delete;
70  RecursiveMutex& operator=(const RecursiveMutex& other) = delete;
71  RecursiveMutex(RecursiveMutex&& other) = delete;
72  RecursiveMutex& operator=(RecursiveMutex&& other) = delete;
73 
74  bool Lock();
75  bool TryLock(int timeout_ms = 0);
76  void Unlock();
77 
79 
80 private:
81  etcpal_recursive_mutex_t mutex_{};
82 };
83 
86 {
87  (void)etcpal_recursive_mutex_create(&mutex_);
88 }
89 
92 {
94 }
95 
98 inline bool RecursiveMutex::Lock()
99 {
100  return etcpal_recursive_mutex_lock(&mutex_);
101 }
102 
114 inline bool RecursiveMutex::TryLock(int timeout_ms)
115 {
116  return etcpal_recursive_mutex_timed_lock(&mutex_, timeout_ms);
117 }
118 
123 {
125 }
126 
129 {
130  return mutex_;
131 }
132 
149 {
150 public:
151  explicit RecursiveMutexGuard(RecursiveMutex& mutex);
154 
155  RecursiveMutexGuard(const RecursiveMutexGuard& other) = delete;
156  RecursiveMutexGuard& operator=(const RecursiveMutexGuard& other) = delete;
157  RecursiveMutexGuard(RecursiveMutexGuard&& other) = delete;
158  RecursiveMutexGuard& operator=(RecursiveMutexGuard&& other) = delete;
159 
160 private:
161  etcpal_recursive_mutex_t& mutex_;
162 
163  void GetLock();
164 };
165 
168 inline RecursiveMutexGuard::RecursiveMutexGuard(RecursiveMutex& mutex) : mutex_(mutex.get())
169 {
170  GetLock();
171 }
172 
176 {
177  GetLock();
178 }
179 
182 {
184 }
185 
186 inline void RecursiveMutexGuard::GetLock()
187 {
188  if (!etcpal_recursive_mutex_lock(&mutex_))
189  ETCPAL_THROW(std::runtime_error("etcpal_recursive_mutex_lock failed."));
190 }
191 
192 }; // namespace etcpal
193 
194 #endif // ETCPAL_CPP_RECURSIVE_MUTEX_H_
Lock guard around a recursive mutex.
Definition: recursive_mutex.h:149
~RecursiveMutexGuard()
Release the lock upon going out-of-scope.
Definition: recursive_mutex.h:181
RecursiveMutexGuard(RecursiveMutex &mutex)
Lock an etcpal::RecursiveMutex.
Definition: recursive_mutex.h:168
A wrapper class for the EtcPal recursive mutex type.
Definition: recursive_mutex.h:64
bool Lock()
Lock the mutex.
Definition: recursive_mutex.h:98
RecursiveMutex()
Create a new recursive mutex.
Definition: recursive_mutex.h:85
bool TryLock(int timeout_ms=0)
Attempt to lock the recursive mutex.
Definition: recursive_mutex.h:114
void Unlock()
Unlock the recursive mutex.
Definition: recursive_mutex.h:122
etcpal_recursive_mutex_t & get()
Get a reference to the underlying etcpal_recursive_mutex_t type.
Definition: recursive_mutex.h:128
~RecursiveMutex()
Destroy the recursive mutex.
Definition: recursive_mutex.h:91
Common definitions used by EtcPal C++ wrappers.
void etcpal_recursive_mutex_destroy(etcpal_recursive_mutex_t *id)
Destroy a mutex object.
bool etcpal_recursive_mutex_lock(etcpal_recursive_mutex_t *id)
Lock a mutex.
bool etcpal_recursive_mutex_create(etcpal_recursive_mutex_t *id)
Create a new mutex.
void etcpal_recursive_mutex_unlock(etcpal_recursive_mutex_t *id)
Unlock a mutex.
PLATFORM_DEFINED etcpal_recursive_mutex_t
The mutex identifier.
Definition: recursive_mutex.dox:51
bool etcpal_recursive_mutex_timed_lock(etcpal_recursive_mutex_t *id, int timeout_ms)
Try to lock a mutex, giving up after a timeout.