1 /*- 2 * This file is a part of the MDCacheD server 3 * (c) 2007.-2010. Ivan Voras <ivoras@gmail.com> 4 * Released under the 2-clause BSDL. 5 * $Id: RWLock.h 347 2011-09-20 20:58:41Z ivoras $ 6 */ 7 8 #ifndef _RWLock_H_ 9 #define _RWLock_H_ 10 11 #include <pthread.h> 12 #include <exception> 13 14 15 class RWLockException : public std::exception { 16 private: 17 char *msg; 18 public: RWLockException(char * msg)19 RWLockException(char *msg) : exception() { 20 this->msg = msg; 21 } 22 }; 23 24 25 class RWLock { 26 public: RWLock()27 RWLock() throw (RWLockException) { 28 if (pthread_rwlock_init(&rwl, NULL) != 0) 29 throw new RWLockException((char*)"pthread_rwlock_init failed at " __FILE__); 30 } ~RWLock()31 ~RWLock() { 32 pthread_rwlock_destroy(&rwl); 33 } RdLock()34 void RdLock() { 35 pthread_rwlock_rdlock(&rwl); 36 } WrLock()37 void WrLock() { 38 pthread_rwlock_wrlock(&rwl); 39 } TryRdLock()40 bool TryRdLock() { 41 return pthread_rwlock_tryrdlock(&rwl) == 0; 42 } TryWrLock()43 bool TryWrLock() { 44 return pthread_rwlock_trywrlock(&rwl) == 0; 45 } Unlock()46 void Unlock() { 47 pthread_rwlock_unlock(&rwl); 48 } 49 private: 50 pthread_rwlock_t rwl; 51 }; 52 53 #endif 54