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