|
reSIProcate/rutil
9694
|
Wraps the readers/writers mutex implementation on your platform. More...
#include <RWMutex.hxx>


Public Member Functions | |
| RWMutex () | |
| ~RWMutex () | |
| void | readlock () |
| void | writelock () |
| void | lock () |
| void | unlock () |
| unsigned int | readerCount () const |
| unsigned int | pendingWriterCount () const |
Private Attributes | |
| Mutex | mMutex |
| Condition | mReadCondition |
| Condition | mPendingWriteCondition |
| unsigned int | mReaderCount |
| bool | mWriterHasLock |
| unsigned int | mPendingWriterCount |
Wraps the readers/writers mutex implementation on your platform.
Usually, if a thread attempts to aquire a write-lock while read-locks are being held, this will prevent any further read-locks from being obtained, until the write-lock is aquired and released. This prevents the "writer starvation" problem.
Definition at line 29 of file RWMutex.hxx.
| RWMutex::RWMutex | ( | ) |
Definition at line 58 of file RWMutex.cxx.
: Lockable(), mReaderCount(0), mWriterHasLock(false), mPendingWriterCount(0) { }
| RWMutex::~RWMutex | ( | ) |
Definition at line 67 of file RWMutex.cxx.
{
}
| void RWMutex::lock | ( | ) | [virtual] |
Implements resip::Lockable.
Definition at line 105 of file RWMutex.cxx.
References writelock().
Referenced by readlock(), unlock(), and writelock().
{
writelock();
}

| unsigned int RWMutex::pendingWriterCount | ( | ) | const |
Definition at line 162 of file RWMutex.cxx.
References mPendingWriterCount.
{
return ( mPendingWriterCount );
}
| unsigned int RWMutex::readerCount | ( | ) | const |
| void RWMutex::readlock | ( | ) | [virtual] |
Reimplemented from resip::Lockable.
Definition at line 73 of file RWMutex.cxx.
References lock(), mMutex, mPendingWriterCount, mReadCondition, mReaderCount, mWriterHasLock, and resip::Condition::wait().
{
Lock lock(mMutex);
while ( mWriterHasLock || mPendingWriterCount > 0 )
{
mReadCondition.wait(mMutex);
}
mReaderCount++;
}

| void RWMutex::unlock | ( | ) | [virtual] |
Implements resip::Lockable.
Definition at line 112 of file RWMutex.cxx.
References resip::Condition::broadcast(), lock(), mMutex, mPendingWriteCondition, mPendingWriterCount, mReadCondition, mReaderCount, mWriterHasLock, and resip::Condition::signal().
{
Lock lock(mMutex);
// Unlocking a write lock.
//
if ( mWriterHasLock )
{
assert( mReaderCount == 0 );
mWriterHasLock = false;
// Pending writers have priority. Could potentially starve readers.
//
if ( mPendingWriterCount > 0 )
{
mPendingWriteCondition.signal();
}
// No writer, no pending writers, so all the readers can go.
//
else
{
mReadCondition.broadcast();
}
}
// Unlocking a read lock.
//
else
{
assert( mReaderCount > 0 );
mReaderCount--;
if ( mReaderCount == 0 && mPendingWriterCount > 0 )
{
mPendingWriteCondition.signal();
}
}
}

| void RWMutex::writelock | ( | ) | [virtual] |
Reimplemented from resip::Lockable.
Definition at line 87 of file RWMutex.cxx.
References lock(), mMutex, mPendingWriteCondition, mPendingWriterCount, mReaderCount, mWriterHasLock, and resip::Condition::wait().
Referenced by lock().
{
Lock lock(mMutex);
mPendingWriterCount++;
while ( mWriterHasLock || mReaderCount > 0 )
{
mPendingWriteCondition.wait(mMutex);
}
mPendingWriterCount--;
mWriterHasLock = true;
}

Mutex resip::RWMutex::mMutex [private] |
Definition at line 42 of file RWMutex.hxx.
Referenced by readlock(), unlock(), and writelock().
Definition at line 44 of file RWMutex.hxx.
Referenced by unlock(), and writelock().
unsigned int resip::RWMutex::mPendingWriterCount [private] |
Definition at line 47 of file RWMutex.hxx.
Referenced by pendingWriterCount(), readlock(), unlock(), and writelock().
Condition resip::RWMutex::mReadCondition [private] |
Definition at line 43 of file RWMutex.hxx.
Referenced by readlock(), and unlock().
unsigned int resip::RWMutex::mReaderCount [private] |
Definition at line 45 of file RWMutex.hxx.
Referenced by readerCount(), readlock(), unlock(), and writelock().
bool resip::RWMutex::mWriterHasLock [private] |
Definition at line 46 of file RWMutex.hxx.
Referenced by readlock(), unlock(), and writelock().
1.7.5.1