Qore Programming Language  0.8.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreThreadLock.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreThreadLock.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2013 David Nichols, all rights reserved
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23 
24 #ifndef _QORE_QORETHREADLOCK_H
25 
26 #define _QORE_QORETHREADLOCK_H
27 
28 #include <pthread.h>
29 
30 #include <assert.h>
31 
32 #include <string.h>
33 #include <stdio.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 
38 
42  friend class QoreCondition;
43 
44 private:
46  pthread_mutex_t ptm_lock;
47 
49  DLLLOCAL QoreThreadLock& operator=(const QoreThreadLock&);
50 
52  DLLLOCAL void init(const pthread_mutexattr_t *pma = 0) {
53 #ifndef NDEBUG
54  int rc =
55 #endif
56  pthread_mutex_init(&ptm_lock, pma);
57  assert(!rc);
58  }
59 
60 public:
61 
63  DLLLOCAL QoreThreadLock() {
64  init();
65  }
66 
68  DLLLOCAL QoreThreadLock(const pthread_mutexattr_t *ma) {
69  init(ma);
70  }
71 
73  DLLLOCAL ~QoreThreadLock() {
74  pthread_mutex_destroy(&ptm_lock);
75  }
76 
78  DLLLOCAL QoreThreadLock(const QoreThreadLock&) {
79  init();
80  }
81 
83 
85  DLLLOCAL void lock() {
86 #ifndef NDEBUG
87  int rc =
88 #endif
89  pthread_mutex_lock(&ptm_lock);
90  assert(!rc);
91  }
92 
94 
96  DLLLOCAL void unlock() {
97 #ifndef NDEBUG
98  int rc =
99 #endif
100  pthread_mutex_unlock(&ptm_lock);
101  assert(!rc);
102  }
103 
105 
108  DLLLOCAL int trylock() {
109  return pthread_mutex_trylock(&ptm_lock);
110  }
111 };
112 
114 
123 class AutoLocker {
124 private:
126  DLLLOCAL AutoLocker(const AutoLocker&);
127 
129  DLLLOCAL AutoLocker& operator=(const AutoLocker&);
130 
132  DLLLOCAL void *operator new(size_t);
133 
134 protected:
137 
138 public:
140  DLLLOCAL AutoLocker(QoreThreadLock *l) : lck(l) {
141  lck->lock();
142  }
143 
145  DLLLOCAL AutoLocker(QoreThreadLock &l) : lck(&l) {
146  lck->lock();
147  }
148 
150  DLLLOCAL ~AutoLocker() {
151  lck->unlock();
152  }
153 };
154 
156 
165 private:
167  DLLLOCAL AutoUnlocker(const AutoUnlocker&);
168 
170  DLLLOCAL AutoUnlocker& operator=(const AutoUnlocker&);
171 
173  DLLLOCAL void *operator new(size_t);
174 
175 protected:
178 
179 public:
181  DLLLOCAL AutoUnlocker(QoreThreadLock *l) : lck(l) {
182  if (lck)
183  lck->unlock();
184  }
185 
187  DLLLOCAL AutoUnlocker(QoreThreadLock &l) : lck(&l) {
188  lck->unlock();
189  }
190 
192  DLLLOCAL ~AutoUnlocker() {
193  if (lck)
194  lck->lock();
195  }
196 };
197 
198 
200 
208 class SafeLocker {
209 private:
211  DLLLOCAL SafeLocker(const SafeLocker&);
212 
214  DLLLOCAL SafeLocker& operator=(const SafeLocker&);
215 
217  DLLLOCAL void *operator new(size_t);
218 
219 protected:
222 
224  bool locked;
225 
226 public:
228  DLLLOCAL SafeLocker(QoreThreadLock *l) : lck(l) {
229  lck->lock();
230  locked = true;
231  }
232 
234  DLLLOCAL SafeLocker(QoreThreadLock &l) : lck(&l) {
235  lck->lock();
236  locked = true;
237  }
238 
240  DLLLOCAL ~SafeLocker() {
241  if (locked)
242  lck->unlock();
243  }
244 
246  DLLLOCAL void lock() {
247  assert(!locked);
248  lck->lock();
249  locked = true;
250  }
251 
253  DLLLOCAL void unlock() {
254  assert(locked);
255  locked = false;
256  lck->unlock();
257  }
258 
260  DLLLOCAL void stay_locked() {
261  assert(locked);
262  locked = false;
263  }
264 };
265 
267 
271 class OptLocker {
272 private:
274  DLLLOCAL OptLocker(const OptLocker&);
275 
277  DLLLOCAL OptLocker& operator=(const OptLocker&);
278 
280  DLLLOCAL void *operator new(size_t);
281 
282 protected:
285 
286 public:
288  DLLLOCAL OptLocker(QoreThreadLock *l) : lck(l) {
289  if (lck)
290  lck->lock();
291  }
292 
294  DLLLOCAL ~OptLocker() {
295  if (lck)
296  lck->unlock();
297  }
298 };
299 
300 #endif // _QORE_QORETHREADLOCK_H
DLLLOCAL ~QoreThreadLock()
destroys the lock
Definition: QoreThreadLock.h:73
DLLLOCAL ~AutoUnlocker()
grabs the lock and destroys the object
Definition: QoreThreadLock.h:192
DLLLOCAL ~AutoLocker()
destroys the object and releases the lock
Definition: QoreThreadLock.h:150
DLLLOCAL AutoLocker(QoreThreadLock *l)
creates the object and grabs the lock
Definition: QoreThreadLock.h:140
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: QoreThreadLock.h:260
DLLLOCAL QoreThreadLock(const pthread_mutexattr_t *ma)
creates the lock with the given attributes
Definition: QoreThreadLock.h:68
DLLLOCAL QoreThreadLock(const QoreThreadLock &)
creates a new object (not based on the original lock status)
Definition: QoreThreadLock.h:78
DLLLOCAL AutoUnlocker(QoreThreadLock *l)
creates the object and releases the lock
Definition: QoreThreadLock.h:181
DLLLOCAL SafeLocker(QoreThreadLock &l)
creates the object and grabs the lock
Definition: QoreThreadLock.h:234
DLLLOCAL int trylock()
attempts to acquire the mutex and returns the status immediately; does not block
Definition: QoreThreadLock.h:108
DLLLOCAL AutoLocker(QoreThreadLock &l)
creates the object and grabs the lock
Definition: QoreThreadLock.h:145
a thread condition class implementing a wrapper for pthread_cond_t
Definition: QoreCondition.h:37
DLLLOCAL void unlock()
unlocks the object and updates the locked flag, assumes that the lock is held
Definition: QoreThreadLock.h:253
provides a safe and exception-safe way to hold locks in Qore, only to be used on the stack...
Definition: QoreThreadLock.h:123
provides a safe and exception-safe way to hold optional locks in Qore, only to be used on the stack...
Definition: QoreThreadLock.h:271
DLLLOCAL void lock()
grabs the lock (assumes that the lock is unlocked)
Definition: QoreThreadLock.h:85
QoreThreadLock * lck
the pointer to the lock that will be managed
Definition: QoreThreadLock.h:136
DLLLOCAL void unlock()
releases the lock (assumes that the lock is locked)
Definition: QoreThreadLock.h:96
DLLLOCAL AutoUnlocker(QoreThreadLock &l)
creates the object and releases the lock
Definition: QoreThreadLock.h:187
QoreThreadLock * lck
the pointer to the lock that will be managed
Definition: QoreThreadLock.h:177
DLLLOCAL OptLocker(QoreThreadLock *l)
creates the object and grabs the lock if the argument is not NULL
Definition: QoreThreadLock.h:288
DLLLOCAL ~SafeLocker()
destroys the object and unlocks the lock if it's held
Definition: QoreThreadLock.h:240
bool locked
flag indicating if the lock is held or not
Definition: QoreThreadLock.h:224
provides a mutually-exclusive thread lock
Definition: QoreThreadLock.h:41
DLLLOCAL SafeLocker(QoreThreadLock *l)
creates the object and grabs the lock
Definition: QoreThreadLock.h:228
QoreThreadLock * lck
the pointer to the lock that will be managed
Definition: QoreThreadLock.h:284
DLLLOCAL QoreThreadLock()
creates the lock
Definition: QoreThreadLock.h:63
QoreThreadLock * lck
the pointer to the lock that will be managed
Definition: QoreThreadLock.h:221
DLLLOCAL void lock()
locks the object and updates the locked flag, assumes that the lock is not already held ...
Definition: QoreThreadLock.h:246
provides an exception-safe way to manage locks in Qore, only to be used on the stack, cannot be dynamically allocated
Definition: QoreThreadLock.h:208
provides a safe and exception-safe way to release and re-acquire locks in Qore, only to be used on th...
Definition: QoreThreadLock.h:164
DLLLOCAL ~OptLocker()
releases the lock if there is a lock pointer being managed and destroys the object ...
Definition: QoreThreadLock.h:294