Qore Programming Language  0.8.3
include/qore/QoreRWLock.h (4311)
00001 /* -*- mode: c++; indent-tabs-mode: nil -*- */
00002 /*
00003   QoreRWLock.cc
00004 
00005   simple pthreads-based read-write lock
00006 
00007   Qore Programming Language
00008 
00009   Copyright 2003 - 2011 David Nichols
00010 
00011   This library is free software; you can redistribute it and/or
00012   modify it under the terms of the GNU Lesser General Public
00013   License as published by the Free Software Foundation; either
00014   version 2.1 of the License, or (at your option) any later version.
00015   
00016   This library is distributed in the hope that it will be useful,
00017   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019   Lesser General Public License for more details.
00020   
00021   You should have received a copy of the GNU Lesser General Public
00022   License along with this library; if not, write to the Free Software
00023   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00024 */
00025 
00026 #ifndef _QORE_QORERWLOCK_H
00027 #define _QORE_QORERWLOCK_H
00028 
00029 #include <pthread.h>
00030 
00032 
00035 class QoreRWLock {
00036 protected:
00038    pthread_rwlock_t m;
00039 
00041    DLLLOCAL QoreRWLock& operator=(const QoreRWLock&);
00042 
00043 public:
00045    DLLLOCAL QoreRWLock() {
00046 #ifndef NDEBUG
00047       int rc =
00048 #endif
00049       pthread_rwlock_init(&m, 0);
00050       assert(!rc);
00051    }
00052 
00054    DLLLOCAL ~QoreRWLock() {
00055 #ifndef NDEBUG
00056       int rc =
00057 #endif
00058       pthread_rwlock_destroy(&m);
00059       assert(!rc);
00060    }
00061 
00063    DLLLOCAL int rdlock() {
00064       return pthread_rwlock_rdlock(&m);
00065    }
00066 
00068    DLLLOCAL int wrlock() {
00069       return pthread_rwlock_wrlock(&m);
00070    }
00071 
00073    DLLLOCAL int tryrdlock() {
00074       return pthread_rwlock_tryrdlock(&m);
00075    }
00076 
00078    DLLLOCAL int trywrlock() {
00079       return pthread_rwlock_trywrlock(&m);
00080    }
00081 
00083    DLLLOCAL int unlock() {
00084       return pthread_rwlock_unlock(&m);
00085    }
00086 };
00087 
00089 
00093 class QoreAutoRWReadLocker {
00094 private:
00096    DLLLOCAL QoreAutoRWReadLocker(const QoreAutoRWReadLocker&);
00097    
00099    DLLLOCAL QoreAutoRWReadLocker& operator=(const QoreAutoRWReadLocker&);
00100    
00102    DLLLOCAL void *operator new(size_t);
00103 
00104 protected:
00106    QoreRWLock *l;
00107 
00108 public:
00110    DLLLOCAL QoreAutoRWReadLocker(QoreRWLock &n_l) : l(&n_l) {
00111       l->rdlock();
00112    }
00113 
00115    DLLLOCAL QoreAutoRWReadLocker(QoreRWLock *n_l) : l(n_l) {
00116       l->rdlock();
00117    }
00118 
00120    DLLLOCAL ~QoreAutoRWReadLocker() {
00121       l->unlock();
00122    }
00123 };
00124 
00126 
00130 class QoreAutoRWWriteLocker {
00131 private:
00133    DLLLOCAL QoreAutoRWWriteLocker(const QoreAutoRWWriteLocker&);
00134    
00136    DLLLOCAL QoreAutoRWWriteLocker& operator=(const QoreAutoRWWriteLocker&);
00137    
00139    DLLLOCAL void *operator new(size_t);
00140 
00141 protected:
00143    QoreRWLock *l;
00144 
00145 public:
00147    DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock &n_l) : l(&n_l) {
00148       l->wrlock();
00149    }
00150 
00152    DLLLOCAL QoreAutoRWWriteLocker(QoreRWLock *n_l) : l(n_l) {
00153       l->wrlock();
00154    }
00155 
00157    DLLLOCAL ~QoreAutoRWWriteLocker() {
00158       l->unlock();
00159    }
00160 };
00161 
00162 #endif // #ifndef _QORE_QORERWLOCK_H
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines