Qore Programming Language  0.8.11.1
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 (C) 2003 - 2014 David Nichols
10 
11  Permission is hereby granted, free of charge, to any person obtaining a
12  copy of this software and associated documentation files (the "Software"),
13  to deal in the Software without restriction, including without limitation
14  the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  and/or sell copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following conditions:
17 
18  The above copyright notice and this permission notice shall be included in
19  all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  DEALINGS IN THE SOFTWARE.
28 
29  Note that the Qore library is released under a choice of three open-source
30  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
31  information.
32 */
33 
34 #ifndef _QORE_QORERWLOCK_H
35 #define _QORE_QORERWLOCK_H
36 
37 #include <pthread.h>
38 
40 
43 class QoreRWLock {
44 protected:
46  pthread_rwlock_t m;
47 
49  DLLLOCAL QoreRWLock& operator=(const QoreRWLock&);
50 
51 public:
53  DLLLOCAL QoreRWLock() {
54 #ifndef NDEBUG
55  int rc =
56 #endif
57  pthread_rwlock_init(&m, 0);
58  assert(!rc);
59  }
60 
62  DLLLOCAL ~QoreRWLock() {
63 #ifndef NDEBUG
64  int rc =
65 #endif
66  pthread_rwlock_destroy(&m);
67  assert(!rc);
68  }
69 
71  DLLLOCAL int rdlock() {
72  return pthread_rwlock_rdlock(&m);
73  }
74 
76  DLLLOCAL int wrlock() {
77  return pthread_rwlock_wrlock(&m);
78  }
79 
81  DLLLOCAL int tryrdlock() {
82  return pthread_rwlock_tryrdlock(&m);
83  }
84 
86  DLLLOCAL int trywrlock() {
87  return pthread_rwlock_trywrlock(&m);
88  }
89 
91  DLLLOCAL int unlock() {
92  return pthread_rwlock_unlock(&m);
93  }
94 };
95 
97 
102 private:
105 
107  DLLLOCAL QoreAutoRWReadLocker& operator=(const QoreAutoRWReadLocker&);
108 
110  DLLLOCAL void *operator new(size_t);
111 
112 protected:
115 
116 public:
118  DLLLOCAL QoreAutoRWReadLocker(QoreRWLock &n_l) : l(&n_l) {
119  l->rdlock();
120  }
121 
123  DLLLOCAL QoreAutoRWReadLocker(QoreRWLock *n_l) : l(n_l) {
124  l->rdlock();
125  }
126 
129  l->unlock();
130  }
131 };
132 
134 
139 private:
142 
144  DLLLOCAL QoreAutoRWWriteLocker& operator=(const QoreAutoRWWriteLocker&);
145 
147  DLLLOCAL void *operator new(size_t);
148 
149 protected:
152 
153 public:
155  DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock &n_l) : l(&n_l) {
156  l->wrlock();
157  }
158 
160  DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock *n_l) : l(n_l) {
161  l->wrlock();
162  }
163 
166  l->unlock();
167  }
168 };
169 
171 
176 private:
179 
181  DLLLOCAL QoreSafeRWReadLocker& operator=(const QoreSafeRWReadLocker&);
182 
184  DLLLOCAL void *operator new(size_t);
185 
186 protected:
189 
191  bool locked;
192 
193 public:
195  DLLLOCAL QoreSafeRWReadLocker(QoreRWLock &n_l) : l(&n_l) {
196  l->rdlock();
197  locked = true;
198  }
199 
201  DLLLOCAL QoreSafeRWReadLocker(QoreRWLock *n_l) : l(n_l) {
202  l->rdlock();
203  locked = true;
204  }
205 
208  if (locked)
209  l->unlock();
210  }
211 
213  DLLLOCAL void lock() {
214  assert(!locked);
215  l->rdlock();
216  locked = true;
217  }
218 
220  DLLLOCAL void unlock() {
221  assert(locked);
222  locked = false;
223  l->unlock();
224  }
225 
227  DLLLOCAL void stay_locked() {
228  assert(locked);
229  locked = false;
230  }
231 };
232 
234 
239 private:
242 
244  DLLLOCAL QoreSafeRWWriteLocker& operator=(const QoreSafeRWWriteLocker&);
245 
247  DLLLOCAL void *operator new(size_t);
248 
249 protected:
252 
254  bool locked;
255 
256 public:
258  DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock &n_l) : l(&n_l) {
259  l->wrlock();
260  locked = true;
261  }
262 
264  DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock *n_l) : l(n_l) {
265  l->wrlock();
266  locked = true;
267  }
268 
271  if (locked)
272  l->unlock();
273  }
274 
276  DLLLOCAL void lock() {
277  assert(!locked);
278  l->wrlock();
279  locked = true;
280  }
281 
283  DLLLOCAL void unlock() {
284  assert(locked);
285  locked = false;
286  l->unlock();
287  }
288 
290  DLLLOCAL void stay_locked() {
291  assert(locked);
292  locked = false;
293  }
294 };
295 
296 class QoreOptionalRWWriteLocker {
297 protected:
298  QoreRWLock* l;
299 
300 public:
301  DLLLOCAL QoreOptionalRWWriteLocker(QoreRWLock* n_l) : l(n_l->trywrlock() ? 0 : n_l) {
302  }
303 
304  DLLLOCAL QoreOptionalRWWriteLocker(QoreRWLock& n_l) : l(n_l.trywrlock() ? 0 : &n_l) {
305  }
306 
307  DLLLOCAL ~QoreOptionalRWWriteLocker() {
308  if (l)
309  l->unlock();
310  }
311 
312  DLLLOCAL operator bool() const {
313  return (bool)l;
314  }
315 };
316 
317 class QoreOptionalRWReadLocker {
318 protected:
319  QoreRWLock* l;
320 
321 public:
322  DLLLOCAL QoreOptionalRWReadLocker(QoreRWLock* n_l) : l(n_l->tryrdlock() ? 0 : n_l) {
323  }
324 
325  DLLLOCAL QoreOptionalRWReadLocker(QoreRWLock& n_l) : l(n_l.tryrdlock() ? 0 : &n_l) {
326  }
327 
328  DLLLOCAL ~QoreOptionalRWReadLocker() {
329  if (l)
330  l->unlock();
331  }
332 
333  DLLLOCAL operator bool() const {
334  return (bool)l;
335  }
336 };
337 
338 #endif // #ifndef _QORE_QORERWLOCK_H
provides a safe and exception-safe way to hold write locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:138
DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock &n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:155
DLLLOCAL int wrlock()
grabs the write lock
Definition: QoreRWLock.h:76
DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock &n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:258
DLLLOCAL void unlock()
unlocks the object and updates the locked flag, assumes that the lock is held
Definition: QoreRWLock.h:220
DLLLOCAL ~QoreSafeRWReadLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:207
DLLLOCAL void unlock()
unlocks the object and updates the locked flag, assumes that the lock is held
Definition: QoreRWLock.h:283
DLLLOCAL QoreSafeRWReadLocker(QoreRWLock *n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:201
DLLLOCAL QoreRWLock()
creates and initializes the lock
Definition: QoreRWLock.h:53
DLLLOCAL ~QoreRWLock()
destroys the lock
Definition: QoreRWLock.h:62
DLLLOCAL ~QoreAutoRWReadLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:128
DLLLOCAL ~QoreAutoRWWriteLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:165
bool locked
lock flag
Definition: QoreRWLock.h:254
bool locked
lock flag
Definition: QoreRWLock.h:191
DLLLOCAL QoreSafeRWReadLocker(QoreRWLock &n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:195
provides a safe and exception-safe way to hold write locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:238
DLLLOCAL int unlock()
unlocks the lock (assumes the lock is locked)
Definition: QoreRWLock.h:91
DLLLOCAL int rdlock()
grabs the read lock
Definition: QoreRWLock.h:71
provides a safe and exception-safe way to hold read locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:101
DLLLOCAL QoreAutoRWReadLocker(QoreRWLock &n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:118
provides a safe and exception-safe way to hold read locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:175
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:151
DLLLOCAL void lock()
locks the object and updates the locked flag, assumes that the lock is not already held ...
Definition: QoreRWLock.h:213
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:114
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:227
provides a simple POSIX-threads-based read-write lock
Definition: QoreRWLock.h:43
DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock *n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:264
pthread_rwlock_t m
the actual locking primitive wrapped in this class
Definition: QoreRWLock.h:46
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:290
DLLLOCAL QoreAutoRWReadLocker(QoreRWLock *n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:123
DLLLOCAL int tryrdlock()
tries to grab the read lock; does not block if unsuccessful
Definition: QoreRWLock.h:81
DLLLOCAL QoreRWLock & operator=(const QoreRWLock &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock *n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:160
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:251
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:188
DLLLOCAL void lock()
locks the object and updates the locked flag, assumes that the lock is not already held ...
Definition: QoreRWLock.h:276
DLLLOCAL int trywrlock()
tries to grab the write lock; does not block if unsuccessful
Definition: QoreRWLock.h:86
DLLLOCAL ~QoreSafeRWWriteLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:270