Qore Programming Language  1.12.0
VLock.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  VLock.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2022 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 
42 typedef std::map<int, class VLock *> vlock_map_t;
43 
44 // testing shows that a vector is slightly faster than a deque for this usage
45 // and must faster than a list
46 typedef std::vector<AbstractSmartLock *> abstract_lock_list_t;
47 
48 // for tracking locks per thread and detecting deadlocks
49 class VLock : protected abstract_lock_list_t
50 {
51  private:
52  AbstractSmartLock *waiting_on; // the lock this object is waiting on
53  int tid;
54 
55  // not implemented
56  VLock(const VLock&);
57  VLock& operator=(const VLock&);
58 
59  public:
60  DLLLOCAL VLock(int n_tid);
61  DLLLOCAL ~VLock();
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, class VLock *vl, class ExceptionSink *xsink, int timeout_ms = 0);
69  // for smart locks using an alternate condition variable
70  DLLLOCAL int waitOn(AbstractSmartLock *asl, class QoreCondition *cond, class VLock *vl, class 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, class 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 };
79 
80 #endif
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
a thread condition class implementing a wrapper for pthread_cond_t
Definition: QoreCondition.h:45