join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
mutex.hpp
Go to the documentation of this file.
1
25#ifndef JOIN_CORE_MUTEX_HPP
26#define JOIN_CORE_MUTEX_HPP
27
28// C.
29#include <pthread.h>
30
31namespace join
32{
36 class Mutex
37 {
38 public:
43 explicit Mutex (bool inherit = false);
44
49 Mutex (const Mutex& other) = delete;
50
56 Mutex& operator= (const Mutex& other) = delete;
57
62 Mutex (Mutex&& other) = delete;
63
69 Mutex& operator= (Mutex&& other) = delete;
70
74 ~Mutex ();
75
79 void lock () noexcept;
80
85 bool tryLock () noexcept;
86
90 void unlock () noexcept;
91
96 pthread_mutex_t* handle () noexcept;
97
98 private:
100 pthread_mutex_t _handle;
101 };
102
107 {
108 public:
113 explicit RecursiveMutex (bool inherit = false);
114
119 RecursiveMutex (const Mutex& other) = delete;
120
126 RecursiveMutex& operator= (const RecursiveMutex& other) = delete;
127
132 RecursiveMutex (RecursiveMutex&& other) = delete;
133
140
145
149 void lock () noexcept;
150
155 bool tryLock () noexcept;
156
160 void unlock () noexcept;
161
166 pthread_mutex_t* handle () noexcept;
167
168 private:
170 pthread_mutex_t _handle;
171 };
172
178 {
179 public:
184 explicit SharedMutex (bool inherit = false);
185
190 SharedMutex (const SharedMutex& other) = delete;
191
197 SharedMutex& operator= (const SharedMutex& other) = delete;
198
203 SharedMutex (SharedMutex&& other) = delete;
204
211
215 ~SharedMutex ();
216
220 void lock () noexcept;
221
226 bool tryLock () noexcept;
227
231 void unlock () noexcept;
232
237 pthread_mutex_t* handle () noexcept;
238
239 private:
241 pthread_mutex_t _handle;
242 };
243
247 template <typename Lockable>
249 {
250 public:
254 ScopedLock () = delete;
255
260 explicit ScopedLock (Lockable& mutex)
261 : _mutex (mutex)
262 {
263 _mutex.lock ();
264 }
265
270 ScopedLock (const ScopedLock& other) = delete;
271
277 ScopedLock& operator= (const ScopedLock& other) = delete;
278
283 ScopedLock (ScopedLock&& other) = delete;
284
290 ScopedLock& operator= (ScopedLock&& other) = delete;
291
296 {
297 _mutex.unlock ();
298 }
299
304 Lockable* mutex () const
305 {
306 return &_mutex;
307 }
308
309 private:
311 Lockable& _mutex;
312 };
313}
314
315#endif
class used to protect shared data from being simultaneously accessed by multiple threads.
Definition mutex.hpp:37
Mutex(Mutex &&other)=delete
move constructor.
pthread_mutex_t * handle() noexcept
get native handle.
Definition mutex.cpp:94
void unlock() noexcept
unlock the mutex.
Definition mutex.cpp:85
Mutex & operator=(const Mutex &other)=delete
copy assignment.
bool tryLock() noexcept
lock the mutex or return immediatly if the mutex is already locked.
Definition mutex.cpp:76
void lock() noexcept
lock the mutex.
Definition mutex.cpp:67
Mutex(bool inherit=false)
default constructor.
Definition mutex.cpp:42
~Mutex()
destroys the mutex object.
Definition mutex.cpp:58
Mutex(const Mutex &other)=delete
copy constructor.
class used to protect shared data from being simultaneously accessed by multiple threads.
Definition mutex.hpp:107
RecursiveMutex(const Mutex &other)=delete
copy constructor.
RecursiveMutex(RecursiveMutex &&other)=delete
move constructor.
class owning a mutex for the duration of a scoped block.
Definition mutex.hpp:249
ScopedLock()=delete
default constructor.
Lockable * mutex() const
get associated mutex.
Definition mutex.hpp:304
ScopedLock(const ScopedLock &other)=delete
copy constructor.
ScopedLock(ScopedLock &&other)=delete
move constructor.
~ScopedLock()
releases the ownership of the owned mutex.
Definition mutex.hpp:295
ScopedLock(Lockable &mutex)
acquires ownership of the given mutex.
Definition mutex.hpp:260
class used to protect shared data from being simultaneously accessed by multiple process via a shared...
Definition mutex.hpp:178
SharedMutex(const SharedMutex &other)=delete
copy constructor.
SharedMutex(SharedMutex &&other)=delete
move constructor.
Definition acceptor.hpp:32