All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreRWLock.h
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
QoreRWLock.cc
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();
}
};
#endif // #ifndef _QORE_QORERWLOCK_H