Qore Programming Language - C/C++ Library  0.8.13
QoreThreadLock.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreThreadLock.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2016 Qore Technologies, s.r.o.
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef _QORE_QORETHREADLOCK_H
33 
34 #define _QORE_QORETHREADLOCK_H
35 
36 #include <pthread.h>
37 
38 #include <assert.h>
39 
40 #include <string.h>
41 #include <stdio.h>
42 #include <signal.h>
43 #include <stdlib.h>
44 
46 
50  friend class QoreCondition;
51 
52 private:
54  pthread_mutex_t ptm_lock;
55 
57  DLLLOCAL QoreThreadLock& operator=(const QoreThreadLock&);
58 
60  DLLLOCAL void init(const pthread_mutexattr_t* pma = 0) {
61 #ifndef NDEBUG
62  int rc =
63 #endif
64  pthread_mutex_init(&ptm_lock, pma);
65  assert(!rc);
66  }
67 
68 public:
69 
71  DLLLOCAL QoreThreadLock() {
72  init();
73  }
74 
76  DLLLOCAL QoreThreadLock(const pthread_mutexattr_t* ma) {
77  init(ma);
78  }
79 
81  DLLLOCAL ~QoreThreadLock() {
82  pthread_mutex_destroy(&ptm_lock);
83  }
84 
86  DLLLOCAL QoreThreadLock(const QoreThreadLock&) {
87  init();
88  }
89 
91 
93  DLLLOCAL void lock() {
94 #ifndef NDEBUG
95  int rc =
96 #endif
97  pthread_mutex_lock(&ptm_lock);
98  assert(!rc);
99  }
100 
102 
104  DLLLOCAL void unlock() {
105 #ifndef NDEBUG
106  int rc =
107 #endif
108  pthread_mutex_unlock(&ptm_lock);
109  assert(!rc);
110  }
111 
113 
116  DLLLOCAL int trylock() {
117  return pthread_mutex_trylock(&ptm_lock);
118  }
119 };
120 
122 
131 class AutoLocker {
132 private:
134  DLLLOCAL AutoLocker(const AutoLocker&);
135 
137  DLLLOCAL AutoLocker& operator=(const AutoLocker&);
138 
140  DLLLOCAL void *operator new(size_t);
141 
142 protected:
145 
146 public:
148  DLLLOCAL AutoLocker(QoreThreadLock* l) : lck(l) {
149  lck->lock();
150  }
151 
153  DLLLOCAL AutoLocker(QoreThreadLock& l) : lck(&l) {
154  lck->lock();
155  }
156 
158  DLLLOCAL ~AutoLocker() {
159  lck->unlock();
160  }
161 };
162 
164 
173 private:
175  DLLLOCAL AutoUnlocker(const AutoUnlocker&);
176 
178  DLLLOCAL AutoUnlocker& operator=(const AutoUnlocker&);
179 
181  DLLLOCAL void *operator new(size_t);
182 
183 protected:
186 
187 public:
189  DLLLOCAL AutoUnlocker(QoreThreadLock* l) : lck(l) {
190  if (lck)
191  lck->unlock();
192  }
193 
195  DLLLOCAL AutoUnlocker(QoreThreadLock& l) : lck(&l) {
196  lck->unlock();
197  }
198 
200  DLLLOCAL ~AutoUnlocker() {
201  if (lck)
202  lck->lock();
203  }
204 };
205 
206 
208 
216 class SafeLocker {
217 private:
219  DLLLOCAL SafeLocker(const SafeLocker&);
220 
222  DLLLOCAL SafeLocker& operator=(const SafeLocker&);
223 
225  DLLLOCAL void *operator new(size_t);
226 
227 protected:
230 
232  bool locked;
233 
234 public:
236  DLLLOCAL SafeLocker(QoreThreadLock* l) : lck(l) {
237  lck->lock();
238  locked = true;
239  }
240 
242  DLLLOCAL SafeLocker(QoreThreadLock& l) : lck(&l) {
243  lck->lock();
244  locked = true;
245  }
246 
248  DLLLOCAL ~SafeLocker() {
249  if (locked)
250  lck->unlock();
251  }
252 
254  DLLLOCAL void lock() {
255  assert(!locked);
256  lck->lock();
257  locked = true;
258  }
259 
261  DLLLOCAL void unlock() {
262  assert(locked);
263  locked = false;
264  lck->unlock();
265  }
266 
268  DLLLOCAL void stay_locked() {
269  assert(locked);
270  locked = false;
271  }
272 
274  DLLLOCAL void relock() {
275  assert(!locked);
276  lck->lock();
277  locked = true;
278  }
279 };
280 
282 
286 class OptLocker {
287 private:
289  DLLLOCAL OptLocker(const OptLocker&);
290 
292  DLLLOCAL OptLocker& operator=(const OptLocker&);
293 
295  DLLLOCAL void *operator new(size_t);
296 
297 protected:
300 
301 public:
303  DLLLOCAL OptLocker(QoreThreadLock* l) : lck(l) {
304  if (lck)
305  lck->lock();
306  }
307 
309  DLLLOCAL ~OptLocker() {
310  if (lck)
311  lck->unlock();
312  }
313 };
314 
315 #endif // _QORE_QORETHREADLOCK_H
DLLLOCAL ~QoreThreadLock()
destroys the lock
Definition: QoreThreadLock.h:81
DLLLOCAL int trylock()
attempts to acquire the mutex and returns the status immediately; does not block
Definition: QoreThreadLock.h:116
a thread condition class implementing a wrapper for pthread_cond_t
Definition: QoreCondition.h:45
provides a safe and exception-safe way to hold locks in Qore, only to be used on the stack...
Definition: QoreThreadLock.h:131
provides a safe and exception-safe way to hold optional locks in Qore, only to be used on the stack...
Definition: QoreThreadLock.h:286
DLLLOCAL void lock()
grabs the lock (assumes that the lock is unlocked)
Definition: QoreThreadLock.h:93
QoreThreadLock * lck
the pointer to the lock that will be managed
Definition: QoreThreadLock.h:144
DLLLOCAL void unlock()
releases the lock (assumes that the lock is locked)
Definition: QoreThreadLock.h:104
QoreThreadLock * lck
the pointer to the lock that will be managed
Definition: QoreThreadLock.h:185
bool locked
flag indicating if the lock is held or not
Definition: QoreThreadLock.h:232
provides a mutually-exclusive thread lock
Definition: QoreThreadLock.h:49
QoreThreadLock * lck
the pointer to the lock that will be managed
Definition: QoreThreadLock.h:299
DLLLOCAL QoreThreadLock()
creates the lock
Definition: QoreThreadLock.h:71
QoreThreadLock * lck
the pointer to the lock that will be managed
Definition: QoreThreadLock.h:229
provides an exception-safe way to manage locks in Qore, only to be used on the stack, cannot be dynamically allocated
Definition: QoreThreadLock.h:216
provides a safe and exception-safe way to release and re-acquire locks in Qore, only to be used on th...
Definition: QoreThreadLock.h:172