All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreRWLock.h
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
QoreRWLock.h
simple pthreads-based read-write lock
Qore Programming Language
Copyright 2003 - 2013 David Nichols
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _QORE_QORERWLOCK_H
#define _QORE_QORERWLOCK_H
#include <pthread.h>
class QoreRWLock {
protected:
pthread_rwlock_t m;
DLLLOCAL QoreRWLock& operator=(const QoreRWLock&);
public:
DLLLOCAL QoreRWLock() {
#ifndef NDEBUG
int rc =
#endif
pthread_rwlock_init(&m, 0);
assert(!rc);
}
DLLLOCAL ~QoreRWLock() {
#ifndef NDEBUG
int rc =
#endif
pthread_rwlock_destroy(&m);
assert(!rc);
}
DLLLOCAL int rdlock() {
return pthread_rwlock_rdlock(&m);
}
DLLLOCAL int wrlock() {
return pthread_rwlock_wrlock(&m);
}
DLLLOCAL int tryrdlock() {
return pthread_rwlock_tryrdlock(&m);
}
DLLLOCAL int trywrlock() {
return pthread_rwlock_trywrlock(&m);
}
DLLLOCAL int unlock() {
return pthread_rwlock_unlock(&m);
}
};
class QoreAutoRWReadLocker {
private:
DLLLOCAL QoreAutoRWReadLocker(const QoreAutoRWReadLocker&);
DLLLOCAL QoreAutoRWReadLocker& operator=(const QoreAutoRWReadLocker&);
DLLLOCAL void *operator new(size_t);
protected:
QoreRWLock *l;
public:
DLLLOCAL QoreAutoRWReadLocker(QoreRWLock &n_l) : l(&n_l) {
l->rdlock();
}
DLLLOCAL QoreAutoRWReadLocker(QoreRWLock *n_l) : l(n_l) {
l->rdlock();
}
DLLLOCAL ~QoreAutoRWReadLocker() {
l->unlock();
}
};
class QoreAutoRWWriteLocker {
private:
DLLLOCAL QoreAutoRWWriteLocker(const QoreAutoRWWriteLocker&);
DLLLOCAL QoreAutoRWWriteLocker& operator=(const QoreAutoRWWriteLocker&);
DLLLOCAL void *operator new(size_t);
protected:
QoreRWLock *l;
public:
DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock &n_l) : l(&n_l) {
l->wrlock();
}
DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock *n_l) : l(n_l) {
l->wrlock();
}
DLLLOCAL ~QoreAutoRWWriteLocker() {
l->unlock();
}
};
class QoreSafeRWReadLocker {
private:
DLLLOCAL QoreSafeRWReadLocker(const QoreSafeRWReadLocker&);
DLLLOCAL QoreSafeRWReadLocker& operator=(const QoreSafeRWReadLocker&);
DLLLOCAL void *operator new(size_t);
protected:
QoreRWLock *l;
bool locked;
public:
DLLLOCAL QoreSafeRWReadLocker(QoreRWLock &n_l) : l(&n_l) {
l->rdlock();
locked = true;
}
DLLLOCAL QoreSafeRWReadLocker(QoreRWLock *n_l) : l(n_l) {
l->rdlock();
locked = true;
}
DLLLOCAL ~QoreSafeRWReadLocker() {
if (locked)
l->unlock();
}
DLLLOCAL void lock() {
assert(!locked);
l->rdlock();
locked = true;
}
DLLLOCAL void unlock() {
assert(locked);
locked = false;
l->unlock();
}
DLLLOCAL void stay_locked() {
assert(locked);
locked = false;
}
};
class QoreSafeRWWriteLocker {
private:
DLLLOCAL QoreSafeRWWriteLocker(const QoreSafeRWWriteLocker&);
DLLLOCAL QoreSafeRWWriteLocker& operator=(const QoreSafeRWWriteLocker&);
DLLLOCAL void *operator new(size_t);
protected:
QoreRWLock *l;
bool locked;
public:
DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock &n_l) : l(&n_l) {
l->wrlock();
locked = true;
}
DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock *n_l) : l(n_l) {
l->wrlock();
locked = true;
}
DLLLOCAL ~QoreSafeRWWriteLocker() {
if (locked)
l->unlock();
}
DLLLOCAL void lock() {
assert(!locked);
l->wrlock();
locked = true;
}
DLLLOCAL void unlock() {
assert(locked);
locked = false;
l->unlock();
}
DLLLOCAL void stay_locked() {
assert(locked);
locked = false;
}
};
class QoreOptionalRWWriteLocker {
protected:
QoreRWLock* l;
public:
DLLLOCAL QoreOptionalRWWriteLocker(QoreRWLock* n_l) : l(n_l->trywrlock() ? 0 : n_l) {
}
DLLLOCAL QoreOptionalRWWriteLocker(QoreRWLock& n_l) : l(n_l.trywrlock() ? 0 : &n_l) {
}
DLLLOCAL ~QoreOptionalRWWriteLocker() {
if (l)
l->unlock();
}
DLLLOCAL operator bool() const {
return (bool)l;
}
};
class QoreOptionalRWReadLocker {
protected:
QoreRWLock* l;
public:
DLLLOCAL QoreOptionalRWReadLocker(QoreRWLock* n_l) : l(n_l->tryrdlock() ? 0 : n_l) {
}
DLLLOCAL QoreOptionalRWReadLocker(QoreRWLock& n_l) : l(n_l.tryrdlock() ? 0 : &n_l) {
}
DLLLOCAL ~QoreOptionalRWReadLocker() {
if (l)
l->unlock();
}
DLLLOCAL operator bool() const {
return (bool)l;
}
};
#endif // #ifndef _QORE_QORERWLOCK_H
QoreAutoRWWriteLocker
provides a safe and exception-safe way to hold write locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:130
QoreAutoRWWriteLocker::QoreAutoRWWriteLocker
DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock &n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:147
QoreRWLock::wrlock
DLLLOCAL int wrlock()
grabs the write lock
Definition: QoreRWLock.h:68
QoreSafeRWWriteLocker::QoreSafeRWWriteLocker
DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock &n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:250
QoreSafeRWReadLocker::unlock
DLLLOCAL void unlock()
unlocks the object and updates the locked flag, assumes that the lock is held
Definition: QoreRWLock.h:212
QoreSafeRWReadLocker::~QoreSafeRWReadLocker
DLLLOCAL ~QoreSafeRWReadLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:199
QoreSafeRWWriteLocker::unlock
DLLLOCAL void unlock()
unlocks the object and updates the locked flag, assumes that the lock is held
Definition: QoreRWLock.h:275
QoreSafeRWReadLocker::QoreSafeRWReadLocker
DLLLOCAL QoreSafeRWReadLocker(QoreRWLock *n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:193
QoreRWLock::QoreRWLock
DLLLOCAL QoreRWLock()
creates and initializes the lock
Definition: QoreRWLock.h:45
QoreRWLock::~QoreRWLock
DLLLOCAL ~QoreRWLock()
destroys the lock
Definition: QoreRWLock.h:54
QoreAutoRWReadLocker::~QoreAutoRWReadLocker
DLLLOCAL ~QoreAutoRWReadLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:120
QoreAutoRWWriteLocker::~QoreAutoRWWriteLocker
DLLLOCAL ~QoreAutoRWWriteLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:157
QoreSafeRWWriteLocker::locked
bool locked
lock flag
Definition: QoreRWLock.h:246
QoreSafeRWReadLocker::locked
bool locked
lock flag
Definition: QoreRWLock.h:183
QoreSafeRWReadLocker::QoreSafeRWReadLocker
DLLLOCAL QoreSafeRWReadLocker(QoreRWLock &n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:187
QoreSafeRWWriteLocker
provides a safe and exception-safe way to hold write locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:230
QoreRWLock::unlock
DLLLOCAL int unlock()
unlocks the lock (assumes the lock is locked)
Definition: QoreRWLock.h:83
QoreRWLock::rdlock
DLLLOCAL int rdlock()
grabs the read lock
Definition: QoreRWLock.h:63
QoreAutoRWReadLocker
provides a safe and exception-safe way to hold read locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:93
QoreAutoRWReadLocker::QoreAutoRWReadLocker
DLLLOCAL QoreAutoRWReadLocker(QoreRWLock &n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:110
QoreSafeRWReadLocker
provides a safe and exception-safe way to hold read locks in Qore, only to be used on the stack...
Definition: QoreRWLock.h:167
QoreAutoRWWriteLocker::l
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:143
QoreSafeRWReadLocker::lock
DLLLOCAL void lock()
locks the object and updates the locked flag, assumes that the lock is not already held ...
Definition: QoreRWLock.h:205
QoreAutoRWReadLocker::l
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:106
QoreSafeRWReadLocker::stay_locked
DLLLOCAL void stay_locked()
will not unlock the lock when the destructor is run; do not use any other functions of this class aft...
Definition: QoreRWLock.h:219
QoreRWLock
provides a simple POSIX-threads-based read-write lock
Definition: QoreRWLock.h:35
QoreSafeRWWriteLocker::QoreSafeRWWriteLocker
DLLLOCAL QoreSafeRWWriteLocker(QoreRWLock *n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:256
QoreRWLock::m
pthread_rwlock_t m
the actual locking primitive wrapped in this class
Definition: QoreRWLock.h:38
QoreSafeRWWriteLocker::stay_locked
DLLLOCAL void stay_locked()
will not unlock the lock when the destructor is run; do not use any other functions of this class aft...
Definition: QoreRWLock.h:282
QoreAutoRWReadLocker::QoreAutoRWReadLocker
DLLLOCAL QoreAutoRWReadLocker(QoreRWLock *n_l)
creates the object and grabs the read lock
Definition: QoreRWLock.h:115
QoreRWLock::tryrdlock
DLLLOCAL int tryrdlock()
tries to grab the read lock; does not block if unsuccessful
Definition: QoreRWLock.h:73
QoreRWLock::operator=
DLLLOCAL QoreRWLock & operator=(const QoreRWLock &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
QoreAutoRWWriteLocker::QoreAutoRWWriteLocker
DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock *n_l)
creates the object and grabs the write lock
Definition: QoreRWLock.h:152
QoreSafeRWWriteLocker::l
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:243
QoreSafeRWReadLocker::l
QoreRWLock * l
the pointer to the lock that will be managed
Definition: QoreRWLock.h:180
QoreSafeRWWriteLocker::lock
DLLLOCAL void lock()
locks the object and updates the locked flag, assumes that the lock is not already held ...
Definition: QoreRWLock.h:268
QoreRWLock::trywrlock
DLLLOCAL int trywrlock()
tries to grab the write lock; does not block if unsuccessful
Definition: QoreRWLock.h:78
QoreSafeRWWriteLocker::~QoreSafeRWWriteLocker
DLLLOCAL ~QoreSafeRWWriteLocker()
destroys the object and releases the lock
Definition: QoreRWLock.h:262