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
This commit is contained in:
joss17 2010-04-08 19:08:20 +00:00
parent 5974ac004e
commit ffdd38ddd5
2 changed files with 54 additions and 7 deletions

View File

@ -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();
}
}

View File

@ -101,15 +101,15 @@ class RsStackMutex
class RsReadWriteMutex: public RsMutex class RsReadWriteMutex: public RsMutex
{ {
public: public:
RsReadWriteMutex(): readLocks(0) { } RsReadWriteMutex();
void readLock() { if (readLocks == 0) {lock();} ; readLocks++;} void readLock();
void readUnlock() { if (readLocks == 1) {unlock();} if (readLocks != 0) {readLocks--;} } void readUnlock();
void writeLock() {lock();} void writeLock();
void writeUnlock() {unlock();} void writeUnlock();
void rwlock(uint32_t type) { if (type & READ_LOCK) {readLock();} else {writeLock();} } void rwlock(uint32_t type);
void rwunlock(uint32_t type) { if (type & READ_LOCK) {readUnlock();} else {writeUnlock();} } void rwunlock(uint32_t type);
const static uint32_t READ_LOCK = 0x0001; const static uint32_t READ_LOCK = 0x0001;
const static uint32_t WRITE_LOCK = 0x0002; const static uint32_t WRITE_LOCK = 0x0002;
@ -117,6 +117,7 @@ class RsReadWriteMutex: public RsMutex
private: private:
int readLocks; int readLocks;
RsMutex internalCounterMtx;
}; };
class RsStackReadWriteMutex class RsStackReadWriteMutex