Qore Programming Language 1.19.2
Loading...
Searching...
No Matches
AbstractSmartLock.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 AbstractSmartLock.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2023 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_ABSTRACTSMARTLOCK_H
33
34#define _QORE_ABSTRACTSMARTLOCK_H
35
36#include <qore/Qore.h>
37#include <qore/QoreThreadLock.h>
38#include <qore/QoreCondition.h>
39#include <qore/AbstractThreadResource.h>
40
41class VLock;
42
43class QoreCondition;
44
45class AbstractSmartLock : public AbstractThreadResource {
46public:
47 mutable QoreThreadLock asl_lock;
48 QoreCondition asl_cond;
49
50 DLLLOCAL AbstractSmartLock() : vl(NULL), tid(-1), waiting(0) {}
51 DLLLOCAL virtual ~AbstractSmartLock() {}
52 DLLLOCAL void destructor(ExceptionSink *xsink);
53 DLLLOCAL virtual void cleanup(ExceptionSink *xsink);
54
55 DLLLOCAL int grab(ExceptionSink *xsink, int64 timeout_ms = 0);
56 DLLLOCAL int tryGrab();
57 DLLLOCAL int release();
58 DLLLOCAL int release(ExceptionSink *xsink);
59
60 DLLLOCAL int self_wait(int64 timeout_ms) {
61 return timeout_ms ? asl_cond.wait(&asl_lock, timeout_ms) : asl_cond.wait(&asl_lock);
62 }
63
64 DLLLOCAL int self_wait(QoreCondition *cond, int64 timeout_ms = 0) {
65 return timeout_ms ? cond->wait(&asl_lock, timeout_ms) : cond->wait(&asl_lock);
66 }
67
68 DLLLOCAL int extern_wait(QoreCondition *cond, ExceptionSink *xsink, int64 timeout_ms = 0);
69
70 DLLLOCAL int get_tid() const { return tid; }
71 DLLLOCAL int get_waiting() const { return waiting; }
72 DLLLOCAL virtual const char *getName() const = 0;
73 DLLLOCAL int cond_count(QoreCondition *cond) const {
74 AutoLocker al(&asl_lock);
75 cond_map_t::const_iterator i = cmap.find(cond);
76 return i != cmap.end() ? i->second : 0;
77 }
78
79protected:
80 enum lock_status_e { Lock_Deleted = -2, Lock_Unlocked = -1 };
81
82 VLock *vl;
83 int tid, waiting;
84 cond_map_t cmap; // map of condition variables to wait counts
85
86 virtual int releaseImpl() = 0;
87 virtual int releaseImpl(ExceptionSink *xsink) = 0;
88 virtual int grabImpl(int mtid, VLock *nvl, ExceptionSink *xsink, int64 timeout_ms = 0) = 0;
89 virtual int tryGrabImpl(int mtid, VLock *nvl) = 0;
90
91 DLLLOCAL virtual int externWaitImpl(int mtid, QoreCondition *cond, ExceptionSink *xsink, int64 timeout_ms = 0);
92 DLLLOCAL virtual void destructorImpl(ExceptionSink *xsink);
93 DLLLOCAL virtual void signalAllImpl();
94 DLLLOCAL virtual void signalImpl();
95 // returns 0 = OK, -1 = lock released, throw exception
96 DLLLOCAL virtual int cleanupImpl();
97
98 DLLLOCAL void mark_and_push(int mtid, VLock *nvl);
99 DLLLOCAL void release_and_signal();
100 DLLLOCAL void grab_intern(int mtid, VLock *nvl);
101 DLLLOCAL void release_intern();
102 DLLLOCAL int verify_wait_unlocked(int mtid, ExceptionSink *xsink);
103};
104
105#endif
base class for saving data using Qore's thread resource management system
Definition: AbstractThreadResource.h:51
virtual void cleanup(ExceptionSink *xsink)=0
this function is called when a thread terminates and a thread resource is still allocated to the thre...
provides a safe and exception-safe way to hold locks in Qore, only to be used on the stack,...
Definition: QoreThreadLock.h:136
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
a thread condition class implementing a wrapper for pthread_cond_t
Definition: QoreCondition.h:45
DLLEXPORT int wait(pthread_mutex_t *m)
blocks a thread on a mutex until the condition is signaled
provides a mutually-exclusive thread lock
Definition: QoreThreadLock.h:49
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260