Qore Programming Language - C/C++ Library  0.8.12.2
 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 (C) 2003 - 2015 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 
39 #ifdef DEBUG
40 extern int gettid();
41 #endif
42 
44 
47 class QoreRWLock {
48 protected:
50  pthread_rwlock_t m;
51 
53  DLLLOCAL QoreRWLock& operator=(const QoreRWLock&);
54 
55 public:
57  DLLLOCAL QoreRWLock() {
58 #ifndef NDEBUG
59  int rc =
60 #endif
61  pthread_rwlock_init(&m, 0);
62  assert(!rc);
63  }
64 
66  DLLLOCAL ~QoreRWLock() {
67 #ifndef NDEBUG
68  int rc =
69 #endif
70  pthread_rwlock_destroy(&m);
71  assert(!rc);
72  }
73 
75  DLLLOCAL int wrlock() {
76  return pthread_rwlock_wrlock(&m);
77  }
78 
80  DLLLOCAL int trywrlock() {
81  return pthread_rwlock_trywrlock(&m);
82  }
83 
85  DLLLOCAL int unlock() {
86  return pthread_rwlock_unlock(&m);
87  }
88 
90  DLLLOCAL int rdlock() {
91  return pthread_rwlock_rdlock(&m);
92  }
93 
95  DLLLOCAL int tryrdlock() {
96  return pthread_rwlock_tryrdlock(&m);
97  }
98 };
99 
101 
106 private:
109 
111  DLLLOCAL QoreAutoRWReadLocker& operator=(const QoreAutoRWReadLocker&);
112 
114  DLLLOCAL void *operator new(size_t);
115 
116 protected:
119 
120 public:
122  DLLLOCAL QoreAutoRWReadLocker(QoreRWLock &n_l) : l(&n_l) {
123  l->rdlock();
124  }
125 
127  DLLLOCAL QoreAutoRWReadLocker(QoreRWLock *n_l) : l(n_l) {
128  l->rdlock();
129  }
130 
133  l->unlock();
134  }
135 };
136 
138 
143 private:
146 
148  DLLLOCAL QoreAutoRWWriteLocker& operator=(const QoreAutoRWWriteLocker&);
149 
151  DLLLOCAL void *operator new(size_t);
152 
153 protected:
156 
157 public:
159  DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock &n_l) : l(&n_l) {
160  l->wrlock();
161  }
162 
164  DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock *n_l) : l(n_l) {
165  l->wrlock();
166  }
167 
170  l->unlock();
171  }
172 };
173 
175 
180 private:
183 
185  DLLLOCAL QoreSafeRWReadLocker& operator=(const QoreSafeRWReadLocker&);
186 
188  DLLLOCAL void *operator new(size_t);
189 
190 protected:
193 
195  bool locked;
196 
197 public:
199  DLLLOCAL QoreSafeRWReadLocker(QoreRWLock &n_l) : l(&n_l) {
200  l->rdlock();
201  locked = true;
202  }
203 
205  DLLLOCAL QoreSafeRWReadLocker(QoreRWLock *n_l) : l(n_l) {
206  l->rdlock();
207  locked = true;
208  }
209 
212  if (locked)
213  l->unlock();
214  }
215 
217  DLLLOCAL void lock() {
218  assert(!locked);
219  l->rdlock();
220  locked = true;
221  }
222 
224  DLLLOCAL void unlock() {
225  assert(locked);
226  locked = false;
227  l->unlock();
228  }
229 
231  DLLLOCAL void stay_locked() {
232  assert(locked);
233  locked = false;
234  }
235 };
236 
238 
243 private:
246 
248  DLLLOCAL QoreSafeRWWriteLocker& operator=(const QoreSafeRWWriteLocker&);
249 
251  DLLLOCAL void *operator new(size_t);
252 
253 protected:
256 
258  bool locked;
259 
260 public:
262  DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock &n_l) : l(&n_l) {
263  l->wrlock();
264  locked = true;
265  }
266 
268  DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock *n_l) : l(n_l) {
269  l->wrlock();
270  locked = true;
271  }
272 
275  if (locked)
276  l->unlock();
277  }
278 
280  DLLLOCAL void lock() {
281  assert(!locked);
282  l->wrlock();
283  locked = true;
284  }
285 
287  DLLLOCAL void unlock() {
288  assert(locked);
289  locked = false;
290  l->unlock();
291  }
292 
294  DLLLOCAL void stay_locked() {
295  assert(locked);
296  locked = false;
297  }
298 };
299 
300 class QoreOptionalRWWriteLocker {
301 protected:
302  QoreRWLock* l;
303 
304 public:
305  DLLLOCAL QoreOptionalRWWriteLocker(QoreRWLock* n_l) : l(n_l->trywrlock() ? 0 : n_l) {
306  }
307 
308  DLLLOCAL QoreOptionalRWWriteLocker(QoreRWLock& n_l) : l(n_l.trywrlock() ? 0 : &n_l) {
309  }
310 
311  DLLLOCAL ~QoreOptionalRWWriteLocker() {
312  if (l)
313  l->unlock();
314  }
315 
316  DLLLOCAL operator bool() const {
317  return (bool)l;
318  }
319 };
320 
321 class QoreOptionalRWReadLocker {
322 protected:
323  QoreRWLock* l;
324 
325 public:
326  DLLLOCAL QoreOptionalRWReadLocker(QoreRWLock* n_l) : l(n_l->tryrdlock() ? 0 : n_l) {
327  }
328 
329  DLLLOCAL QoreOptionalRWReadLocker(QoreRWLock& n_l) : l(n_l.tryrdlock() ? 0 : &n_l) {
330  }
331 
332  DLLLOCAL ~QoreOptionalRWReadLocker() {
333  if (l)
334  l->unlock();
335  }
336 
337  DLLLOCAL operator bool() const {
338  return (bool)l;
339  }
340 };
341 
342 class qore_var_rwlock_priv;
343 
344 class QoreVarRWLock {
345 private:
347  DLLLOCAL QoreVarRWLock(const QoreVarRWLock&);
349  DLLLOCAL QoreVarRWLock& operator=(const QoreVarRWLock&);
350 
351 protected:
352  qore_var_rwlock_priv* priv;
353 
354  DLLLOCAL QoreVarRWLock(qore_var_rwlock_priv* p);
355 
356 public:
357  DLLLOCAL QoreVarRWLock();
358 
360  DLLLOCAL ~QoreVarRWLock();
361 
363  DLLLOCAL void wrlock();
364 
366  DLLLOCAL int trywrlock();
367 
369  DLLLOCAL void unlock();
370 
372  DLLLOCAL void rdlock();
373 
375  DLLLOCAL int tryrdlock();
376 };
377 
378 #endif
provides a safe and exception-safe way to hold write locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:142
DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock &n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:159
DLLLOCAL int wrlock()
grabs the write lock
Definition: QoreRWLock.h:75
DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock &n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:262
DLLLOCAL void unlock()
unlocks the object and updates the locked flag, assumes that the lock is held
Definition: QoreRWLock.h:224
DLLEXPORT int gettid()
returns the current TID number
DLLLOCAL ~QoreSafeRWReadLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:211
DLLLOCAL void unlock()
unlocks the object and updates the locked flag, assumes that the lock is held
Definition: QoreRWLock.h:287
DLLLOCAL QoreSafeRWReadLocker(QoreRWLock *n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:205
DLLLOCAL QoreRWLock()
creates and initializes the lock
Definition: QoreRWLock.h:57
DLLLOCAL ~QoreRWLock()
destroys the lock
Definition: QoreRWLock.h:66
DLLLOCAL ~QoreAutoRWReadLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:132
DLLLOCAL ~QoreAutoRWWriteLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:169
bool locked
lock flag
Definition: QoreRWLock.h:258
bool locked
lock flag
Definition: QoreRWLock.h:195
DLLLOCAL QoreSafeRWReadLocker(QoreRWLock &n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:199
provides a safe and exception-safe way to hold write locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:242
DLLLOCAL int unlock()
unlocks the lock (assumes the lock is locked)
Definition: QoreRWLock.h:85
DLLLOCAL int rdlock()
grabs the read lock
Definition: QoreRWLock.h:90
provides a safe and exception-safe way to hold read locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:105
DLLLOCAL QoreAutoRWReadLocker(QoreRWLock &n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:122
provides a safe and exception-safe way to hold read locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:179
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:155
DLLLOCAL void lock()
locks the object and updates the locked flag, assumes that the lock is not already held ...
Definition: QoreRWLock.h:217
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:118
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:231
provides a simple POSIX-threads-based read-write lock
Definition: QoreRWLock.h:47
DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock *n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:268
pthread_rwlock_t m
the actual locking primitive wrapped in this class
Definition: QoreRWLock.h:50
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:294
DLLLOCAL QoreAutoRWReadLocker(QoreRWLock *n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:127
DLLLOCAL int tryrdlock()
tries to grab the read lock; does not block if unsuccessful; returns 0 if successful ...
Definition: QoreRWLock.h:95
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:164
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:255
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:192
DLLLOCAL void lock()
locks the object and updates the locked flag, assumes that the lock is not already held ...
Definition: QoreRWLock.h:280
DLLLOCAL int trywrlock()
tries to grab the write lock; does not block if unsuccessful; returns 0 if successful ...
Definition: QoreRWLock.h:80
DLLLOCAL ~QoreSafeRWWriteLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:274