Loading...
Searching...
No Matches
AutoVLock.h
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
AutoVLock.h
Qore Programming Language
Copyright (C) 2003 - 2022 Qore Technologies, s.r.o.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Note that the Qore library is released under a choice of three open-source
licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
information.
*/
#ifndef _QORE_AUTOVLOCK_H
#define _QORE_AUTOVLOCK_H
class AbstractSmartLock;
struct QLckPtr {
public:
DLLLOCAL QLckPtr() : rwl(0) {
}
DLLLOCAL void set(QoreVarRWLock* n_rwl) {
rwl = n_rwl;
}
DLLLOCAL QoreVarRWLock* getRWL() const {
return rwl;
}
DLLLOCAL bool isSet() const {
return rwl;
}
DLLLOCAL void unlockAndClear() {
if (rwl) {
unlockIntern();
rwl = 0;
}
}
DLLLOCAL void clear() {
assert(rwl);
rwl = 0;
}
protected:
DLLLOCAL void unlockIntern() {
assert(rwl);
rwl->unlock();
}
private:
QoreVarRWLock* rwl;
};
class AutoVLock {
private:
// pointer to lock currently held
QLckPtr lock;
// pointer to object to dereference
QoreObject* o;
public:
// pointer to ExceptionSink object for use with object notifications
ExceptionSink* xsink;
private:
struct qore_avl_private* priv;
DLLLOCAL AutoVLock(const AutoVLock&) = delete;
DLLLOCAL AutoVLock& operator=(const AutoVLock&) = delete;
DLLLOCAL void* operator new(size_t) = delete;
public:
DLLEXPORT AutoVLock(ExceptionSink* n_xsink);
DLLLOCAL AutoVLock(AutoVLock&& o) = default;
DLLEXPORT ~AutoVLock();
DLLEXPORT operator bool() const;
DLLEXPORT void del();
DLLLOCAL void set(QoreVarRWLock* n_rwl);
DLLLOCAL void set(QoreObject* n_o, QoreVarRWLock* n_rwl);
DLLLOCAL QoreVarRWLock* getRWL() const;
DLLLOCAL QoreObject* getObject() const;
DLLLOCAL void clear();
DLLLOCAL void addMemberNotification(QoreObject* o, const char* member);
};
#endif
AutoVLock
AutoVLock is a container for safely managing global variable and object lock handovers,...
Definition: AutoVLock.h:80
AutoVLock::set
DLLLOCAL void set(QoreObject *n_o, QoreVarRWLock *n_rwl)
sets the current object (for dereference) and lock
AutoVLock::del
DLLEXPORT void del()
manually releases the lock currently held
AutoVLock::clear
DLLLOCAL void clear()
leaves the lock locked and the object referenced and clears the object and lock pointers
AutoVLock::~AutoVLock
DLLEXPORT ~AutoVLock()
releases all locks held and destroys the container
AutoVLock::getObject
DLLLOCAL QoreObject * getObject() const
gets the current object
AutoVLock::AutoVLock
DLLEXPORT AutoVLock(ExceptionSink *n_xsink)
creates an empty lock container
AutoVLock::set
DLLLOCAL void set(QoreVarRWLock *n_rwl)
sets the current lock
AutoVLock::addMemberNotification
DLLLOCAL void addMemberNotification(QoreObject *o, const char *member)
adds an object member notification entry, internal-only
AutoVLock::getRWL
DLLLOCAL QoreVarRWLock * getRWL() const
gets the current read-write lock
AutoVLock::AutoVLock
DLLLOCAL AutoVLock(AutoVLock &&o)=default
default move constructor
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
QoreObject
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:60