From a48c9eddce4cbc56521fca7fd64582d9dafe1d40 Mon Sep 17 00:00:00 2001 From: "Carl C. Trieloff" Date: Thu, 5 Jul 2007 16:19:05 +0000 Subject: - Added RW lock - Updated all exchanges to us RW lock - Updated all registries to us RW lock - Still need to do (client, channel, message and queues) git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@553549 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/sys/Mutex.h | 20 ++++++++++ cpp/src/qpid/sys/posix/Mutex.h | 84 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 99 insertions(+), 5 deletions(-) (limited to 'cpp/src/qpid/sys') diff --git a/cpp/src/qpid/sys/Mutex.h b/cpp/src/qpid/sys/Mutex.h index 4eff6078ae..b8545d4449 100644 --- a/cpp/src/qpid/sys/Mutex.h +++ b/cpp/src/qpid/sys/Mutex.h @@ -46,6 +46,26 @@ class ScopedUnlock L& mutex; }; +template +class ScopedRlock +{ + public: + ScopedRlock(L& l) : mutex(l) { l.rlock(); } + ~ScopedRlock() { mutex.unlock(); } + private: + L& mutex; +}; + +template +class ScopedWlock +{ + public: + ScopedWlock(L& l) : mutex(l) { l.wlock(); } + ~ScopedWlock() { mutex.unlock(); } + private: + L& mutex; +}; + }} #ifdef USE_APR_PLATFORM diff --git a/cpp/src/qpid/sys/posix/Mutex.h b/cpp/src/qpid/sys/posix/Mutex.h index b278c6b14a..b29219235d 100644 --- a/cpp/src/qpid/sys/posix/Mutex.h +++ b/cpp/src/qpid/sys/posix/Mutex.h @@ -38,29 +38,58 @@ class Mutex : private boost::noncopyable { public: typedef ScopedLock ScopedLock; typedef ScopedUnlock ScopedUnlock; - + inline Mutex(); inline ~Mutex(); - inline void lock(); + inline void lock(); inline void unlock(); - inline void trylock(); + inline void trylock(); + protected: pthread_mutex_t mutex; }; +/** + * RW lock. + */ +class RWlock : private boost::noncopyable { + friend class Condition; + +public: + typedef ScopedRlock ScopedRlock; + typedef ScopedWlock ScopedWlock; + + inline RWlock(); + inline ~RWlock(); + inline void wlock(); // will write-lock + inline void rlock(); // will read-lock + inline void unlock(); + inline void trywlock(); // will write-try + inline void tryrlock(); // will read-try + +protected: + pthread_rwlock_t rwlock; +}; + + /** * Initialise a recursive mutex attr for use in creating mutexes later * (we use pthread_once to make sure it is initialised exactly once) */ namespace { pthread_once_t onceControl = PTHREAD_ONCE_INIT; + pthread_rwlockattr_t rwlockattr; pthread_mutexattr_t mutexattr; void initMutexattr() { pthread_mutexattr_init(&mutexattr); pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE); } + + void initRWlockattr() { + pthread_rwlockattr_init(&rwlockattr); + } struct RecursiveMutexattr { RecursiveMutexattr() { @@ -71,8 +100,21 @@ namespace { return &mutexattr; } }; + struct RecursiveRWlockattr { + RecursiveRWlockattr() { + pthread_once(&onceControl, initRWlockattr); + } + + operator const pthread_rwlockattr_t*() const { + return &rwlockattr; + } + }; RecursiveMutexattr recursiveMutexattr; + RecursiveRWlockattr recursiveRWlockattr; + + + } /** @@ -83,9 +125,9 @@ struct PODMutex { typedef ScopedLock ScopedLock; - inline void lock(); + inline void lock(); inline void unlock(); - inline void trylock(); + inline void trylock(); // Must be public to be a POD: pthread_mutex_t mutex; @@ -96,6 +138,7 @@ struct PODMutex void PODMutex::lock() { QPID_POSIX_THROW_IF(pthread_mutex_lock(&mutex)); } + void PODMutex::unlock() { QPID_POSIX_THROW_IF(pthread_mutex_unlock(&mutex)); } @@ -115,6 +158,7 @@ Mutex::~Mutex(){ void Mutex::lock() { QPID_POSIX_THROW_IF(pthread_mutex_lock(&mutex)); } + void Mutex::unlock() { QPID_POSIX_THROW_IF(pthread_mutex_unlock(&mutex)); } @@ -123,5 +167,35 @@ void Mutex::trylock() { QPID_POSIX_THROW_IF(pthread_mutex_trylock(&mutex)); } + +RWlock::RWlock() { + QPID_POSIX_THROW_IF(pthread_rwlock_init(&rwlock, recursiveRWlockattr)); +} + +RWlock::~RWlock(){ + QPID_POSIX_THROW_IF(pthread_rwlock_destroy(&rwlock)); +} + +void RWlock::wlock() { + QPID_POSIX_THROW_IF(pthread_rwlock_wrlock(&rwlock)); +} + +void RWlock::rlock() { + QPID_POSIX_THROW_IF(pthread_rwlock_rdlock(&rwlock)); +} + +void RWlock::unlock() { + QPID_POSIX_THROW_IF(pthread_rwlock_unlock(&rwlock)); +} + +void RWlock::trywlock() { + QPID_POSIX_THROW_IF(pthread_rwlock_trywrlock(&rwlock)); +} + +void RWlock::tryrlock() { + QPID_POSIX_THROW_IF(pthread_rwlock_tryrdlock(&rwlock)); +} + + }} #endif /*!_sys_posix_Mutex_h*/ -- cgit v1.2.1