Qore Programming Language  1.7.0
AutoVLock.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  AutoVLock.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2022 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_AUTOVLOCK_H
33 
34 #define _QORE_AUTOVLOCK_H
35 
36 class AbstractSmartLock;
37 
38 hashdecl QLckPtr {
39 public:
40  DLLLOCAL QLckPtr() : rwl(0) {
41  }
42 
43  DLLLOCAL void set(QoreVarRWLock* n_rwl) {
44  rwl = n_rwl;
45  }
46 
47  DLLLOCAL QoreVarRWLock* getRWL() const {
48  return rwl;
49  }
50 
51  DLLLOCAL bool isSet() const {
52  return rwl;
53  }
54 
55  DLLLOCAL void unlockAndClear() {
56  if (rwl) {
57  unlockIntern();
58  rwl = 0;
59  }
60  }
61 
62  DLLLOCAL void clear() {
63  assert(rwl);
64  rwl = 0;
65  }
66 
67 protected:
68  DLLLOCAL void unlockIntern() {
69  assert(rwl);
70  rwl->unlock();
71  }
72 
73 private:
74  QoreVarRWLock* rwl;
75 };
76 
78 
80 class AutoVLock {
81 private:
82  // pointer to lock currently held
83  QLckPtr lock;
84 
85  // pointer to object to dereference
86  QoreObject* o;
87 
88 public:
89  // pointer to ExceptionSink object for use with object notifications
90  ExceptionSink* xsink;
91 
92 private:
94  hashdecl qore_avl_private* priv;
95 
97  DLLLOCAL AutoVLock(const AutoVLock&) = delete;
99  DLLLOCAL AutoVLock& operator=(const AutoVLock&) = delete;
101  DLLLOCAL void* operator new(size_t) = delete;
102 
103 public:
105 
107  DLLEXPORT AutoVLock(ExceptionSink* n_xsink);
108 
110  DLLLOCAL AutoVLock(AutoVLock&& o) = default;
111 
113  DLLEXPORT ~AutoVLock();
114 
116  DLLEXPORT operator bool() const;
117 
119  DLLEXPORT void del();
120 
122  DLLLOCAL void set(QoreVarRWLock* n_rwl);
123 
125  DLLLOCAL void set(QoreObject* n_o, QoreVarRWLock* n_rwl);
126 
128  DLLLOCAL QoreVarRWLock* getRWL() const;
129 
131  DLLLOCAL QoreObject* getObject() const;
132 
134  DLLLOCAL void clear();
135 
137 
140  DLLLOCAL void addMemberNotification(QoreObject* o, const char* member);
141 };
142 
143 #endif
AutoVLock is a container for safely managing global variable and object lock handovers,...
Definition: AutoVLock.h:80
DLLEXPORT void del()
manually releases the lock currently held
DLLLOCAL void clear()
leaves the lock locked and the object referenced and clears the object and lock pointers
DLLEXPORT ~AutoVLock()
releases all locks held and destroys the container
DLLLOCAL QoreVarRWLock * getRWL() const
gets the current read-write lock
DLLLOCAL QoreObject * getObject() const
gets the current object
DLLLOCAL void set(QoreVarRWLock *n_rwl)
sets the current lock
DLLLOCAL void addMemberNotification(QoreObject *o, const char *member)
adds an object member notification entry, internal-only
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:60