ad a read / write lock and refactor authgpg with it

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@2695 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
joss17 2010-04-08 19:07:40 +00:00
parent a2a567851e
commit 0f5214ff76
3 changed files with 209 additions and 159 deletions

View file

@ -39,7 +39,7 @@ class RsMutex
public:
RsMutex()
{
{
pthread_mutex_init(&realMutex, NULL);
#ifdef RSTHREAD_SELF_LOCKING_GUARD
_thread_id = 0 ;
@ -98,6 +98,40 @@ class RsStackMutex
RsMutex &mMtx;
};
class RsReadWriteMutex: public RsMutex
{
public:
RsReadWriteMutex(): readLocks(0) { }
void readLock() { if (readLocks == 0) {lock();} ; readLocks++;}
void readUnlock() { if (readLocks == 1) {unlock();} if (readLocks != 0) {readLocks--;} }
void writeLock() {lock();}
void writeUnlock() {unlock();}
void rwlock(uint32_t type) { if (type & READ_LOCK) {readLock();} else {writeLock();} }
void rwunlock(uint32_t type) { if (type & READ_LOCK) {readUnlock();} else {writeUnlock();} }
const static uint32_t READ_LOCK = 0x0001;
const static uint32_t WRITE_LOCK = 0x0002;
private:
int readLocks;
};
class RsStackReadWriteMutex
{
public:
RsStackReadWriteMutex(RsReadWriteMutex &mtx): mMtx(mtx) { mMtx.writeLock(); writeLock = true;}
RsStackReadWriteMutex(RsReadWriteMutex &mtx, uint32_t type): mMtx(mtx) { if (type == RsReadWriteMutex::READ_LOCK) {mMtx.readLock(); writeLock = false;} else {mMtx.writeLock(); writeLock = true;} }
~RsStackReadWriteMutex() { if(writeLock) {mMtx.writeUnlock();} else {mMtx.readUnlock();} }
private:
RsReadWriteMutex &mMtx;
bool writeLock;
};
class RsThread;
/* to create a thread! */