Qore Programming Language  0.9.3
AutoVLock.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  AutoVLock.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2017 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 private:
40  QoreVarRWLock* rwl;
41 
42 protected:
43  DLLLOCAL void unlockIntern() {
44  assert(rwl);
45  rwl->unlock();
46  }
47 
48 public:
49  DLLLOCAL QLckPtr() : rwl(0) {
50  }
51 
52  DLLLOCAL void set(QoreVarRWLock* n_rwl) {
53  rwl = n_rwl;
54  }
55 
56  DLLLOCAL QoreVarRWLock* getRWL() const {
57  return rwl;
58  }
59 
60  DLLLOCAL bool isSet() const {
61  return rwl;
62  }
63 
64  DLLLOCAL void unlockAndClear() {
65  if (rwl) {
66  unlockIntern();
67  rwl = 0;
68  }
69  }
70 
71  DLLLOCAL void clear() {
72  assert(rwl);
73  rwl = 0;
74  }
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  DLLEXPORT 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 ~AutoVLock()
releases all locks held and destroys the container
DLLEXPORT void del()
manually releases the lock currently held
DLLLOCAL void set(QoreVarRWLock *n_rwl)
sets the current lock
DLLLOCAL void clear()
leaves the lock locked and the object referenced and clears the object and lock pointers
DLLLOCAL void addMemberNotification(QoreObject *o, const char *member)
adds an object member notification entry, internal-only
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:61
DLLLOCAL QoreObject * getObject() const
gets the current object
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:46
DLLLOCAL QoreVarRWLock * getRWL() const
gets the current read-write lock