Orocos Real-Time Toolkit  2.9.0
Public Member Functions | Protected Types | Static Protected Member Functions | Protected Attributes | Static Protected Attributes | List of all members
RTT::os::SharedMutex Class Reference

An object oriented wrapper around a shared mutex (multiple readers allowed, but only one writer with exclusive access). More...

#include <rtt/os/Mutex.hpp>

Inheritance diagram for RTT::os::SharedMutex:
RTT::os::MutexInterface

Public Member Functions

 SharedMutex ()
 Initialize a shared Mutex. More...
 
virtual ~SharedMutex ()
 Destroy a shared Mutex. More...
 
virtual void lock ()
 
virtual void unlock ()
 
virtual bool trylock ()
 Try to lock this mutex exclusively. More...
 
virtual bool timedlock (Seconds s)
 Lock this mutex exclusively, but don't wait longer for the lock than the specified timeout. More...
 
void lock_shared ()
 Obtain shared ownership of this mutex. More...
 
virtual void unlock_shared ()
 Release shared ownership of this mutex. More...
 
virtual bool trylock_shared ()
 Attempt to obtain shared ownership of this mutex. More...
 
virtual bool timedlock_shared (Seconds s)
 Attempt to obtain shared ownership of this mutex, but don't wait longer for the lock than the specified timeout. More...
 

Protected Types

typedef unsigned int Status
 

Static Protected Member Functions

static unsigned int readers (Status status)
 
static unsigned int waitToRead (Status status)
 
static unsigned int writers (Status status)
 

Protected Attributes

oro_atomic_t status
 
Semaphore read_semaphore
 
Semaphore write_semaphore
 

Static Protected Attributes

static const Status one_reader = (1 << 0)
 
static const Status one_wait_to_read = (1 << 10)
 
static const Status one_writer = (1 << 20)
 
static const Status status_mask = (1 << 10) - 1
 

Detailed Description

An object oriented wrapper around a shared mutex (multiple readers allowed, but only one writer with exclusive access).

A mutex can only be unlock()'ed, by the thread which lock()'ed it. A trylock is a non blocking lock action which fails or succeeds.

Implementation is motivated by http://preshing.com/20150316/semaphores-are-surprisingly-versatile/#a-lightweight-semaphore-with-partial-spinning.

See also
MutexLock, MutexTryLock, SharedMutexLock, SharedMutexTryLock, Mutex

Definition at line 346 of file Mutex.hpp.

Member Typedef Documentation

typedef unsigned int RTT::os::SharedMutex::Status
protected

Definition at line 349 of file Mutex.hpp.

Constructor & Destructor Documentation

RTT::os::SharedMutex::SharedMutex ( )
inline

Initialize a shared Mutex.

Definition at line 373 of file Mutex.hpp.

References ORO_ATOMIC_SETUP().

virtual RTT::os::SharedMutex::~SharedMutex ( )
inlinevirtual

Destroy a shared Mutex.

Definition at line 383 of file Mutex.hpp.

References ORO_ATOMIC_CLEANUP().

Member Function Documentation

virtual void RTT::os::SharedMutex::lock ( )
inlinevirtual

Implements RTT::os::MutexInterface.

Definition at line 388 of file Mutex.hpp.

References RTT::os::CAS(), oro_atomic_read(), and RTT::os::Semaphore::wait().

void RTT::os::SharedMutex::lock_shared ( )
inlinevirtual

Obtain shared ownership of this mutex.

Reimplemented from RTT::os::MutexInterface.

Definition at line 457 of file Mutex.hpp.

References RTT::os::CAS(), oro_atomic_read(), and RTT::os::Semaphore::wait().

static unsigned int RTT::os::SharedMutex::readers ( Status  status)
inlinestaticprotected

Definition at line 359 of file Mutex.hpp.

virtual bool RTT::os::SharedMutex::timedlock ( Seconds  s)
inlinevirtual

Lock this mutex exclusively, but don't wait longer for the lock than the specified timeout.

Parameters
sThe maximum time to wait before aqcuiring the lock.
Note
not implemented for this type of mutex
Returns
false

Implements RTT::os::MutexInterface.

Definition at line 448 of file Mutex.hpp.

virtual bool RTT::os::SharedMutex::timedlock_shared ( Seconds  s)
inlinevirtual

Attempt to obtain shared ownership of this mutex, but don't wait longer for the lock than the specified timeout.

Parameters
sThe maximum time to wait before aqcuiring the lock.
Note
not implemented for this type of mutex
Returns
false

Reimplemented from RTT::os::MutexInterface.

Definition at line 515 of file Mutex.hpp.

virtual bool RTT::os::SharedMutex::trylock ( )
inlinevirtual

Try to lock this mutex exclusively.

Note
not implemented for this type of mutex
Returns
false

Implements RTT::os::MutexInterface.

Definition at line 434 of file Mutex.hpp.

virtual bool RTT::os::SharedMutex::trylock_shared ( )
inlinevirtual

Attempt to obtain shared ownership of this mutex.

Note
not implemented for this type of mutex
Returns
false

Reimplemented from RTT::os::MutexInterface.

Definition at line 501 of file Mutex.hpp.

virtual void RTT::os::SharedMutex::unlock ( )
inlinevirtual

Implements RTT::os::MutexInterface.

Definition at line 402 of file Mutex.hpp.

References RTT::os::CAS(), oro_atomic_read(), and RTT::os::Semaphore::signal().

virtual void RTT::os::SharedMutex::unlock_shared ( )
inlinevirtual

Release shared ownership of this mutex.

Reimplemented from RTT::os::MutexInterface.

Definition at line 480 of file Mutex.hpp.

References RTT::os::CAS(), oro_atomic_read(), and RTT::os::Semaphore::signal().

static unsigned int RTT::os::SharedMutex::waitToRead ( Status  status)
inlinestaticprotected

Definition at line 362 of file Mutex.hpp.

static unsigned int RTT::os::SharedMutex::writers ( Status  status)
inlinestaticprotected

Definition at line 365 of file Mutex.hpp.

Member Data Documentation

const Status RTT::os::SharedMutex::one_reader = (1 << 0)
staticprotected

Definition at line 354 of file Mutex.hpp.

const Status RTT::os::SharedMutex::one_wait_to_read = (1 << 10)
staticprotected

Definition at line 355 of file Mutex.hpp.

const Status RTT::os::SharedMutex::one_writer = (1 << 20)
staticprotected

Definition at line 356 of file Mutex.hpp.

Semaphore RTT::os::SharedMutex::read_semaphore
protected

Definition at line 351 of file Mutex.hpp.

oro_atomic_t RTT::os::SharedMutex::status
protected

Definition at line 350 of file Mutex.hpp.

const Status RTT::os::SharedMutex::status_mask = (1 << 10) - 1
staticprotected

Definition at line 357 of file Mutex.hpp.

Semaphore RTT::os::SharedMutex::write_semaphore
protected

Definition at line 352 of file Mutex.hpp.


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