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:
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
176 {
177 public:
181 SharedMutex ();
182
187 SharedMutex (const SharedMutex& other) = delete;
188
194 SharedMutex& operator= (const SharedMutex& other) = delete;
195
200 SharedMutex (SharedMutex&& other) = delete;
201
208
212 ~SharedMutex ();
213
217 void lock () noexcept;
218
223 bool tryLock () noexcept;
224
228 void unlock () noexcept;
229
234 pthread_mutex_t* handle () noexcept;
235
236 private:
238 pthread_mutex_t _handle;
239 };
240
244 template <typename Lockable>
246 {
247 public:
251 ScopedLock () = delete;
252
257 explicit ScopedLock (Lockable& mutex)
258 : _mutex (mutex)
259 {
260 _mutex.lock ();
261 }
262
267 ScopedLock (const ScopedLock& other) = delete;
268
274 ScopedLock& operator= (const ScopedLock& other) = delete;
275
280 ScopedLock (ScopedLock&& other) = delete;
281
287 ScopedLock& operator= (ScopedLock&& other) = delete;
288
293 {
294 _mutex.unlock ();
295 }
296
301 Lockable* mutex () const
302 {
303 return &_mutex;
304 }
305
306 private:
308 Lockable& _mutex;
309 };
310}
311
312#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:246
ScopedLock()=delete
default constructor.
Lockable * mutex() const
get associated mutex.
Definition mutex.hpp:301
ScopedLock(const ScopedLock &other)=delete
copy constructor.
ScopedLock(ScopedLock &&other)=delete
move constructor.
~ScopedLock()
releases the ownership of the owned mutex.
Definition mutex.hpp:292
ScopedLock(Lockable &mutex)
acquires ownership of the given mutex.
Definition mutex.hpp:257
class used to protect shared data from being simultaneously accessed by multiple process via a shared...
Definition mutex.hpp:176
SharedMutex(const SharedMutex &other)=delete
copy constructor.
SharedMutex(SharedMutex &&other)=delete
move constructor.
Definition acceptor.hpp:32