libfilezilla
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 
43  bool try_lock();
44 
45 private:
46  friend class condition;
47  friend class scoped_lock;
48 
49 #ifdef FZ_WINDOWS
50  CRITICAL_SECTION m_;
51 #else
52  pthread_mutex_t m_;
53 #endif
54 };
55 
64 class FZ_PUBLIC_SYMBOL scoped_lock final
65 {
66 public:
67  explicit scoped_lock(mutex& m)
68  : m_(&m.m_)
69  {
70 #ifdef FZ_WINDOWS
71  EnterCriticalSection(m_);
72 #else
73  pthread_mutex_lock(m_);
74 #endif
75  }
76 
77  ~scoped_lock()
78  {
79  if (locked_) {
80 #ifdef FZ_WINDOWS
81  LeaveCriticalSection(m_);
82 #else
83  pthread_mutex_unlock(m_);
84 #endif
85  }
86 
87  }
88 
89  scoped_lock(scoped_lock const&) = delete;
90  scoped_lock& operator=(scoped_lock const&) = delete;
91 
92  scoped_lock(scoped_lock && op) noexcept
93  {
94  m_ = op.m_;
95  op.m_ = 0;
96  locked_ = op.locked_;
97  op.locked_ = false;
98  }
99 
100  scoped_lock& operator=(scoped_lock && op) noexcept
101  {
102  if (this != &op) {
103  m_ = op.m_;
104  op.m_ = 0;
105  locked_ = op.locked_;
106  op.locked_ = false;
107  }
108  return *this;
109  }
110 
115  void lock()
116  {
117  locked_ = true;
118 #ifdef FZ_WINDOWS
119  EnterCriticalSection(m_);
120 #else
121  pthread_mutex_lock(m_);
122 #endif
123  }
124 
129  void unlock()
130  {
131  locked_ = false;
132 #ifdef FZ_WINDOWS
133  LeaveCriticalSection(m_);
134 #else
135  pthread_mutex_unlock(m_);
136 #endif
137  }
138 
139 private:
140  friend class condition;
141 
142 #ifdef FZ_WINDOWS
143  CRITICAL_SECTION * m_;
144 #else
145  pthread_mutex_t * m_;
146 #endif
147  bool locked_{true};
148 };
149 
154 class FZ_PUBLIC_SYMBOL condition final
155 {
156 public:
157  condition();
158  ~condition();
159 
160  condition(condition const&) = delete;
161  condition& operator=(condition const&) = delete;
162 
169  void wait(scoped_lock& l);
170 
182  bool wait(scoped_lock& l, duration const& timeout);
183 
193  void signal(scoped_lock& l);
194 
202  bool signalled(scoped_lock const&) const { return signalled_; }
203 private:
204 #ifdef FZ_WINDOWS
205  CONDITION_VARIABLE cond_;
206 #else
207  pthread_cond_t cond_;
208 #endif
209  bool signalled_{};
210 };
211 
212 }
213 #endif
bool signalled(scoped_lock const &) const
Check if condition is already signalled.
Definition: mutex.hpp:202
A simple scoped lock.
Definition: mutex.hpp:64
Waitable condition variable.
Definition: mutex.hpp:154
void lock()
Obtains the mutex.
Definition: mutex.hpp:115
Assorted classes dealing with time.
void unlock()
Releases the mutex.
Definition: mutex.hpp:129
The namespace used by libfilezilla.
Definition: apply.hpp:17
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