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 "glue/windows.hpp"
12#else
13#include <pthread.h>
14#endif
15
16namespace fz {
17
27class FZ_PUBLIC_SYMBOL mutex final
28{
29public:
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
45private:
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
64class FZ_PUBLIC_SYMBOL scoped_lock final
65{
66public:
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
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
139private:
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
154class FZ_PUBLIC_SYMBOL condition final
155{
156public:
157 condition();
158 ~condition();
159
160 condition(condition const&) = delete;
161 condition& operator=(condition const&) = delete;
162
170
182 bool wait(scoped_lock& l, duration const& timeout);
183
194
202 bool signalled(scoped_lock const&) const { return signalled_; }
203private:
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
Waitable condition variable.
Definition: mutex.hpp:155
void wait(scoped_lock &l)
Wait indefinitely for condition to become signalled.
bool wait(scoped_lock &l, duration const &timeout)
Wait until timeout for condition to become signalled.
bool signalled(scoped_lock const &) const
Check if condition is already signalled.
Definition: mutex.hpp:202
void signal(scoped_lock &l)
Signal condition variable.
The duration class represents a time interval in milliseconds.
Definition: time.hpp:286
Lean replacement for std::(recursive_)mutex.
Definition: mutex.hpp:28
void unlock()
Beware, manual locking isn't exception safe, use scoped_lock.
bool try_lock()
Beware, manual locking isn't exception safe.
void lock()
Beware, manual locking isn't exception safe, use scoped_lock.
A simple scoped lock.
Definition: mutex.hpp:65
void unlock()
Releases the mutex.
Definition: mutex.hpp:129
void lock()
Obtains the mutex.
Definition: mutex.hpp:115
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition: apply.hpp:17
Assorted classes dealing with time.