Qore Programming Language: include/qore/QoreRWLock.h (6585) Source File

Qore Programming Language 0.8.11

- Main Page
- Related Pages
- Modules
- Classes
- Files

- File List
- File Members

All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreRWLock.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3 QoreRWLock.h
4
5 simple pthreads-based read-write lock
6
7 Qore Programming Language
8
9 Copyright 2003 - 2013 David Nichols
10
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
15
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #ifndef _QORE_QORERWLOCK_H
27 #define _QORE_QORERWLOCK_H
28
29 #include <pthread.h>
30
32
35 class QoreRWLock {
36 protected:
38 pthread_rwlock_t m;
39
41 DLLLOCAL QoreRWLock& operator=(const QoreRWLock&);
42
43 public:
45 DLLLOCAL QoreRWLock() {
46 #ifndef NDEBUG
47 int rc =
48 #endif
49 pthread_rwlock_init(&m, 0);
50 assert(!rc);
51 }
52
54 DLLLOCAL ~QoreRWLock() {
55 #ifndef NDEBUG
56 int rc =
57 #endif
58 pthread_rwlock_destroy(&m);
59 assert(!rc);
60 }
61
63 DLLLOCAL int rdlock() {
64 return pthread_rwlock_rdlock(&m);
65 }
66
68 DLLLOCAL int wrlock() {
69 return pthread_rwlock_wrlock(&m);
70 }
71
73 DLLLOCAL int tryrdlock() {
74 return pthread_rwlock_tryrdlock(&m);
75 }
76
78 DLLLOCAL int trywrlock() {
79 return pthread_rwlock_trywrlock(&m);
80 }
81
83 DLLLOCAL int unlock() {
84 return pthread_rwlock_unlock(&m);
85 }
86 };
87
89
93 class QoreAutoRWReadLocker {
94 private:
96 DLLLOCAL QoreAutoRWReadLocker(const QoreAutoRWReadLocker&);
97
99 DLLLOCAL QoreAutoRWReadLocker& operator=(const QoreAutoRWReadLocker&);
100
102 DLLLOCAL void *operator new(size_t);
103
104 protected:
106 QoreRWLock *l;
107
108 public:
110 DLLLOCAL QoreAutoRWReadLocker(QoreRWLock &n_l) : l(&n_l) {
111 l->rdlock();
112 }
113
115 DLLLOCAL QoreAutoRWReadLocker(QoreRWLock *n_l) : l(n_l) {
116 l->rdlock();
117 }
118
120 DLLLOCAL ~QoreAutoRWReadLocker() {
121 l->unlock();
122 }
123 };
124
126
130 class QoreAutoRWWriteLocker {
131 private:
133 DLLLOCAL QoreAutoRWWriteLocker(const QoreAutoRWWriteLocker&);
134
136 DLLLOCAL QoreAutoRWWriteLocker& operator=(const QoreAutoRWWriteLocker&);
137
139 DLLLOCAL void *operator new(size_t);
140
141 protected:
143 QoreRWLock *l;
144
145 public:
147 DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock &n_l) : l(&n_l) {
148 l->wrlock();
149 }
150
152 DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock *n_l) : l(n_l) {
153 l->wrlock();
154 }
155
157 DLLLOCAL ~QoreAutoRWWriteLocker() {
158 l->unlock();
159 }
160 };
161
163
167 class QoreSafeRWReadLocker {
168 private:
170 DLLLOCAL QoreSafeRWReadLocker(const QoreSafeRWReadLocker&);
171
173 DLLLOCAL QoreSafeRWReadLocker& operator=(const QoreSafeRWReadLocker&);
174
176 DLLLOCAL void *operator new(size_t);
177
178 protected:
180 QoreRWLock *l;
181
183 bool locked;
184
185 public:
187 DLLLOCAL QoreSafeRWReadLocker(QoreRWLock &n_l) : l(&n_l) {
188 l->rdlock();
189 locked = true;
190 }
191
193 DLLLOCAL QoreSafeRWReadLocker(QoreRWLock *n_l) : l(n_l) {
194 l->rdlock();
195 locked = true;
196 }
197
199 DLLLOCAL ~QoreSafeRWReadLocker() {
200 if (locked)
201 l->unlock();
202 }
203
205 DLLLOCAL void lock() {
206 assert(!locked);
207 l->rdlock();
208 locked = true;
209 }
210
212 DLLLOCAL void unlock() {
213 assert(locked);
214 locked = false;
215 l->unlock();
216 }
217
219 DLLLOCAL void stay_locked() {
220 assert(locked);
221 locked = false;
222 }
223 };
224
226
230 class QoreSafeRWWriteLocker {
231 private:
233 DLLLOCAL QoreSafeRWWriteLocker(const QoreSafeRWWriteLocker&);
234
236 DLLLOCAL QoreSafeRWWriteLocker& operator=(const QoreSafeRWWriteLocker&);
237
239 DLLLOCAL void *operator new(size_t);
240
241 protected:
243 QoreRWLock *l;
244
246 bool locked;
247
248 public:
250 DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock &n_l) : l(&n_l) {
251 l->wrlock();
252 locked = true;
253 }
254
256 DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock *n_l) : l(n_l) {
257 l->wrlock();
258 locked = true;
259 }
260
262 DLLLOCAL ~QoreSafeRWWriteLocker() {
263 if (locked)
264 l->unlock();
265 }
266
268 DLLLOCAL void lock() {
269 assert(!locked);
270 l->wrlock();
271 locked = true;
272 }
273
275 DLLLOCAL void unlock() {
276 assert(locked);
277 locked = false;
278 l->unlock();
279 }
280
282 DLLLOCAL void stay_locked() {
283 assert(locked);
284 locked = false;
285 }
286 };
287
288 class QoreOptionalRWWriteLocker {
289 protected:
290 QoreRWLock* l;
291
292 public:
293 DLLLOCAL QoreOptionalRWWriteLocker(QoreRWLock* n_l) : l(n_l->trywrlock() ? 0 : n_l) {
294 }
295
296 DLLLOCAL QoreOptionalRWWriteLocker(QoreRWLock& n_l) : l(n_l.trywrlock() ? 0 : &n_l) {
297 }
298
299 DLLLOCAL ~QoreOptionalRWWriteLocker() {
300 if (l)
301 l->unlock();
302 }
303
304 DLLLOCAL operator bool() const {
305 return (bool)l;
306 }
307 };
308
309 class QoreOptionalRWReadLocker {
310 protected:
311 QoreRWLock* l;
312
313 public:
314 DLLLOCAL QoreOptionalRWReadLocker(QoreRWLock* n_l) : l(n_l->tryrdlock() ? 0 : n_l) {
315 }
316
317 DLLLOCAL QoreOptionalRWReadLocker(QoreRWLock& n_l) : l(n_l.tryrdlock() ? 0 : &n_l) {
318 }
319
320 DLLLOCAL ~QoreOptionalRWReadLocker() {
321 if (l)
322 l->unlock();
323 }
324
325 DLLLOCAL operator bool() const {
326 return (bool)l;
327 }
328 };
329
330 #endif // #ifndef _QORE_QORERWLOCK_H
QoreAutoRWWriteLocker
provides a safe and exception-safe way to hold write locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:130
QoreAutoRWWriteLocker::QoreAutoRWWriteLocker
DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock &n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:147
QoreRWLock::wrlock
DLLLOCAL int wrlock()
grabs the write lock
Definition: QoreRWLock.h:68
QoreSafeRWWriteLocker::QoreSafeRWWriteLocker
DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock &n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:250
QoreSafeRWReadLocker::unlock
DLLLOCAL void unlock()
unlocks the object and updates the locked flag, assumes that the lock is held
Definition: QoreRWLock.h:212
QoreSafeRWReadLocker::~QoreSafeRWReadLocker
DLLLOCAL ~QoreSafeRWReadLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:199
QoreSafeRWWriteLocker::unlock
DLLLOCAL void unlock()
unlocks the object and updates the locked flag, assumes that the lock is held
Definition: QoreRWLock.h:275
QoreSafeRWReadLocker::QoreSafeRWReadLocker
DLLLOCAL QoreSafeRWReadLocker(QoreRWLock *n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:193
QoreRWLock::QoreRWLock
DLLLOCAL QoreRWLock()
creates and initializes the lock
Definition: QoreRWLock.h:45
QoreRWLock::~QoreRWLock
DLLLOCAL ~QoreRWLock()
destroys the lock
Definition: QoreRWLock.h:54
QoreAutoRWReadLocker::~QoreAutoRWReadLocker
DLLLOCAL ~QoreAutoRWReadLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:120
QoreAutoRWWriteLocker::~QoreAutoRWWriteLocker
DLLLOCAL ~QoreAutoRWWriteLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:157
QoreSafeRWWriteLocker::locked
bool locked
lock flag
Definition: QoreRWLock.h:246
QoreSafeRWReadLocker::locked
bool locked
lock flag
Definition: QoreRWLock.h:183
QoreSafeRWReadLocker::QoreSafeRWReadLocker
DLLLOCAL QoreSafeRWReadLocker(QoreRWLock &n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:187
QoreSafeRWWriteLocker
provides a safe and exception-safe way to hold write locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:230
QoreRWLock::unlock
DLLLOCAL int unlock()
unlocks the lock (assumes the lock is locked)
Definition: QoreRWLock.h:83
QoreRWLock::rdlock
DLLLOCAL int rdlock()
grabs the read lock
Definition: QoreRWLock.h:63
QoreAutoRWReadLocker
provides a safe and exception-safe way to hold read locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:93
QoreAutoRWReadLocker::QoreAutoRWReadLocker
DLLLOCAL QoreAutoRWReadLocker(QoreRWLock &n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:110
QoreSafeRWReadLocker
provides a safe and exception-safe way to hold read locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:167
QoreAutoRWWriteLocker::l
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:143
QoreSafeRWReadLocker::lock
DLLLOCAL void lock()
locks the object and updates the locked flag, assumes that the lock is not already held ...
Definition: QoreRWLock.h:205
QoreAutoRWReadLocker::l
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:106
QoreSafeRWReadLocker::stay_locked
DLLLOCAL void stay_locked()
will not unlock the lock when the destructor is run; do not use any other functions of this class aft...
Definition: QoreRWLock.h:219
QoreRWLock
provides a simple POSIX-threads-based read-write lock
Definition: QoreRWLock.h:35
QoreSafeRWWriteLocker::QoreSafeRWWriteLocker
DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock *n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:256
QoreRWLock::m
pthread_rwlock_t m
the actual locking primitive wrapped in this class
Definition: QoreRWLock.h:38
QoreSafeRWWriteLocker::stay_locked
DLLLOCAL void stay_locked()
will not unlock the lock when the destructor is run; do not use any other functions of this class aft...
Definition: QoreRWLock.h:282
QoreAutoRWReadLocker::QoreAutoRWReadLocker
DLLLOCAL QoreAutoRWReadLocker(QoreRWLock *n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:115
QoreRWLock::tryrdlock
DLLLOCAL int tryrdlock()
tries to grab the read lock; does not block if unsuccessful
Definition: QoreRWLock.h:73
QoreRWLock::operator=
DLLLOCAL QoreRWLock & operator=(const QoreRWLock &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
QoreAutoRWWriteLocker::QoreAutoRWWriteLocker
DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock *n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:152
QoreSafeRWWriteLocker::l
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:243
QoreSafeRWReadLocker::l
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:180
QoreSafeRWWriteLocker::lock
DLLLOCAL void lock()
locks the object and updates the locked flag, assumes that the lock is not already held ...
Definition: QoreRWLock.h:268
QoreRWLock::trywrlock
DLLLOCAL int trywrlock()
tries to grab the write lock; does not block if unsuccessful
Definition: QoreRWLock.h:78
QoreSafeRWWriteLocker::~QoreSafeRWWriteLocker
DLLLOCAL ~QoreSafeRWWriteLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:262