Qore Programming Language 0.8.8

- Main Page
- Related Pages
- Modules
- Classes
- Files

- File List
- File Members

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
41 class QoreThreadLock {
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:
134 QoreThreadLock *lck;
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:
174 QoreThreadLock *lck;
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:
237 QoreThreadLock *lck;
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
QoreThreadLock::~QoreThreadLock
DLLLOCAL ~QoreThreadLock()
destroys the lock
Definition: QoreThreadLock.h:73
AutoLocker::~AutoLocker
DLLLOCAL ~AutoLocker()
destroys the object and grabs the lock
Definition: QoreThreadLock.h:148
AutoLocker::AutoLocker
DLLLOCAL AutoLocker(QoreThreadLock *l)
creates the object and grabs the lock
Definition: QoreThreadLock.h:138
SafeLocker::stay_locked
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:213
QoreThreadLock::QoreThreadLock
DLLLOCAL QoreThreadLock(const pthread_mutexattr_t *ma)
creates the lock with the given attributes
Definition: QoreThreadLock.h:68
QoreThreadLock::QoreThreadLock
DLLLOCAL QoreThreadLock(const QoreThreadLock &)
creates a new object (not based on the original lock status)
Definition: QoreThreadLock.h:78
SafeLocker::SafeLocker
DLLLOCAL SafeLocker(QoreThreadLock &l)
creates the object and grabs the lock
Definition: QoreThreadLock.h:187
QoreThreadLock::trylock
DLLLOCAL int trylock()
attempts to acquire the mutex and returns the status immediately; does not block
Definition: QoreThreadLock.h:108
AutoLocker::AutoLocker
DLLLOCAL AutoLocker(QoreThreadLock &l)
creates the object and grabs the lock
Definition: QoreThreadLock.h:143
QoreCondition
a thread condition class implementing a wrapper for pthread_cond_t
Definition: QoreCondition.h:37
SafeLocker::unlock
DLLLOCAL void unlock()
unlocks the object and updates the locked flag, assumes that the lock is held
Definition: QoreThreadLock.h:206
AutoLocker
provides a safe and exception-safe way to hold locks in Qore, only to be used on the stack...
Definition: QoreThreadLock.h:121
OptLocker
provides a safe and exception-safe way to hold optional locks in Qore, only to be used on the stack...
Definition: QoreThreadLock.h:224
QoreThreadLock::lock
DLLLOCAL void lock()
grabs the lock (assumes that the lock is unlocked)
Definition: QoreThreadLock.h:85
AutoLocker::lck
QoreThreadLock * lck
the pointer to the lock that will be managed
Definition: QoreThreadLock.h:134
QoreThreadLock::unlock
DLLLOCAL void unlock()
releases the lock (assumes that the lock is locked)
Definition: QoreThreadLock.h:96
OptLocker::OptLocker
DLLLOCAL OptLocker(QoreThreadLock *l)
creates the object and grabs the lock if the argument is not NULL
Definition: QoreThreadLock.h:241
SafeLocker::~SafeLocker
DLLLOCAL ~SafeLocker()
destroys the object and unlocks the lock if it&#39;s held
Definition: QoreThreadLock.h:193
SafeLocker::locked
bool locked
flag indicating if the lock is held or not
Definition: QoreThreadLock.h:177
QoreThreadLock
provides a mutually-exclusive thread lock
Definition: QoreThreadLock.h:41
SafeLocker::SafeLocker
DLLLOCAL SafeLocker(QoreThreadLock *l)
creates the object and grabs the lock
Definition: QoreThreadLock.h:181
OptLocker::lck
QoreThreadLock * lck
the pointer to the lock that will be managed
Definition: QoreThreadLock.h:237
QoreThreadLock::QoreThreadLock
DLLLOCAL QoreThreadLock()
creates the lock
Definition: QoreThreadLock.h:63
SafeLocker::lck
QoreThreadLock * lck
the pointer to the lock that will be managed
Definition: QoreThreadLock.h:174
SafeLocker::lock
DLLLOCAL void lock()
locks the object and updates the locked flag, assumes that the lock is not already held ...
Definition: QoreThreadLock.h:199
SafeLocker
provides an exception-safe way to manage locks in Qore, only to be used on the stack, cannot be dynamically allocated
Definition: QoreThreadLock.h:161
OptLocker::~OptLocker
DLLLOCAL ~OptLocker()
releases the lock if there is a lock pointer being managed and destroys the object ...
Definition: QoreThreadLock.h:247