All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreThreadLock.h
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
QoreThreadLock.h
Qore Programming Language
Copyright (C) 2003 - 2013 David Nichols, all rights reserved
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_QORETHREADLOCK_H
#define _QORE_QORETHREADLOCK_H
#include <pthread.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
class QoreThreadLock {
friend class QoreCondition;
private:
pthread_mutex_t ptm_lock;
DLLLOCAL QoreThreadLock& operator=(const QoreThreadLock&);
DLLLOCAL void init(const pthread_mutexattr_t *pma = 0) {
#ifndef NDEBUG
int rc =
#endif
pthread_mutex_init(&ptm_lock, pma);
assert(!rc);
}
public:
DLLLOCAL QoreThreadLock() {
init();
}
DLLLOCAL QoreThreadLock(const pthread_mutexattr_t *ma) {
init(ma);
}
DLLLOCAL ~QoreThreadLock() {
pthread_mutex_destroy(&ptm_lock);
}
DLLLOCAL QoreThreadLock(const QoreThreadLock&) {
init();
}
DLLLOCAL void lock() {
#ifndef NDEBUG
int rc =
#endif
pthread_mutex_lock(&ptm_lock);
assert(!rc);
}
DLLLOCAL void unlock() {
#ifndef NDEBUG
int rc =
#endif
pthread_mutex_unlock(&ptm_lock);
assert(!rc);
}
DLLLOCAL int trylock() {
return pthread_mutex_trylock(&ptm_lock);
}
};
class AutoLocker {
private:
DLLLOCAL AutoLocker(const AutoLocker&);
DLLLOCAL AutoLocker& operator=(const AutoLocker&);
DLLLOCAL void *operator new(size_t);
protected:
QoreThreadLock *lck;
public:
DLLLOCAL AutoLocker(QoreThreadLock *l) : lck(l) {
lck->lock();
}
DLLLOCAL AutoLocker(QoreThreadLock &l) : lck(&l) {
lck->lock();
}
DLLLOCAL ~AutoLocker() {
lck->unlock();
}
};
class SafeLocker {
private:
DLLLOCAL SafeLocker(const SafeLocker&);
DLLLOCAL SafeLocker& operator=(const SafeLocker&);
DLLLOCAL void *operator new(size_t);
protected:
QoreThreadLock *lck;
bool locked;
public:
DLLLOCAL SafeLocker(QoreThreadLock *l) : lck(l) {
lck->lock();
locked = true;
}
DLLLOCAL SafeLocker(QoreThreadLock &l) : lck(&l) {
lck->lock();
locked = true;
}
DLLLOCAL ~SafeLocker() {
if (locked)
lck->unlock();
}
DLLLOCAL void lock() {
assert(!locked);
lck->lock();
locked = true;
}
DLLLOCAL void unlock() {
assert(locked);
locked = false;
lck->unlock();
}
DLLLOCAL void stay_locked() {
assert(locked);
locked = false;
}
};
class OptLocker {
private:
DLLLOCAL OptLocker(const OptLocker&);
DLLLOCAL OptLocker& operator=(const OptLocker&);
DLLLOCAL void *operator new(size_t);
protected:
QoreThreadLock *lck;
public:
DLLLOCAL OptLocker(QoreThreadLock *l) : lck(l) {
if (lck)
lck->lock();
}
DLLLOCAL ~OptLocker() {
if (lck)
lck->unlock();
}
};
#endif // _QORE_QORETHREADLOCK_H