version 1.6.6.2, 2008/09/29 17:41:56
|
version 1.14, 2008/09/16 18:37:03
|
|
|
#include <Pegasus/Common/Mutex.h> | #include <Pegasus/Common/Mutex.h> |
#include <Pegasus/Common/AtomicInt.h> | #include <Pegasus/Common/AtomicInt.h> |
| |
#define PEG_SEM_READ 1 |
|
#define PEG_SEM_WRITE 2 |
|
|
|
PEGASUS_NAMESPACE_BEGIN | PEGASUS_NAMESPACE_BEGIN |
| |
/** | /** |
|
|
struct ReadWriteSemRep | struct ReadWriteSemRep |
{ | { |
pthread_rwlock_t rwlock; | pthread_rwlock_t rwlock; |
ThreadType owner; |
|
}; | }; |
#endif /* PEGASUS_USE_POSIX_RWLOCK */ | #endif /* PEGASUS_USE_POSIX_RWLOCK */ |
| |
|
|
Semaphore _rlock; | Semaphore _rlock; |
Mutex _wlock; | Mutex _wlock; |
Mutex _internal_lock; | Mutex _internal_lock; |
ThreadType _owner; |
AtomicInt _readers; |
|
AtomicInt _writers; |
ReadWriteSemRep() : | ReadWriteSemRep() : |
_rlock(10), _wlock(), _internal_lock(), _owner(Threads::self()) |
_rlock(10), _wlock(), _internal_lock(), _readers(0), _writers(0) |
{ | { |
} | } |
}; | }; |
|
|
| |
~ReadWriteSem(); | ~ReadWriteSem(); |
| |
// @exception Deadlock |
void waitRead(); |
// @exception Permission |
|
// @exception WaitFailed |
|
inline void wait_read(ThreadType caller) |
|
{ |
|
wait(PEG_SEM_READ, caller ); |
|
} |
|
| |
// @exception Deadlock |
void waitWrite(); |
// @exception Permission |
|
// @exception WaitFailed |
|
inline void wait_write(ThreadType caller) |
|
{ |
|
wait(PEG_SEM_WRITE, caller); |
|
} |
|
| |
// @exception Permission |
void unlockRead(); |
inline void unlock_read(ThreadType caller) |
|
{ |
|
unlock(PEG_SEM_READ, caller); |
|
} |
|
| |
// @exception Permission |
void unlockWrite(); |
inline void unlock_write(ThreadType caller) |
|
{ |
|
unlock(PEG_SEM_WRITE, caller); |
|
} |
|
|
|
int read_count() const; |
|
int write_count() const; |
|
|
|
// @exception Deadlock |
|
// @exception Permission |
|
// @exception WaitFailed |
|
// @exception TooManyReaders |
|
void wait(Uint32 mode, ThreadType caller); |
|
|
|
// @exception Permission |
|
void unlock(Uint32 mode, ThreadType caller); |
|
| |
private: | private: |
AtomicInt _readers; |
|
AtomicInt _writers; |
|
ReadWriteSemRep _rwlock; | ReadWriteSemRep _rwlock; |
friend void extricate_read_write(void *); |
|
}; | }; |
| |
//============================================================================== | //============================================================================== |
|
|
| |
ReadLock(ReadWriteSem& rwsem) : _rwsem(rwsem) | ReadLock(ReadWriteSem& rwsem) : _rwsem(rwsem) |
{ | { |
_rwsem.wait_read(Threads::self()); |
_rwsem.waitRead(); |
} | } |
| |
~ReadLock() | ~ReadLock() |
{ | { |
_rwsem.unlock_read(Threads::self()); |
_rwsem.unlockRead(); |
} | } |
| |
private: | private: |
|
|
| |
WriteLock(ReadWriteSem& rwsem) : _rwsem(rwsem) | WriteLock(ReadWriteSem& rwsem) : _rwsem(rwsem) |
{ | { |
_rwsem.wait_write(Threads::self()); |
_rwsem.waitWrite(); |
} | } |
| |
~WriteLock() | ~WriteLock() |
{ | { |
_rwsem.unlock_write(Threads::self()); |
_rwsem.unlockWrite(); |
} | } |
| |
private: | private: |