OSB_LIB::RwMutex Class Reference

Collaboration diagram for OSB_LIB::RwMutex:

Collaboration graph
[legend]
List of all members.

Detailed Description

Read-Write mutex.

The class provides a mutex that allows for many readers or one writer.

A flag, that is set in the constructor, determines whether waiting readers or waiting writers are given priority if the mutex is unlocked. The behaviour is as follows:
If readers are preferred, new readers will be allowed if the mutex is currently not locked for writing. A write lock can only be aquired if no readers hold or are waiting for a lock.
If writers are preferred, a read lock can only be aquired when no writers are requesting or holding locks.

A possible enhancement is to allow for recursive locks of the mutex. This may also include to allow a thread, currently holding a read lock, to aquire a write lock.

Definition at line 166 of file mutex.h.

Public Types

Public Member Functions

Private Types

Private Member Functions

Private Attributes


Member Typedef Documentation

typedef std::deque<LockType> OSB_LIB::RwMutex::LockQueue [private]

Type used to queue waiting lock requests.

Definition at line 287 of file mutex.h.


Member Enumeration Documentation

enum OSB_LIB::RwMutex::LockType [private]

Type of (requested) lock.

Enumerator:
ltRead 
ltWrite 

Definition at line 285 of file mutex.h.

enum OSB_LIB::RwMutex::Priority

Priority defines whether waiting readers or writers are perferred or none.

Enumerator:
prioRead  Perfer waiting readers.
prioWrite  Perfer waiting writers.
prioQueue  Lock requests are queued by type, i.e. reader or writer.
When the mutex is unlocked, priority is granted to the lock type that is at the front of the queue.

Definition at line 172 of file mutex.h.


Constructor & Destructor Documentation

OSB_LIB::RwMutex::RwMutex ( Priority  priority = prioRead,
const pthread_mutexattr_t *  attr = 0 
) [explicit]

Constructor with priority setting.

Parameters:
priority Flag if priority is given to waiting writers or readers or in the order of requests.
attr Posix mutex attributes used for initalization of `mutex_', see pthread_mutex_init for details.

OSB_LIB::RwMutex::~RwMutex (  ) 

Destructor: Destroys the mutex and the conditions.

OSB_LIB::RwMutex::RwMutex ( const RwMutex  )  [private]

Prevent copying: not implemented.


Member Function Documentation

void OSB_LIB::RwMutex::addWait ( LockType  lt  )  [private]

Add a waiting request.

bool OSB_LIB::RwMutex::canReadLock (  )  [private]

Check if read lock is possible.

bool OSB_LIB::RwMutex::canWriteLock (  )  [private]

Check if write lock is possible.

RwMutex& OSB_LIB::RwMutex::operator= ( const RwMutex  )  [private]

Prevent assignment: not implemented.

int OSB_LIB::RwMutex::readLock (  ) 

Get a read lock.

The function gets a read lock. It blocks until the lock is successfully aquired or an error occurs.

Returns:
0 in case of success or a non-zero value on error. (The value returned is actually the one from pthread_mutex_lock or pthread_mutex_unlock.)

Referenced by OSB_LIB::ResourceList< R >::readLock(), and OSB_LIB::RwShared< T >::readLock().

int OSB_LIB::RwMutex::readUnlock (  ) 

Release a read lock.

Returns:
0 in case of success or a non-zero value on error. (The value returned is actually the one from pthread_mutex_lock or pthread_mutex_unlock.)

Referenced by OSB_LIB::ResourceList< R >::readLock(), and OSB_LIB::RwShared< T >::readUnlock().

void OSB_LIB::RwMutex::rmWait ( LockType  lt  )  [private]

Remove a waiting request.

void OSB_LIB::RwMutex::sendCondition (  )  [private]

Signal read or write condition.

int OSB_LIB::RwMutex::tryReadLock (  ) 

Attempts to get a read lock without waiting.

The function tries to get a read lock. It returns immediately if this is not possible, e.g. because a writer currently owns the lock.

The lock will succeed under the following conditions: ToDo.

Returns:
0 in case of success, else a non-zero value:
  • EBUSY if a read lock could not be aquired,
  • as specified for pthread_mutex_lock and pthread_mutex_unlock.

Referenced by OSB_LIB::RwShared< T >::tryReadLock().

int OSB_LIB::RwMutex::tryWriteLock (  ) 

Attempts to get a write lock without waiting.

The function tries to get a write lock. It returns immediately if this is not possible, e.g., because one or more readers are currently holding a lock.

The lock will succeed under the following conditions: ToDo.

Returns:
0 in case of success, else a non-zero value:
  • EBUSY if a read lock could not be aquired,
  • as specified for pthread_mutex_lock and pthread_mutex_unlock.

Referenced by OSB_LIB::RwShared< T >::tryWriteLock().

int OSB_LIB::RwMutex::writeLock (  ) 

Get a write lock.

The function gets a write lock. It blocks until the lock is successfully aquired or an error occurs.

Returns:
0 in case of success or a non-zero value on error. (The value returned is actually the one from pthread_mutex_lock or pthread_mutex_unlock.)

Referenced by OSB_LIB::ResourceList< R >::writeLock(), and OSB_LIB::RwShared< T >::writeLock().

int OSB_LIB::RwMutex::writeUnlock (  ) 

Release a write lock.

Returns:
0 in case of success or a non-zero value on error. (The value returned is actually the one from pthread_mutex_lock or pthread_mutex_unlock.)

Referenced by OSB_LIB::ResourceList< R >::writeLock(), and OSB_LIB::RwShared< T >::writeUnlock().


Member Data Documentation

pthread_cond_t OSB_LIB::RwMutex::condRead_ [private]

Condition that is signalled when reading is possible.

Definition at line 292 of file mutex.h.

pthread_cond_t OSB_LIB::RwMutex::condWrite_ [private]

Condition that is signalled when writing is possible.

Definition at line 294 of file mutex.h.

int OSB_LIB::RwMutex::curReaders_ [private]

Number of current readers.

Definition at line 295 of file mutex.h.

int OSB_LIB::RwMutex::curWriters_ [private]

Number of current writers.

Definition at line 296 of file mutex.h.

LockQueue OSB_LIB::RwMutex::lockQueue_ [private]

Queue with waiting requests.

Definition at line 303 of file mutex.h.

pthread_mutex_t OSB_LIB::RwMutex::mutex_ [private]

Posix mutex to protect the object data.

Definition at line 290 of file mutex.h.

Priority OSB_LIB::RwMutex::priority_ [private]

Flag if priority is given to waiting writers or readers or in the order of the lock requests.

Definition at line 301 of file mutex.h.


The documentation for this class was generated from the following file:
Generated on Sat Sep 2 14:11:34 2006 for OSB Library by  doxygen 1.4.7