UCommon
ucommon::RWLock Class Reference

A generic and portable implementation of Read/Write locking. More...

#include <thread.h>

Inheritance diagram for ucommon::RWLock:
Collaboration diagram for ucommon::RWLock:

Data Structures

class  reader
 Apply automatic scope based access locking to objects. More...
 
class  writer
 Apply automatic scope based exclusive locking to objects. More...
 

Public Types

typedef autoshared< RWLockautoreader
 
typedef autoexclusive< RWLockautowriter
 

Public Member Functions

bool access (timeout_t timeout=Timer::inf)
 Request shared (read) access through the lock.
 
bool modify (timeout_t timeout=Timer::inf)
 Request modify (write) access through the lock.
 
void release (void)
 Release the lock.
 
 RWLock ()
 Create an instance of a rwlock.
 
- Public Member Functions inherited from ucommon::SharedProtocol
virtual void exclusive (void)
 Convert object to an exclusive lock.
 
virtual void share (void)
 Share the lock with other referencers.
 

Static Public Member Functions

static void indexing (unsigned size)
 Specify hash table size for guard protection.
 
static bool release (const void *object)
 Release an arbitrary object that has been protected by a rwlock.
 

Protected Member Functions

virtual void _lock (void)
 
virtual void _share (void)
 Access interface to share lock the object.
 
virtual void _unlock (void)
 
virtual void _unshare (void)
 

Protected Attributes

pthread_t writeid
 
unsigned writers
 

Detailed Description

A generic and portable implementation of Read/Write locking.

This class implements classical read/write locking, including "timed" locks. Support for scheduling threads to avoid writer starvation is also provided for. By building read/write locks from a conditional, we make them available on pthread implemetations and other platforms which do not normally include optional pthread rwlock's. We also do not restrict the number of threads that may use the lock. Finally, both the exclusive and shared protocols are implemented to support exclusive_lock and shared_lock referencing. Because of the thread locking semantics this is part of thread rather than condition, and was originally called ThreadLock in older ucommon/commoncpp releases. Our autolock semantics are also different as we protect a target object, not a rwlock instance.

Author
David Sugar dyfet.nosp@m.@gnu.nosp@m.telep.nosp@m.hony.nosp@m..org

Definition at line 83 of file thread.h.

Member Typedef Documentation

◆ autoreader

typedef autoshared<RWLock> ucommon::RWLock::autoreader

Definition at line 101 of file thread.h.

◆ autowriter

typedef autoexclusive<RWLock> ucommon::RWLock::autowriter

Definition at line 103 of file thread.h.

Member Function Documentation

◆ _lock()

virtual void ucommon::RWLock::_lock ( void )
protectedvirtual

◆ _share()

virtual void ucommon::RWLock::_share ( void )
protectedvirtual

Access interface to share lock the object.

Implements ucommon::SharedProtocol.

◆ _unlock()

virtual void ucommon::RWLock::_unlock ( void )
protectedvirtual

◆ _unshare()

virtual void ucommon::RWLock::_unshare ( void )
protectedvirtual

◆ access()

bool ucommon::RWLock::access ( timeout_t timeout = Timer::inf)

Request shared (read) access through the lock.

Parameters
timeoutin milliseconds to wait for lock.
Returns
true if locked, false if timeout.

◆ indexing()

static void ucommon::RWLock::indexing ( unsigned size)
static

Specify hash table size for guard protection.

The default is 1. This should be called at initialization time from the main thread of the application before any other threads are created.

Parameters
sizeof hash table used for guarding.

◆ modify()

bool ucommon::RWLock::modify ( timeout_t timeout = Timer::inf)

Request modify (write) access through the lock.

Parameters
timeoutin milliseconds to wait for lock.
Returns
true if locked, false if timeout.

◆ release()

static bool ucommon::RWLock::release ( const void * object)
static

Release an arbitrary object that has been protected by a rwlock.

Parameters
objectto release.

Field Documentation

◆ writeid

pthread_t ucommon::RWLock::writeid
protected

Definition at line 90 of file thread.h.

◆ writers

unsigned ucommon::RWLock::writers
protected

Definition at line 89 of file thread.h.


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