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 00044 public: 00046 DLLLOCAL QoreThreadLock() 00047 { 00048 pthread_mutex_init(&ptm_lock, 0); 00049 } 00050 00052 DLLLOCAL ~QoreThreadLock() 00053 { 00054 pthread_mutex_destroy(&ptm_lock); 00055 } 00056 00058 DLLLOCAL QoreThreadLock(const QoreThreadLock&) 00059 { 00060 pthread_mutex_init(&ptm_lock, 0); 00061 } 00062 00064 00066 DLLLOCAL void lock() 00067 { 00068 pthread_mutex_lock(&ptm_lock); 00069 } 00070 00072 00074 DLLLOCAL void unlock() 00075 { 00076 pthread_mutex_unlock(&ptm_lock); 00077 } 00078 00080 00083 DLLLOCAL int trylock() 00084 { 00085 return pthread_mutex_trylock(&ptm_lock); 00086 } 00087 }; 00088 00090 00096 class AutoLocker { 00097 private: 00099 DLLLOCAL AutoLocker(const AutoLocker&); 00100 00102 DLLLOCAL AutoLocker& operator=(const AutoLocker&); 00103 00105 DLLLOCAL void *operator new(size_t); 00106 00107 protected: 00109 QoreThreadLock *lck; 00110 00111 public: 00113 DLLLOCAL AutoLocker(QoreThreadLock *l) : lck(l) { 00114 lck->lock(); 00115 } 00116 00118 DLLLOCAL AutoLocker(QoreThreadLock &l) : lck(&l) { 00119 lck->lock(); 00120 } 00121 00123 DLLLOCAL ~AutoLocker() { 00124 lck->unlock(); 00125 } 00126 }; 00127 00129 00135 class SafeLocker { 00136 private: 00138 DLLLOCAL SafeLocker(const SafeLocker&); 00139 00141 DLLLOCAL SafeLocker& operator=(const SafeLocker&); 00142 00144 DLLLOCAL void *operator new(size_t); 00145 00146 protected: 00148 QoreThreadLock *lck; 00149 00151 bool locked; 00152 00153 public: 00155 DLLEXPORT SafeLocker(QoreThreadLock *l) : lck(l) { 00156 lck->lock(); 00157 locked = true; 00158 } 00159 00161 DLLEXPORT SafeLocker(QoreThreadLock &l) : lck(&l) { 00162 lck->lock(); 00163 locked = true; 00164 } 00165 00167 DLLEXPORT ~SafeLocker() { 00168 if (locked) 00169 lck->unlock(); 00170 } 00171 00173 DLLEXPORT void lock() { 00174 assert(!locked); 00175 lck->lock(); 00176 locked = true; 00177 } 00178 00180 DLLEXPORT void unlock() { 00181 assert(locked); 00182 locked = false; 00183 lck->unlock(); 00184 } 00185 00187 DLLEXPORT void stay_locked() { 00188 assert(locked); 00189 locked = false; 00190 } 00191 }; 00192 00193 #endif // _QORE_QORETHREADLOCK_H