libfilezilla
Loading...
Searching...
No Matches
rwmutex.hpp
Go to the documentation of this file.
1#ifndef LIBFILEZILLA_RWMUTEX_HEADER
2#define LIBFILEZILLA_RWMUTEX_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
24{
25public:
26#ifdef FZ_WINDOWS
27 explicit rwmutex() = default;
28#else
29 explicit rwmutex()
30 {
31 pthread_rwlock_init(&m_, nullptr);
32 }
33 ~rwmutex()
34 {
36 }
37#endif
38
39 rwmutex(rwmutex const&) = delete;
40 rwmutex& operator=(rwmutex const&) = delete;
41
43 void lock_read()
44 {
45#ifdef FZ_WINDOWS
47#else
49#endif
50 }
51
52 void lock_write()
53 {
54#ifdef FZ_WINDOWS
56#else
58#endif
59 }
60
63 {
64#ifdef FZ_WINDOWS
66#else
68#endif
69 }
70
71 void unlock_write()
72 {
73#ifdef FZ_WINDOWS
75#else
77#endif
78 }
79
80private:
81 friend class scoped_read_lock;
82 friend class scoped_write_lock;
83
84#ifdef FZ_WINDOWS
85 SRWLOCK m_{};
86#else
88#endif
89};
90
100{
101public:
102 explicit scoped_read_lock(rwmutex& m)
103 : m_(&m.m_)
104 {
105#ifdef FZ_WINDOWS
107#else
109#endif
110 }
111
113 {
114 if (locked_) {
115#ifdef FZ_WINDOWS
117#else
119#endif
120 }
121
122 }
123
124 scoped_read_lock(scoped_read_lock const&) = delete;
125 scoped_read_lock& operator=(scoped_read_lock const&) = delete;
126
128 {
129 m_ = op.m_;
130 op.m_ = 0;
131 locked_ = op.locked_;
132 op.locked_ = false;
133 }
134
135 scoped_read_lock& operator=(scoped_read_lock&& op) noexcept
136 {
137 if (this != &op) {
138 m_ = op.m_;
139 op.m_ = 0;
140 locked_ = op.locked_;
141 op.locked_ = false;
142 }
143 return *this;
144 }
145
150 void lock()
151 {
152 locked_ = true;
153#ifdef FZ_WINDOWS
155#else
157#endif
158 }
159
164 void unlock()
165 {
166 locked_ = false;
167#ifdef FZ_WINDOWS
169#else
171#endif
172 }
173
174private:
175#ifdef FZ_WINDOWS
176 SRWLOCK* m_;
177#else
179#endif
180 bool locked_{ true };
181};
182
189{
190public:
191 explicit scoped_write_lock(rwmutex& m)
192 : m_(&m.m_)
193 {
194#ifdef FZ_WINDOWS
196#else
198#endif
199 }
200
202 {
203 if (locked_) {
204#ifdef FZ_WINDOWS
206#else
208#endif
209 }
210
211 }
212
213 scoped_write_lock(scoped_write_lock const&) = delete;
214 scoped_write_lock& operator=(scoped_write_lock const&) = delete;
215
217 {
218 m_ = op.m_;
219 op.m_ = 0;
220 locked_ = op.locked_;
221 op.locked_ = false;
222 }
223
224 scoped_write_lock& operator=(scoped_write_lock&& op) noexcept
225 {
226 if (this != &op) {
227 m_ = op.m_;
228 op.m_ = 0;
229 locked_ = op.locked_;
230 op.locked_ = false;
231 }
232 return *this;
233 }
234
239 void lock()
240 {
241 locked_ = true;
242#ifdef FZ_WINDOWS
244#else
246#endif
247 }
248
253 void unlock()
254 {
255 locked_ = false;
256#ifdef FZ_WINDOWS
258#else
260#endif
261 }
262
263private:
264#ifdef FZ_WINDOWS
265 SRWLOCK* m_;
266#else
268#endif
269 bool locked_{ true };
270};
271
272}
273
274#endif
Lean rw mutex.
Definition rwmutex.hpp:24
void lock_read()
Beware, manual locking isn't exception safe, use scoped_lock.
Definition rwmutex.hpp:43
void unlock_read()
Beware, manual locking isn't exception safe, use scoped_lock.
Definition rwmutex.hpp:62
A simple scoped read lock.
Definition rwmutex.hpp:100
void unlock()
Releases the mutex.
Definition rwmutex.hpp:164
void lock()
Obtains the mutex.
Definition rwmutex.hpp:150
A simple scoped read lock.
Definition rwmutex.hpp:189
void unlock()
Releases the mutex.
Definition rwmutex.hpp:253
void lock()
Obtains the mutex.
Definition rwmutex.hpp:239
Sets some global macros and further includes string.hpp.
The namespace used by libfilezilla.
Definition apply.hpp:17
bool dispatch(event_base const &ev, F &&f)
Dispatch for simple_event<> based events to simple functors.
Definition event_handler.hpp:199
Assorted classes dealing with time.