EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
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_MUTEX_H_
24 #define ETCPAL_CPP_MUTEX_H_
25 
26 #include <stdexcept>
27 #include "etcpal/cpp/common.h"
28 #include "etcpal/mutex.h"
29 
30 namespace etcpal
31 {
43 
87 class Mutex
88 {
89 public:
90  Mutex();
91  ~Mutex();
92 
93  Mutex(const Mutex& other) = delete;
94  Mutex& operator=(const Mutex& other) = delete;
95  Mutex(Mutex&& other) = delete;
96  Mutex& operator=(Mutex&& other) = delete;
97 
98  bool Lock();
99  bool TryLock(int timeout_ms = 0);
100  void Unlock();
101 
102  etcpal_mutex_t& get();
103 
104 private:
105  etcpal_mutex_t mutex_{};
106 };
107 
109 inline Mutex::Mutex()
110 {
111  (void)etcpal_mutex_create(&mutex_);
112 }
113 
116 {
117  etcpal_mutex_destroy(&mutex_);
118 }
119 
122 inline bool Mutex::Lock()
123 {
124  return etcpal_mutex_lock(&mutex_);
125 }
126 
138 inline bool Mutex::TryLock(int timeout_ms)
139 {
140  return etcpal_mutex_timed_lock(&mutex_, timeout_ms);
141 }
142 
146 inline void Mutex::Unlock()
147 {
148  etcpal_mutex_unlock(&mutex_);
149 }
150 
153 {
154  return mutex_;
155 }
156 
173 {
174 public:
175  explicit MutexGuard(Mutex& mutex);
176  explicit MutexGuard(etcpal_mutex_t& mutex);
177  ~MutexGuard();
178 
179  MutexGuard(const MutexGuard& other) = delete;
180  MutexGuard& operator=(const MutexGuard& other) = delete;
181  MutexGuard(MutexGuard&& other) = delete;
182  MutexGuard& operator=(MutexGuard&& other) = delete;
183 
184 private:
185  etcpal_mutex_t& mutex_;
186 
187  void GetLock();
188 };
189 
192 inline MutexGuard::MutexGuard(Mutex& mutex) : mutex_(mutex.get())
193 {
194  GetLock();
195 }
196 
199 inline MutexGuard::MutexGuard(etcpal_mutex_t& mutex) : mutex_(mutex)
200 {
201  GetLock();
202 }
203 
206 {
207  etcpal_mutex_unlock(&mutex_);
208 }
209 
210 inline void MutexGuard::GetLock()
211 {
212  if (!etcpal_mutex_lock(&mutex_))
213  ETCPAL_THROW(std::runtime_error("etcpal_mutex_lock failed."));
214 }
215 
216 }; // namespace etcpal
217 
218 #endif // ETCPAL_CPP_MUTEX_H_
Lock guard around a mutex.
Definition: mutex.h:173
~MutexGuard()
Release the lock upon going out-of-scope.
Definition: mutex.h:205
MutexGuard(Mutex &mutex)
Lock an etcpal::Mutex.
Definition: mutex.h:192
A wrapper class for the EtcPal mutex type.
Definition: mutex.h:88
~Mutex()
Destroy the mutex.
Definition: mutex.h:115
Mutex()
Create a new mutex.
Definition: mutex.h:109
bool Lock()
Lock the mutex.
Definition: mutex.h:122
bool TryLock(int timeout_ms=0)
Attempt to lock the mutex.
Definition: mutex.h:138
void Unlock()
Unlock the mutex.
Definition: mutex.h:146
etcpal_mutex_t & get()
Get a reference to the underlying etcpal_mutex_t type.
Definition: mutex.h:152
Common definitions used by EtcPal C++ wrappers.
void etcpal_mutex_destroy(etcpal_mutex_t *id)
Destroy a mutex object.
PLATFORM_DEFINED etcpal_mutex_t
The mutex identifier.
Definition: mutex.dox:83
void etcpal_mutex_unlock(etcpal_mutex_t *id)
Unlock a mutex.
bool etcpal_mutex_create(etcpal_mutex_t *id)
Create a new mutex.
bool etcpal_mutex_timed_lock(etcpal_mutex_t *id, int timeout_ms)
Try to lock a mutex, giving up after a timeout.
bool etcpal_mutex_lock(etcpal_mutex_t *id)
Lock a mutex.