Qore Programming Language 2.0.0
Loading...
Searching...
No Matches
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 - 2024 Qore Technologies, s.r.o.
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
40extern int q_gettid() noexcept;
41#endif
42
44
48protected:
50 pthread_rwlock_t m;
51
53 DLLLOCAL QoreRWLock& operator=(const QoreRWLock&);
54
55public:
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
106private:
109
111 DLLLOCAL QoreAutoRWReadLocker& operator=(const QoreAutoRWReadLocker&);
112
114 DLLLOCAL void *operator new(size_t);
115
116protected:
119
120public:
122 DLLLOCAL QoreAutoRWReadLocker(QoreRWLock &n_l) : l(&n_l) {
123 l->rdlock();
124 }
125
127 DLLLOCAL QoreAutoRWReadLocker(QoreRWLock *n_l) : l(n_l) {
128 if (l)
129 l->rdlock();
130 }
131
134 if (l)
135 l->unlock();
136 }
137};
138
140
145private:
148
150 DLLLOCAL QoreAutoRWWriteLocker& operator=(const QoreAutoRWWriteLocker&);
151
153 DLLLOCAL void *operator new(size_t);
154
155protected:
158
159public:
161 DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock &n_l) : l(&n_l) {
162 l->wrlock();
163 }
164
166 DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock *n_l) : l(n_l) {
167 if (l)
168 l->wrlock();
169 }
170
173 if (l)
174 l->unlock();
175 }
176};
177
179
184public:
186 DLLLOCAL QoreSafeRWReadLocker() : l(nullptr), locked(false) {
187 }
188
190 DLLLOCAL QoreSafeRWReadLocker(QoreRWLock& n_l) : l(&n_l) {
191 l->rdlock();
192 locked = true;
193 }
194
196 DLLLOCAL QoreSafeRWReadLocker(QoreRWLock* n_l) : l(n_l) {
197 l->rdlock();
198 locked = true;
199 }
200
203 if (locked) {
204 l->unlock();
205 }
206 }
207
209 DLLLOCAL void lock() {
210 assert(!locked);
211 assert(l);
212 l->rdlock();
213 locked = true;
214 }
215
217 DLLLOCAL void unlock() {
218 assert(locked);
219 assert(l);
220 locked = false;
221 l->unlock();
222 }
223
225 DLLLOCAL void stay_locked() {
226 assert(locked);
227 assert(l);
228 locked = false;
229 }
230
232 DLLLOCAL void handoffTo(QoreRWLock& n) {
233 if (l) {
234 unlock();
235 }
236 l = &n;
237 lock();
238 }
239
240protected:
243
245 bool locked;
246
247private:
249 DLLLOCAL QoreSafeRWReadLocker(const QoreSafeRWReadLocker&) = delete;
250
252 DLLLOCAL QoreSafeRWReadLocker& operator=(const QoreSafeRWReadLocker&) = delete;
253
255 DLLLOCAL void* operator new(size_t) = delete;
256};
257
259
264private:
267
269 DLLLOCAL QoreSafeRWWriteLocker& operator=(const QoreSafeRWWriteLocker&);
270
272 DLLLOCAL void *operator new(size_t);
273
274protected:
277
279 bool locked;
280
281public:
283 DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock &n_l) : l(&n_l) {
284 l->wrlock();
285 locked = true;
286 }
287
289 DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock *n_l) : l(n_l) {
290 l->wrlock();
291 locked = true;
292 }
293
296 if (locked)
297 l->unlock();
298 }
299
301 DLLLOCAL void lock() {
302 assert(!locked);
303 l->wrlock();
304 locked = true;
305 }
306
308 DLLLOCAL void unlock() {
309 assert(locked);
310 locked = false;
311 l->unlock();
312 }
313
315 DLLLOCAL void stay_locked() {
316 assert(locked);
317 locked = false;
318 }
319};
320
321class QoreOptionalRWWriteLocker {
322protected:
323 QoreRWLock* l;
324
325public:
326 DLLLOCAL QoreOptionalRWWriteLocker(QoreRWLock* n_l) : l(n_l->trywrlock() ? 0 : n_l) {
327 }
328
329 DLLLOCAL QoreOptionalRWWriteLocker(QoreRWLock& n_l) : l(n_l.trywrlock() ? 0 : &n_l) {
330 }
331
332 DLLLOCAL ~QoreOptionalRWWriteLocker() {
333 if (l)
334 l->unlock();
335 }
336
337 DLLLOCAL operator bool() const {
338 return (bool)l;
339 }
340};
341
342class QoreOptionalRWReadLocker {
343protected:
344 QoreRWLock* l;
345
346public:
347 DLLLOCAL QoreOptionalRWReadLocker(QoreRWLock* n_l) : l(n_l->tryrdlock() ? 0 : n_l) {
348 }
349
350 DLLLOCAL QoreOptionalRWReadLocker(QoreRWLock& n_l) : l(n_l.tryrdlock() ? 0 : &n_l) {
351 }
352
353 DLLLOCAL ~QoreOptionalRWReadLocker() {
354 if (l)
355 l->unlock();
356 }
357
358 DLLLOCAL operator bool() const {
359 return (bool)l;
360 }
361};
362
363class qore_var_rwlock_priv;
364
365class QoreVarRWLock {
366private:
368 DLLLOCAL QoreVarRWLock(const QoreVarRWLock&);
370 DLLLOCAL QoreVarRWLock& operator=(const QoreVarRWLock&);
371
372protected:
373 qore_var_rwlock_priv* priv;
374
375 DLLLOCAL QoreVarRWLock(qore_var_rwlock_priv* p);
376
377public:
378 DLLLOCAL QoreVarRWLock();
379
381 DLLLOCAL ~QoreVarRWLock();
382
384 DLLLOCAL void wrlock();
385
387 DLLLOCAL int trywrlock();
388
390 DLLLOCAL void unlock();
391
393 DLLLOCAL void rdlock();
394
396 DLLLOCAL int tryrdlock();
397};
398
399#endif
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. If parameter is null then no function is performed.
Definition QoreRWLock.h:127
DLLLOCAL ~QoreAutoRWReadLocker()
destroys the object and releases the lock
Definition QoreRWLock.h:133
QoreRWLock * l
the pointer to the lock that will be managed
Definition QoreRWLock.h:118
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 write locks in Qore, only to be used on the stack,...
Definition QoreRWLock.h:144
DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock &n_l)
creates the object and grabs the write lock
Definition QoreRWLock.h:161
DLLLOCAL ~QoreAutoRWWriteLocker()
destroys the object and releases the lock
Definition QoreRWLock.h:172
QoreRWLock * l
the pointer to the lock that will be managed
Definition QoreRWLock.h:157
DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock *n_l)
creates the object and grabs the write lock. If parameter is null then no function is performed.
Definition QoreRWLock.h:166
provides a simple POSIX-threads-based read-write lock
Definition QoreRWLock.h:47
pthread_rwlock_t m
the actual locking primitive wrapped in this class
Definition QoreRWLock.h:50
DLLLOCAL ~QoreRWLock()
destroys the lock
Definition QoreRWLock.h:66
DLLLOCAL int wrlock()
grabs the write lock
Definition QoreRWLock.h:75
DLLLOCAL QoreRWLock()
creates and initializes the lock
Definition QoreRWLock.h:57
DLLLOCAL int trywrlock()
tries to grab the write lock; does not block if unsuccessful; returns 0 if successful
Definition QoreRWLock.h:80
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
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 int tryrdlock()
tries to grab the read lock; does not block if unsuccessful; returns 0 if successful
Definition QoreRWLock.h:95
provides a safe and exception-safe way to hold read locks in Qore, only to be used on the stack,...
Definition QoreRWLock.h:183
DLLLOCAL void handoffTo(QoreRWLock &n)
Handoff to another read lock.
Definition QoreRWLock.h:232
DLLLOCAL QoreSafeRWReadLocker()
creates an empty object
Definition QoreRWLock.h:186
DLLLOCAL QoreSafeRWReadLocker(QoreRWLock *n_l)
creates the object and grabs the read lock
Definition QoreRWLock.h:196
DLLLOCAL void unlock()
unlocks the object and updates the locked flag, assumes that the lock is held
Definition QoreRWLock.h:217
bool locked
lock flag
Definition QoreRWLock.h:245
DLLLOCAL ~QoreSafeRWReadLocker()
destroys the object and releases the lock
Definition QoreRWLock.h:202
DLLLOCAL void lock()
locks the object and updates the locked flag, assumes that the lock is not already held
Definition QoreRWLock.h:209
DLLLOCAL QoreSafeRWReadLocker(QoreRWLock &n_l)
creates the object and grabs the read lock
Definition QoreRWLock.h:190
QoreRWLock * l
the pointer to the lock that will be managed
Definition QoreRWLock.h:242
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:225
provides a safe and exception-safe way to hold write locks in Qore, only to be used on the stack,...
Definition QoreRWLock.h:263
DLLLOCAL void unlock()
unlocks the object and updates the locked flag, assumes that the lock is held
Definition QoreRWLock.h:308
DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock &n_l)
creates the object and grabs the write lock
Definition QoreRWLock.h:283
QoreRWLock * l
the pointer to the lock that will be managed
Definition QoreRWLock.h:276
DLLLOCAL void lock()
locks the object and updates the locked flag, assumes that the lock is not already held
Definition QoreRWLock.h:301
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:315
DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock *n_l)
creates the object and grabs the write lock
Definition QoreRWLock.h:289
DLLLOCAL ~QoreSafeRWWriteLocker()
destroys the object and releases the lock
Definition QoreRWLock.h:295
bool locked
lock flag
Definition QoreRWLock.h:279
DLLEXPORT int q_gettid() noexcept
returns the current TID number