00001 #ifndef PHX_LOCKS_H 00002 #define PHX_LOCKS_H 00003 00004 #define HAVE_PTHREAD_RECURSUVE_LOCKS 0 00005 00006 #include <Phx/PhxConfig.h> // also includes pthread.h 00007 #include <Phx/PhxTypes.h> 00008 00009 #if HAVE_PTHREAD_RECURSUVE_LOCKS 00010 00011 #else 00012 #include <Phx/Util/PhxThread.h> 00013 #endif 00014 00015 #include <errno.h> 00016 #include <iostream> 00017 00024 namespace Phx { 00025 00033 class Lock { 00034 public: 00046 Lock(bool recursive=false); 00047 00054 ~Lock(); 00055 00063 void lock(void) 00064 const; 00065 00072 void unlock(void) const; 00073 00074 bool isRecursive() { 00075 return mRecursive; 00076 } 00077 00078 private: 00079 pthread_mutex_t mLock; 00080 bool mRecursive; 00081 00082 #if HAVE_PTHREAD_RECURSUVE_LOCKS 00083 00084 #else 00085 int mClaimedNumTimes; 00086 pthread_t mThreadID; 00087 #endif 00088 }; 00089 00098 class ReadWriteLock { 00099 public: 00111 ReadWriteLock(); 00112 00119 ~ReadWriteLock(); 00120 00128 void readLock(void) const; 00129 00137 void writeLock(void) const; 00138 00148 void unlock(void) const; 00149 00150 private: 00151 #if HAVE_PTHREAD_RWLOCK_INIT || DOXYGEN 00152 00153 pthread_rwlock_t mLock; 00154 #else 00155 // This is for the replacement for the ReadWriteLock class in the 00156 // event that the platform PThread implementation does not include 00157 // the rwlock type. POSIX does not define this type; it was added 00158 // by the X/Open group. This implementation uses a standard mutex 00159 // at present. This is less efficient, but safe. 00160 00161 pthread_mutex_t mLock; 00162 #endif 00163 }; 00164 00173 class LockHolder { 00174 public: 00175 00183 LockHolder(const Lock* lock) 00184 ; 00185 00201 LockHolder(LockHolder* lockHolder); 00202 00209 ~LockHolder(); 00210 00217 void unlock(void); 00218 00219 private: 00220 // no assignment or copy 00221 LockHolder& operator=(LockHolder& ); 00222 LockHolder(LockHolder& ); 00223 00224 const Lock* mLock; 00225 }; 00226 00237 class ReadLockHolder { 00238 public: 00247 ReadLockHolder(const ReadWriteLock* lock) 00248 ; 00249 00265 ReadLockHolder(ReadLockHolder* lockHolder); 00266 00273 ~ReadLockHolder(); 00274 00281 void unlock(void); 00282 00283 private: 00284 // no assignment 00285 ReadLockHolder& operator=(ReadLockHolder& ); 00286 ReadLockHolder(ReadLockHolder&); 00287 00288 const ReadWriteLock* mLock; 00289 }; 00290 00301 class WriteLockHolder { 00302 public: 00311 WriteLockHolder(const ReadWriteLock* lock) 00312 ; 00313 00329 WriteLockHolder(WriteLockHolder* lockHolder); 00330 00337 ~WriteLockHolder(); 00338 00345 void unlock(void); 00346 00347 private: 00348 // no assignment 00349 WriteLockHolder& operator=(WriteLockHolder& ); 00350 WriteLockHolder(WriteLockHolder&); 00351 00352 const ReadWriteLock* mLock; 00353 }; 00354 00355 00356 } 00357 ; // namespace Phx 00358 00359 #endif /* PHX_LOCKS_H */
1.4.2