Qore Programming Language  0.8.7
 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 
121 class AutoLocker {
122 private:
124  DLLLOCAL AutoLocker(const AutoLocker&);
125 
127  DLLLOCAL AutoLocker& operator=(const AutoLocker&);
128 
130  DLLLOCAL void *operator new(size_t);
131 
132 protected:
135 
136 public:
138  DLLLOCAL AutoLocker(QoreThreadLock *l) : lck(l) {
139  lck->lock();
140  }
141 
143  DLLLOCAL AutoLocker(QoreThreadLock &l) : lck(&l) {
144  lck->lock();
145  }
146 
148  DLLLOCAL ~AutoLocker() {
149  lck->unlock();
150  }
151 };
152 
154 
161 class SafeLocker {
162 private:
164  DLLLOCAL SafeLocker(const SafeLocker&);
165 
167  DLLLOCAL SafeLocker& operator=(const SafeLocker&);
168 
170  DLLLOCAL void *operator new(size_t);
171 
172 protected:
175 
177  bool locked;
178 
179 public:
181  DLLLOCAL SafeLocker(QoreThreadLock *l) : lck(l) {
182  lck->lock();
183  locked = true;
184  }
185 
187  DLLLOCAL SafeLocker(QoreThreadLock &l) : lck(&l) {
188  lck->lock();
189  locked = true;
190  }
191 
193  DLLLOCAL ~SafeLocker() {
194  if (locked)
195  lck->unlock();
196  }
197 
199  DLLLOCAL void lock() {
200  assert(!locked);
201  lck->lock();
202  locked = true;
203  }
204 
206  DLLLOCAL void unlock() {
207  assert(locked);
208  locked = false;
209  lck->unlock();
210  }
211 
213  DLLLOCAL void stay_locked() {
214  assert(locked);
215  locked = false;
216  }
217 };
218 
220 
224 class OptLocker {
225 private:
227  DLLLOCAL OptLocker(const OptLocker&);
228 
230  DLLLOCAL OptLocker& operator=(const OptLocker&);
231 
233  DLLLOCAL void *operator new(size_t);
234 
235 protected:
238 
239 public:
241  DLLLOCAL OptLocker(QoreThreadLock *l) : lck(l) {
242  if (lck)
243  lck->lock();
244  }
245 
247  DLLLOCAL ~OptLocker() {
248  if (lck)
249  lck->unlock();
250  }
251 };
252 
253 #endif // _QORE_QORETHREADLOCK_H