/*- * This file is a part of the MDCacheD server * (c) 2007.-2010. Ivan Voras * Released under the 2-clause BSDL. * $Id: RWLock.h 347 2011-09-20 20:58:41Z ivoras $ */ #ifndef _RWLock_H_ #define _RWLock_H_ #include #include class RWLockException : public std::exception { private: char *msg; public: RWLockException(char *msg) : exception() { this->msg = msg; } }; class RWLock { public: RWLock() throw (RWLockException) { if (pthread_rwlock_init(&rwl, NULL) != 0) throw new RWLockException((char*)"pthread_rwlock_init failed at " __FILE__); } ~RWLock() { pthread_rwlock_destroy(&rwl); } void RdLock() { pthread_rwlock_rdlock(&rwl); } void WrLock() { pthread_rwlock_wrlock(&rwl); } bool TryRdLock() { return pthread_rwlock_tryrdlock(&rwl) == 0; } bool TryWrLock() { return pthread_rwlock_trywrlock(&rwl) == 0; } void Unlock() { pthread_rwlock_unlock(&rwl); } private: pthread_rwlock_t rwl; }; #endif