reSIProcate/rutil  9694
Public Member Functions | Private Attributes
resip::RWMutex Class Reference

Wraps the readers/writers mutex implementation on your platform. More...

#include <RWMutex.hxx>

Inheritance diagram for resip::RWMutex:
Inheritance graph
[legend]
Collaboration diagram for resip::RWMutex:
Collaboration graph
[legend]

List of all members.

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

Detailed Description

Wraps the readers/writers mutex implementation on your platform.

Note:
A readers/writers mutex is a mutex that can be locked in two differing ways:
  • A read-lock: Prevents other threads from obtaining a write-lock, but not other read-locks.
  • A write-lock: Prevents other threads from obtaining either a read-lock or write-lock.

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.


Constructor & Destructor Documentation

RWMutex::RWMutex ( )

Definition at line 58 of file RWMutex.cxx.

RWMutex::~RWMutex ( )

Definition at line 67 of file RWMutex.cxx.

{
}

Member Function Documentation

void RWMutex::lock ( ) [virtual]

Implements resip::Lockable.

Definition at line 105 of file RWMutex.cxx.

References writelock().

Referenced by readlock(), unlock(), and writelock().

{
   writelock();
}

Here is the call graph for this function:

unsigned int RWMutex::pendingWriterCount ( ) const

Definition at line 162 of file RWMutex.cxx.

References mPendingWriterCount.

{
   return ( mPendingWriterCount );
}
unsigned int RWMutex::readerCount ( ) const

Definition at line 156 of file RWMutex.cxx.

References mReaderCount.

{
   return ( mReaderCount );
}
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().

Here is the call graph for this function:

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

Here is the call graph for this function:

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().

Here is the call graph for this function:


Member Data Documentation

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().

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().

Definition at line 46 of file RWMutex.hxx.

Referenced by readlock(), unlock(), and writelock().


The documentation for this class was generated from the following files: