libfilezilla
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
mutex.hpp
Go to the documentation of this file.
1 #ifndef LIBFILEZILLA_MUTEX_HEADER
2 #define LIBFILEZILLA_MUTEX_HEADER
3 
7 #include "libfilezilla.hpp"
8 #include "time.hpp"
9 
10 #ifdef FZ_WINDOWS
11 #include "private/windows.hpp"
12 #else
13 #include <pthread.h>
14 #endif
15 
16 namespace fz {
17 
27 class FZ_PUBLIC_SYMBOL mutex final
28 {
29 public:
30  explicit mutex(bool recursive = true);
31  ~mutex();
32 
33  mutex(mutex const&) = delete;
34  mutex& operator=(mutex const&) = delete;
35 
37  void lock();
38 
40  void unlock();
41 
42 private:
43  friend class condition;
44  friend class scoped_lock;
45 
46 #ifdef FZ_WINDOWS
47  CRITICAL_SECTION m_;
48 #else
49  pthread_mutex_t m_;
50 #endif
51 };
52 
61 class FZ_PUBLIC_SYMBOL scoped_lock final
62 {
63 public:
64  explicit scoped_lock(mutex& m)
65  : m_(&m.m_)
66  {
67 #ifdef FZ_WINDOWS
68  EnterCriticalSection(m_);
69 #else
70  pthread_mutex_lock(m_);
71 #endif
72  }
73 
74  ~scoped_lock()
75  {
76  if (locked_) {
77 #ifdef FZ_WINDOWS
78  LeaveCriticalSection(m_);
79 #else
80  pthread_mutex_unlock(m_);
81 #endif
82  }
83 
84  }
85 
86  scoped_lock(scoped_lock const&) = delete;
87  scoped_lock& operator=(scoped_lock const&) = delete;
88 
90  {
91  m_ = op.m_;
92  op.m_ = 0;
93  locked_ = op.locked_;
94  op.locked_ = false;
95  }
96 
97  scoped_lock& operator=(scoped_lock && op)
98  {
99  if (this != &op) {
100  m_ = op.m_;
101  op.m_ = 0;
102  locked_ = op.locked_;
103  op.locked_ = false;
104  }
105  return *this;
106  }
107 
112  void lock()
113  {
114  locked_ = true;
115 #ifdef FZ_WINDOWS
116  EnterCriticalSection(m_);
117 #else
118  pthread_mutex_lock(m_);
119 #endif
120  }
121 
126  void unlock()
127  {
128  locked_ = false;
129 #ifdef FZ_WINDOWS
130  LeaveCriticalSection(m_);
131 #else
132  pthread_mutex_unlock(m_);
133 #endif
134  }
135 
136 private:
137  friend class condition;
138 
139 #ifdef FZ_WINDOWS
140  CRITICAL_SECTION * m_;
141 #else
142  pthread_mutex_t * m_;
143 #endif
144  bool locked_{true};
145 };
146 
151 class FZ_PUBLIC_SYMBOL condition final
152 {
153 public:
154  condition();
155  ~condition();
156 
157  condition(condition const&) = delete;
158  condition& operator=(condition const&) = delete;
159 
166  void wait(scoped_lock& l);
167 
179  bool wait(scoped_lock& l, duration const& timeout);
180 
190  void signal(scoped_lock& l);
191 
199  bool signalled(scoped_lock const&) const { return signalled_; }
200 private:
201 #ifdef FZ_WINDOWS
202  CONDITION_VARIABLE cond_;
203 #else
204  pthread_cond_t cond_;
205 #endif
206  bool signalled_{};
207 };
208 
209 }
210 #endif
A simple scoped lock.
Definition: mutex.hpp:61
bool signalled(scoped_lock const &) const
Check if condition is already signalled.
Definition: mutex.hpp:199
Waitable condition variable.
Definition: mutex.hpp:151
void lock()
Obtains the mutex.
Definition: mutex.hpp:112
Assorted classes dealing with time.
void unlock()
Releases the mutex.
Definition: mutex.hpp:126
The duration class represents a time interval in milliseconds.
Definition: time.hpp:271
Sets some global macros and further includes string.hpp.
Lean replacement for std::(recursive_)mutex.
Definition: mutex.hpp:27