Qore Programming Language 1.19.2
Loading...
Searching...
No Matches
VLock.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 VLock.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2023 David Nichols
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_VLOCK_H
33
34#define _QORE_VLOCK_H
35
36#include <qore/Qore.h>
37#include "qore/intern/AbstractSmartLock.h"
38
39#include <vector>
40#include <map>
41#include <atomic>
42
43typedef std::map<int, class VLock*> vlock_map_t;
44
45// testing shows that a vector is slightly faster than a deque for this usage
46// and must faster than a list
47typedef std::vector<AbstractSmartLock*> abstract_lock_list_t;
48
49// forward references
50class QoreCondition;
51
52// for tracking locks per thread and detecting deadlocks
53class VLock : protected abstract_lock_list_t {
54public:
55 DLLLOCAL VLock(int tid);
56
57 DLLLOCAL ~VLock() {
58 //printd(5, "VLock::~VLock() this=%p\n", this);
59 assert(begin() == end());
60 }
61
62 DLLLOCAL void push(AbstractSmartLock* g);
63 DLLLOCAL int pop(AbstractSmartLock* asl);
64 DLLLOCAL void del();
65 DLLLOCAL AbstractSmartLock* find(AbstractSmartLock* g) const;
66
67 // for blocking smart locks with deadlock detection
68 DLLLOCAL int waitOn(AbstractSmartLock* asl, VLock* vl, ExceptionSink* xsink, int timeout_ms = 0);
69 // for smart locks using an alternate condition variable
70 DLLLOCAL int waitOn(AbstractSmartLock* asl, QoreCondition* cond, VLock* vl, ExceptionSink* xsink, int timeout_ms = 0);
71 // for smart locks that can be held by more than one thread
72 DLLLOCAL int waitOn(AbstractSmartLock* asl, vlock_map_t& vmap, ExceptionSink* xsink, int timeout_ms = 0);
73 DLLLOCAL int getTID() const { return tid; }
74
75#ifdef DEBUG
76 DLLLOCAL void show(class VLock* nvl) const;
77#endif
78
79private:
80 std::atomic<AbstractSmartLock*> waiting_on; // the lock this object is waiting on
81 int tid;
82
83 // not implemented
84 VLock(const VLock&);
85 VLock& operator=(const VLock&);
86};
87
88#endif
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