summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/sys
diff options
context:
space:
mode:
authorCarl C. Trieloff <cctrieloff@apache.org>2007-07-05 16:19:05 +0000
committerCarl C. Trieloff <cctrieloff@apache.org>2007-07-05 16:19:05 +0000
commita48c9eddce4cbc56521fca7fd64582d9dafe1d40 (patch)
tree923b6677edb1754a4a9a89c5ff0b9e2a8cc28439 /cpp/src/qpid/sys
parent18fc5427470dbe4bfd0e9927beb7b0cc5fe01bfc (diff)
downloadqpid-python-a48c9eddce4cbc56521fca7fd64582d9dafe1d40.tar.gz
- 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
Diffstat (limited to 'cpp/src/qpid/sys')
-rw-r--r--cpp/src/qpid/sys/Mutex.h20
-rw-r--r--cpp/src/qpid/sys/posix/Mutex.h84
2 files changed, 99 insertions, 5 deletions
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 L>
+class ScopedRlock
+{
+ public:
+ ScopedRlock(L& l) : mutex(l) { l.rlock(); }
+ ~ScopedRlock() { mutex.unlock(); }
+ private:
+ L& mutex;
+};
+
+template <class L>
+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<Mutex> ScopedLock;
typedef ScopedUnlock<Mutex> 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<RWlock> ScopedRlock;
+ typedef ScopedWlock<RWlock> 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<PODMutex> 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*/