From ffdd38ddd510da6871ae782a3a7fee466244b0a0 Mon Sep 17 00:00:00 2001 From: joss17 Date: Thu, 8 Apr 2010 19:08:20 +0000 Subject: [PATCH] add the internal counter mutex lock. Move the code of the rw mutec to the rsthreads.cc file git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@2697 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/util/rsthreads.cc | 46 +++++++++++++++++++++++++++++ libretroshare/src/util/rsthreads.h | 15 +++++----- 2 files changed, 54 insertions(+), 7 deletions(-) diff --git a/libretroshare/src/util/rsthreads.cc b/libretroshare/src/util/rsthreads.cc index acc4657bb..2f62d2881 100644 --- a/libretroshare/src/util/rsthreads.cc +++ b/libretroshare/src/util/rsthreads.cc @@ -144,3 +144,49 @@ void RsQueueThread::run() } } +RsReadWriteMutex::RsReadWriteMutex():readLocks(0) { +} + +void RsReadWriteMutex::readLock() { + internalCounterMtx.lock();//lock internal read counter + if (readLocks == 0) { + lock(); //lock normal mutex + } + readLocks++; + internalCounterMtx.unlock(); +} + +void RsReadWriteMutex::readUnlock() { + internalCounterMtx.lock();//lock internal read counter + if (readLocks == 1) { + unlock(); + } + if (readLocks != 0) { + readLocks--; + } + internalCounterMtx.unlock(); +} + +void RsReadWriteMutex::writeLock() { + lock(); +} + +void RsReadWriteMutex::writeUnlock() { + unlock(); +} + +void RsReadWriteMutex::rwlock(uint32_t type) { + if (type & READ_LOCK) { + readLock(); + } else { + writeLock(); + } +} + +void RsReadWriteMutex::rwunlock(uint32_t type) { + if (type & READ_LOCK) { + readUnlock(); + } else { + writeUnlock(); + } +} diff --git a/libretroshare/src/util/rsthreads.h b/libretroshare/src/util/rsthreads.h index 550073c9a..a923b2ed5 100644 --- a/libretroshare/src/util/rsthreads.h +++ b/libretroshare/src/util/rsthreads.h @@ -101,15 +101,15 @@ class RsStackMutex class RsReadWriteMutex: public RsMutex { public: - RsReadWriteMutex(): readLocks(0) { } + RsReadWriteMutex(); - void readLock() { if (readLocks == 0) {lock();} ; readLocks++;} - void readUnlock() { if (readLocks == 1) {unlock();} if (readLocks != 0) {readLocks--;} } - void writeLock() {lock();} - void writeUnlock() {unlock();} + void readLock(); + void readUnlock(); + void writeLock(); + void writeUnlock(); - void rwlock(uint32_t type) { if (type & READ_LOCK) {readLock();} else {writeLock();} } - void rwunlock(uint32_t type) { if (type & READ_LOCK) {readUnlock();} else {writeUnlock();} } + void rwlock(uint32_t type); + void rwunlock(uint32_t type); const static uint32_t READ_LOCK = 0x0001; const static uint32_t WRITE_LOCK = 0x0002; @@ -117,6 +117,7 @@ class RsReadWriteMutex: public RsMutex private: int readLocks; + RsMutex internalCounterMtx; }; class RsStackReadWriteMutex