Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   Related Pages  

Wefts::RRWMutex Class Reference

This is an advanced (and fully reentrant) read/write mutex. More...

#include <wefts_rrwmutex.h>

Inheritance diagram for Wefts::RRWMutex:

Inheritance graph
[legend]
Collaboration diagram for Wefts::RRWMutex:

Collaboration graph
[legend]
List of all members.

Public Methods

 RRWMutex ()
 Initializes the internal members of the mutex.

virtual void lockRead ()
 Locks the mutex for reading.

virtual void promote (bool fence)
 Promote the lock from read only to readwrite.

virtual void promote ()
 Promote the locks from read only to readwrite.

virtual void lockWrite (bool fence)
 Locks the mutex for writing.

virtual void lockWrite ()
 Locks the mutex for writing.

virtual void unlock ()
 Unlocks the mutex.


Private Attributes

volatile int m_fence
pthread_t m_owner

Detailed Description

This is an advanced (and fully reentrant) read/write mutex.

Read lock can be achieved as long as only read locks have been achieved; write lock can be achieved only by one thread, and only if no read locks area currently held.

In this mutex kind, write locks are reentrant. Also, a write locker can require to rise a lock fence, thus blocking any further request of new read lockers. This means that no new lock will be allowed on the mutex, and the write locker will take onwership of this as soon as the last reader that had control over it finishes its operations.

This mutex class ereditates the read lock promotion ability of its ancestor. It is also possible to promote a read-lock to write-lock and rising the fence.

All mutex classes are implemented via inline calls to maximize execution speed; also, their metods are extremely small.


Constructor & Destructor Documentation

Wefts::RRWMutex::RRWMutex   [inline]
 

Initializes the internal members of the mutex.


Member Function Documentation

virtual void Wefts::RRWMutex::lockRead   [inline, virtual]
 

Locks the mutex for reading.

Any amount of read locks can be achieved by the calling thread or by other read-locking threads. After that a read lock has been achieved, write lock requests will be blocked untill all the read lockers have relased their locks. If a write locker has risen the read fence, this function will block the calling thread also if the writer is WAITING to achieve the lock.

Reimplemented from Wefts::RWMutex.

virtual void Wefts::RRWMutex::lockWrite   [inline, virtual]
 

Locks the mutex for writing.

Equivalent to call method lockWrite( false ) (without read lock fence).

Reimplemented from Wefts::RWMutex.

virtual void Wefts::RRWMutex::lockWrite bool    fence [inline, virtual]
 

Locks the mutex for writing.

After this call has returned any request of lock (both read or write) will cause blocking of the caller until the owner of this thread releases the lock. This method is not reentrant, so never call lockWrite() twice from the same thread.

Parameters:
fence  if true, rises the read lock fence: any call to read lock after this will be locked.

virtual void Wefts::RRWMutex::promote   [inline, virtual]
 

Promote the locks from read only to readwrite.

Often, threads that have a read only lock on some data, after inspecting that data, find out that they need to alter it. Releasing the readLock and getting a write lock is not the optimal solution, both because it can cause unnecessary context switch, and because the situation of the data may change while the data is unlocked, requiring then a new rescan of the data to check if there is still a need for this thread to write it.

Another solution may be locking for writing the whole scanning code, if there is any possibility that, sometimes, a write is also needed.

But the most elegant solution is to promote a lock from read-only to read-write, so that no other writer is allowed to proceed before this thread can chage the data, but that all the other readers are done before this newly promoted thread can work.

Note:
This mutex must be locked for reading before issuing a promote, or an assertion error will be risen.

Reimplemented from Wefts::RWMutex.

virtual void Wefts::RRWMutex::promote bool    fence [inline, virtual]
 

Promote the lock from read only to readwrite.

This methods works like the one provided in its ancestor, but it has also the ability to rise the fence, to prevent further readers to gain control of the mutex while this thread is waiting to be promoted.

Note:
This mutex must be locked for reading before issuing a promote, or an assertion error will be risen.
Parameters:
fence  true to block further readers trying to achieve the lock, false otherwise
See also:
RWMutex::promote()

virtual void Wefts::RRWMutex::unlock   [inline, virtual]
 

Unlocks the mutex.

Releases the lock held by the calling thread (be it a read-only lock or a write lock). If the mutex is locked for writing and the calling thread is not the owner of the lock, this call is ignored.

Reimplemented from Wefts::RWMutex.


Member Data Documentation

volatile int Wefts::RRWMutex::m_fence [private]
 

pthread_t Wefts::RRWMutex::m_owner [private]
 


The documentation for this class was generated from the following file:
Generated on Tue Aug 5 18:09:03 2003 for Wefts by doxygen1.2.18