00001 /* 00002 QoreThreadLock.h 00003 00004 Qore Programming Language 00005 00006 Copyright (C) 2003 - 2009 David Nichols, all rights reserved 00007 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Lesser General Public 00010 License as published by the Free Software Foundation; either 00011 version 2.1 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public 00019 License along with this library; if not, write to the Free Software 00020 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00021 */ 00022 00023 #ifndef _QORE_QORETHREADLOCK_H 00024 00025 #define _QORE_QORETHREADLOCK_H 00026 00027 #include <pthread.h> 00028 #include <assert.h> 00029 00031 00034 class QoreThreadLock { 00035 friend class QoreCondition; 00036 00037 private: 00039 pthread_mutex_t ptm_lock; 00040 00042 DLLLOCAL QoreThreadLock& operator=(const QoreThreadLock&); 00043 00045 DLLLOCAL void init() { 00046 #ifndef NDEBUG 00047 int rc = 00048 #endif 00049 pthread_mutex_init(&ptm_lock, 0); 00050 assert(!rc); 00051 } 00052 00053 public: 00055 DLLLOCAL QoreThreadLock() { 00056 init(); 00057 } 00058 00060 DLLLOCAL ~QoreThreadLock() { 00061 #ifndef NDEBUG 00062 int rc = 00063 #endif 00064 pthread_mutex_destroy(&ptm_lock); 00065 assert(!rc); 00066 } 00067 00069 DLLLOCAL QoreThreadLock(const QoreThreadLock&) { 00070 init(); 00071 } 00072 00074 00076 DLLLOCAL void lock() { 00077 #ifndef NDEBUG 00078 int rc = 00079 #endif 00080 pthread_mutex_lock(&ptm_lock); 00081 assert(!rc); 00082 } 00083 00085 00087 DLLLOCAL void unlock() { 00088 #ifndef NDEBUG 00089 int rc = 00090 #endif 00091 pthread_mutex_unlock(&ptm_lock); 00092 assert(!rc); 00093 } 00094 00096 00099 DLLLOCAL int trylock() { 00100 return pthread_mutex_trylock(&ptm_lock); 00101 } 00102 }; 00103 00105 00111 class AutoLocker { 00112 private: 00114 DLLLOCAL AutoLocker(const AutoLocker&); 00115 00117 DLLLOCAL AutoLocker& operator=(const AutoLocker&); 00118 00120 DLLLOCAL void *operator new(size_t); 00121 00122 protected: 00124 QoreThreadLock *lck; 00125 00126 public: 00128 DLLLOCAL AutoLocker(QoreThreadLock *l) : lck(l) { 00129 lck->lock(); 00130 } 00131 00133 DLLLOCAL AutoLocker(QoreThreadLock &l) : lck(&l) { 00134 lck->lock(); 00135 } 00136 00138 DLLLOCAL ~AutoLocker() { 00139 lck->unlock(); 00140 } 00141 }; 00142 00144 00150 class SafeLocker { 00151 private: 00153 DLLLOCAL SafeLocker(const SafeLocker&); 00154 00156 DLLLOCAL SafeLocker& operator=(const SafeLocker&); 00157 00159 DLLLOCAL void *operator new(size_t); 00160 00161 protected: 00163 QoreThreadLock *lck; 00164 00166 bool locked; 00167 00168 public: 00170 DLLEXPORT SafeLocker(QoreThreadLock *l) : lck(l) { 00171 lck->lock(); 00172 locked = true; 00173 } 00174 00176 DLLEXPORT SafeLocker(QoreThreadLock &l) : lck(&l) { 00177 lck->lock(); 00178 locked = true; 00179 } 00180 00182 DLLEXPORT ~SafeLocker() { 00183 if (locked) 00184 lck->unlock(); 00185 } 00186 00188 DLLEXPORT void lock() { 00189 assert(!locked); 00190 lck->lock(); 00191 locked = true; 00192 } 00193 00195 DLLEXPORT void unlock() { 00196 assert(locked); 00197 locked = false; 00198 lck->unlock(); 00199 } 00200 00202 DLLEXPORT void stay_locked() { 00203 assert(locked); 00204 locked = false; 00205 } 00206 }; 00207 00208 #endif // _QORE_QORETHREADLOCK_H