Qore Programming Language  0.8.9
 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 
94 private:
97 
99  DLLLOCAL QoreAutoRWReadLocker& operator=(const QoreAutoRWReadLocker&);
100 
102  DLLLOCAL void *operator new(size_t);
103 
104 protected:
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 
121  l->unlock();
122  }
123 };
124 
126 
131 private:
134 
136  DLLLOCAL QoreAutoRWWriteLocker& operator=(const QoreAutoRWWriteLocker&);
137 
139  DLLLOCAL void *operator new(size_t);
140 
141 protected:
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 
158  l->unlock();
159  }
160 };
161 
163 
168 private:
171 
173  DLLLOCAL QoreSafeRWReadLocker& operator=(const QoreSafeRWReadLocker&);
174 
176  DLLLOCAL void *operator new(size_t);
177 
178 protected:
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 
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 
231 private:
234 
236  DLLLOCAL QoreSafeRWWriteLocker& operator=(const QoreSafeRWWriteLocker&);
237 
239  DLLLOCAL void *operator new(size_t);
240 
241 protected:
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 
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
provides a safe and exception-safe way to hold write locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:130
DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock &n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:147
DLLLOCAL int wrlock()
grabs the write lock
Definition: QoreRWLock.h:68
DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock &n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:250
DLLLOCAL void unlock()
unlocks the object and updates the locked flag, assumes that the lock is held
Definition: QoreRWLock.h:212
DLLLOCAL ~QoreSafeRWReadLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:199
DLLLOCAL void unlock()
unlocks the object and updates the locked flag, assumes that the lock is held
Definition: QoreRWLock.h:275
DLLLOCAL QoreSafeRWReadLocker(QoreRWLock *n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:193
DLLLOCAL QoreRWLock()
creates and initializes the lock
Definition: QoreRWLock.h:45
DLLLOCAL ~QoreRWLock()
destroys the lock
Definition: QoreRWLock.h:54
DLLLOCAL ~QoreAutoRWReadLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:120
DLLLOCAL ~QoreAutoRWWriteLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:157
bool locked
lock flag
Definition: QoreRWLock.h:246
bool locked
lock flag
Definition: QoreRWLock.h:183
DLLLOCAL QoreSafeRWReadLocker(QoreRWLock &n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:187
provides a safe and exception-safe way to hold write locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:230
DLLLOCAL int unlock()
unlocks the lock (assumes the lock is locked)
Definition: QoreRWLock.h:83
DLLLOCAL int rdlock()
grabs the read lock
Definition: QoreRWLock.h:63
provides a safe and exception-safe way to hold read locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:93
DLLLOCAL QoreAutoRWReadLocker(QoreRWLock &n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:110
provides a safe and exception-safe way to hold read locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:167
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:143
DLLLOCAL void lock()
locks the object and updates the locked flag, assumes that the lock is not already held ...
Definition: QoreRWLock.h:205
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:106
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
provides a simple POSIX-threads-based read-write lock
Definition: QoreRWLock.h:35
DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock *n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:256
pthread_rwlock_t m
the actual locking primitive wrapped in this class
Definition: QoreRWLock.h:38
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
DLLLOCAL QoreAutoRWReadLocker(QoreRWLock *n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:115
DLLLOCAL int tryrdlock()
tries to grab the read lock; does not block if unsuccessful
Definition: QoreRWLock.h:73
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:152
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:243
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:180
DLLLOCAL void lock()
locks the object and updates the locked flag, assumes that the lock is not already held ...
Definition: QoreRWLock.h:268
DLLLOCAL int trywrlock()
tries to grab the write lock; does not block if unsuccessful
Definition: QoreRWLock.h:78
DLLLOCAL ~QoreSafeRWWriteLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:262