join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
mutex.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_MUTEX_HPP__
26#define __JOIN_MUTEX_HPP__
27
28// C.
29#include <pthread.h>
30
31namespace join
32{
36 class Mutex
37 {
38 public:
42 Mutex ();
43
48 Mutex (const Mutex& other) = delete;
49
55 Mutex& operator= (const Mutex& other) = delete;
56
61 Mutex (Mutex&& other) = delete;
62
68 Mutex& operator= (Mutex&& other) = delete;
69
73 ~Mutex ();
74
78 void lock () noexcept;
79
84 bool tryLock () noexcept;
85
89 void unlock () noexcept;
90
95 pthread_mutex_t* handle () noexcept;
96
97 private:
99 pthread_mutex_t _handle;
100 };
101
106 {
107 public:
112
117 RecursiveMutex (const Mutex& other) = delete;
118
124 RecursiveMutex& operator= (const RecursiveMutex& other) = delete;
125
130 RecursiveMutex (RecursiveMutex&& other) = delete;
131
138
143
147 void lock () noexcept;
148
153 bool tryLock () noexcept;
154
158 void unlock () noexcept;
159
164 pthread_mutex_t* handle () noexcept;
165
166 private:
168 pthread_mutex_t _handle;
169 };
170
175 {
176 public:
180 SharedMutex ();
181
186 SharedMutex (const SharedMutex& other) = delete;
187
193 SharedMutex& operator= (const SharedMutex& other) = delete;
194
199 SharedMutex (SharedMutex&& other) = delete;
200
207
211 ~SharedMutex ();
212
216 void lock () noexcept;
217
222 bool tryLock () noexcept;
223
227 void unlock () noexcept;
228
233 pthread_mutex_t* handle () noexcept;
234
235 private:
237 pthread_mutex_t _handle;
238 };
239
243 template <typename Lockable>
245 {
246 public:
250 ScopedLock () = delete;
251
256 explicit ScopedLock (Lockable& mutex)
257 : _mutex (mutex)
258 {
259 _mutex.lock ();
260 }
261
266 ScopedLock (const ScopedLock& other) = delete;
267
273 ScopedLock& operator= (const ScopedLock& other) = delete;
274
279 ScopedLock (ScopedLock&& other) = delete;
280
286 ScopedLock& operator= (ScopedLock&& other) = delete;
287
292 {
293 _mutex.unlock ();
294 }
295
300 Lockable* mutex () const
301 {
302 return &_mutex;
303 }
304
305 private:
307 Lockable& _mutex;
308 };
309}
310
311#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:90
Mutex()
default constructor.
Definition mutex.cpp:42
void unlock() noexcept
unlock the mutex.
Definition mutex.cpp:81
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:72
void lock() noexcept
lock the mutex.
Definition mutex.cpp:63
~Mutex()
destroys the mutex object.
Definition mutex.cpp:54
Mutex(const Mutex &other)=delete
copy constructor.
class used to protect shared data from being simultaneously accessed by multiple threads.
Definition mutex.hpp:106
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:245
ScopedLock()=delete
default constructor.
Lockable * mutex() const
get associated mutex.
Definition mutex.hpp:300
ScopedLock(const ScopedLock &other)=delete
copy constructor.
ScopedLock(ScopedLock &&other)=delete
move constructor.
~ScopedLock()
releases the ownership of the owned mutex.
Definition mutex.hpp:291
ScopedLock(Lockable &mutex)
acquires ownership of the given mutex.
Definition mutex.hpp:256
class used to protect shared data from being simultaneously accessed by multiple process via a shared...
Definition mutex.hpp:175
SharedMutex(const SharedMutex &other)=delete
copy constructor.
SharedMutex(SharedMutex &&other)=delete
move constructor.
Definition acceptor.hpp:32