Qore Programming Language  1.12.0
VRMutex.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  VRMutex.h
4 
5  recursive lock object
6 
7  Qore Programming Language
8 
9  Copyright (C) 2003 - 2022 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_VRMUTEX_H
35 
36 #define _QORE_VRMUTEX_H
37 
38 #include "qore/intern/AbstractSmartLock.h"
39 #include "qore/intern/VLock.h"
40 
41 // reentrant thread lock using tiered locking and deadlock detection infrastructure
42 class VRMutex : public AbstractSmartLock {
43 public:
44  DLLLOCAL VRMutex();
45 
46  // grabs the lock recursively, returns >= 0 for OK, -1 for error
47  DLLLOCAL int enter(ExceptionSink *xsink);
48 
49  // releases the lock, returns 0 if the lock is unlocked, -1 if there are still more calls to exit
50  DLLLOCAL int exit();
51 
52  DLLLOCAL virtual const char* getName() const { return "VRMutex"; }
53 
54  DLLLOCAL int get_count() const {
55  return count;
56  }
57 
58 private:
59  int count = 0;
60 
61  DLLLOCAL virtual int releaseImpl();
62  DLLLOCAL virtual int releaseImpl(ExceptionSink *xsink);
63  DLLLOCAL virtual int grabImpl(int mtid, VLock *nvl, ExceptionSink *xsink, int64 timeout_ms = 0);
64  DLLLOCAL virtual int tryGrabImpl(int mtid, VLock *nvl);
65  DLLLOCAL virtual int cleanupImpl();
66 };
67 
68 class VRMutexOptionalLockHelper {
69 public:
70  DLLLOCAL VRMutexOptionalLockHelper(VRMutex* vm, ExceptionSink* xsink) : m(vm && vm->enter(xsink) >= 0 ? vm : 0) {
71  //printd(5, "VRMutexOptionalLockHelper::VRMutexOptionalLockHelper() vm: %p m: %p\n", vm, m);
72  }
73 
74  DLLLOCAL ~VRMutexOptionalLockHelper() {
75  if (m)
76  m->exit();
77  }
78 
79 protected:
80  VRMutex* m;
81 };
82 
83 #endif
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
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