|
Qore Programming Language
0.8.3
|
00001 /* -*- mode: c++; indent-tabs-mode: nil -*- */ 00002 /* 00003 QoreThreadLock.h 00004 00005 Qore Programming Language 00006 00007 Copyright (C) 2003 - 2011 David Nichols, all rights reserved 00008 00009 This library is free software; you can redistribute it and/or 00010 modify it under the terms of the GNU Lesser General Public 00011 License as published by the Free Software Foundation; either 00012 version 2.1 of the License, or (at your option) any later version. 00013 00014 This library is distributed in the hope that it will be useful, 00015 but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 Lesser General Public License for more details. 00018 00019 You should have received a copy of the GNU Lesser General Public 00020 License along with this library; if not, write to the Free Software 00021 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00022 */ 00023 00024 #ifndef _QORE_QORETHREADLOCK_H 00025 00026 #define _QORE_QORETHREADLOCK_H 00027 00028 #include <pthread.h> 00029 00030 #include <assert.h> 00031 00032 #include <string.h> 00033 #include <stdio.h> 00034 #include <signal.h> 00035 #include <stdlib.h> 00036 00038 00041 class QoreThreadLock { 00042 friend class QoreCondition; 00043 00044 private: 00046 pthread_mutex_t ptm_lock; 00047 00049 DLLLOCAL QoreThreadLock& operator=(const QoreThreadLock&); 00050 00052 DLLLOCAL void init(const pthread_mutexattr_t *pma = 0) { 00053 #ifndef NDEBUG 00054 int rc = 00055 #endif 00056 pthread_mutex_init(&ptm_lock, pma); 00057 assert(!rc); 00058 } 00059 00060 public: 00061 00063 DLLLOCAL QoreThreadLock() { 00064 init(); 00065 } 00066 00068 DLLLOCAL QoreThreadLock(const pthread_mutexattr_t *ma) { 00069 init(ma); 00070 } 00071 00073 DLLLOCAL ~QoreThreadLock() { 00074 pthread_mutex_destroy(&ptm_lock); 00075 } 00076 00078 DLLLOCAL QoreThreadLock(const QoreThreadLock&) { 00079 init(); 00080 } 00081 00083 00085 DLLLOCAL void lock() { 00086 #ifndef NDEBUG 00087 int rc = 00088 #endif 00089 pthread_mutex_lock(&ptm_lock); 00090 assert(!rc); 00091 } 00092 00094 00096 DLLLOCAL void unlock() { 00097 #ifndef NDEBUG 00098 int rc = 00099 #endif 00100 pthread_mutex_unlock(&ptm_lock); 00101 assert(!rc); 00102 } 00103 00105 00108 DLLLOCAL int trylock() { 00109 return pthread_mutex_trylock(&ptm_lock); 00110 } 00111 }; 00112 00114 00121 class AutoLocker { 00122 private: 00124 DLLLOCAL AutoLocker(const AutoLocker&); 00125 00127 DLLLOCAL AutoLocker& operator=(const AutoLocker&); 00128 00130 DLLLOCAL void *operator new(size_t); 00131 00132 protected: 00134 QoreThreadLock *lck; 00135 00136 public: 00138 DLLLOCAL AutoLocker(QoreThreadLock *l) : lck(l) { 00139 lck->lock(); 00140 } 00141 00143 DLLLOCAL AutoLocker(QoreThreadLock &l) : lck(&l) { 00144 lck->lock(); 00145 } 00146 00148 DLLLOCAL ~AutoLocker() { 00149 lck->unlock(); 00150 } 00151 }; 00152 00154 00161 class SafeLocker { 00162 private: 00164 DLLLOCAL SafeLocker(const SafeLocker&); 00165 00167 DLLLOCAL SafeLocker& operator=(const SafeLocker&); 00168 00170 DLLLOCAL void *operator new(size_t); 00171 00172 protected: 00174 QoreThreadLock *lck; 00175 00177 bool locked; 00178 00179 public: 00181 DLLLOCAL SafeLocker(QoreThreadLock *l) : lck(l) { 00182 lck->lock(); 00183 locked = true; 00184 } 00185 00187 DLLLOCAL SafeLocker(QoreThreadLock &l) : lck(&l) { 00188 lck->lock(); 00189 locked = true; 00190 } 00191 00193 DLLLOCAL ~SafeLocker() { 00194 if (locked) 00195 lck->unlock(); 00196 } 00197 00199 DLLLOCAL void lock() { 00200 assert(!locked); 00201 lck->lock(); 00202 locked = true; 00203 } 00204 00206 DLLLOCAL void unlock() { 00207 assert(locked); 00208 locked = false; 00209 lck->unlock(); 00210 } 00211 00213 DLLLOCAL void stay_locked() { 00214 assert(locked); 00215 locked = false; 00216 } 00217 }; 00218 00220 00224 class OptLocker { 00225 private: 00227 DLLLOCAL OptLocker(const OptLocker&); 00228 00230 DLLLOCAL OptLocker& operator=(const OptLocker&); 00231 00233 DLLLOCAL void *operator new(size_t); 00234 00235 protected: 00237 QoreThreadLock *lck; 00238 00239 public: 00241 DLLLOCAL OptLocker(QoreThreadLock *l) : lck(l) { 00242 if (lck) 00243 lck->lock(); 00244 } 00245 00247 DLLLOCAL ~OptLocker() { 00248 if (lck) 00249 lck->unlock(); 00250 } 00251 }; 00252 00253 #endif // _QORE_QORETHREADLOCK_H