UCommon
Data Structures | Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
ucommon::ThreadLock Class Reference

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

#include <thread.h>

Inheritance diagram for ucommon::ThreadLock:
Inheritance graph
[legend]
Collaboration diagram for ucommon::ThreadLock:
Collaboration graph
[legend]

Data Structures

class  gaurd_reader
 Gaurd class to apply scope based access locking to objects. More...
class  gaurd_writer
 Gaurd class to apply scope based exclusive locking to objects. More...

Public Member Functions

bool access (timeout_t timeout=Timer::inf)
 Request shared (read) access through the lock.
unsigned getAccess (void)
 Get the number of threads in shared access mode.
unsigned getModify (void)
 Get the number of threads waiting to modify the lock.
unsigned getWaiting (void)
 Get the number of threads waiting to access after writer completes.
bool modify (timeout_t timeout=Timer::inf)
 Request modify (write) access through the lock.
void release (void)
 Release the lock.
 ThreadLock ()
 Create an instance of a rwlock.
- Public Member Functions inherited from ucommon::ExclusiveProtocol
virtual void Exlock (void)=0
 Protocol interface to exclusive lock the object.
void Lock (void)
 A convenience member function for accessing the exclusive lock.
virtual void Unlock (void)=0
 Protocol interface to release a lock.
- Public Member Functions inherited from ucommon::SharedProtocol
virtual void Exclusive (void)
 Convert object to an exclusive lock.
void Lock (void)
 A convenience member function for accessing the shared lock.
virtual void Share (void)
 Share the lock with other referencers.
virtual void Shlock (void)=0
 Protocol interface to share lock the object.
virtual void Unlock (void)=0
 Protocol interface to release a lock.

Static Public Member Functions

static bool access (ThreadLock &lock, timeout_t timeout=Timer::inf)
 Convenience function to access (read lock) a rwlock.
static void indexing (unsigned size)
 Specify hash table size for gaurd protection.
static bool modify (ThreadLock &lock, timeout_t timeout=Timer::inf)
 Convenience function to modify (write lock) a rwlock.
static bool reader (void *object, timeout_t timeout=Timer::inf)
 Shared access to an arbitrary object.
static void release (void *object)
 Release an arbitrary object that has been protected by a rwlock.
static void release (ThreadLock &lock)
 Convenience function to release a rwlock.
static bool writer (void *object, timeout_t timeout=Timer::inf)
 Write protect access to an arbitrary object.

Protected Member Functions

void Exlock (void)
void Shlock (void)
void Unlock (void)

Protected Attributes

pthread_t writeid
unsigned writers

Additional Inherited Members

- Private Member Functions inherited from ucommon::ConditionalAccess
void access (void)
 Access mode shared thread scheduling.
void commit (void)
 Complete exclusive mode write scheduling.
 ConditionalAccess ()
 Initialize and construct conditional.
void limit_sharing (unsigned max)
 Specify a maximum sharing (access) limit.
void modify (void)
 Exclusive mode write thread scheduling.
void release (void)
 Release access mode read scheduling.
 ~ConditionalAccess ()
 Destroy conditional, release any blocked threads.
void broadcast (void)
 Signal the conditional to release all broadcast threads.
void lock (void)
 Lock the conditional's supporting mutex.
void signal (void)
 Signal the conditional to release one signalled thread.
void unlock (void)
 Unlock the conditional's supporting mutex.
bool waitBroadcast (timeout_t timeout)
 Conditional wait for broadcast on millisecond timeout.
bool waitBroadcast (struct timespec *timeout)
 Conditional wait for broadcast on timespec timeout.
void waitBroadcast (void)
 Wait (block) until broadcast.
bool waitSignal (timeout_t timeout)
 Conditional wait for signal on millisecond timeout.
bool waitSignal (struct timespec *timeout)
 Conditional wait for signal on timespec timeout.
void waitSignal (void)
 Wait (block) until signalled.

Detailed Description

A generic and portable implimentation of Read/Write locking.

This class impliments 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 implimetations 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 implimented to support exclusive_lock and shared_lock referencing.

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

Definition at line 547 of file thread.h.

Member Function Documentation

bool ucommon::ThreadLock::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.
static bool ucommon::ThreadLock::access ( ThreadLock lock,
timeout_t  timeout = Timer::inf 
)
inlinestatic

Convenience function to access (read lock) a rwlock.

Parameters
lockto access.
timeoutto wait for lock.
Returns
true if successful, false if timeout.

Definition at line 749 of file thread.h.

Here is the call graph for this function:

unsigned ucommon::ThreadLock::getAccess ( void  )

Get the number of threads in shared access mode.

Returns
number of accessing threads.
unsigned ucommon::ThreadLock::getModify ( void  )

Get the number of threads waiting to modify the lock.

Returns
number of pending write threads.
unsigned ucommon::ThreadLock::getWaiting ( void  )

Get the number of threads waiting to access after writer completes.

Returns
number of waiting access threads.
static void ucommon::ThreadLock::indexing ( unsigned  size)
static

Specify hash table size for gaurd 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 gaurding.
bool ucommon::ThreadLock::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.
static bool ucommon::ThreadLock::modify ( ThreadLock lock,
timeout_t  timeout = Timer::inf 
)
inlinestatic

Convenience function to modify (write lock) a rwlock.

Parameters
lockto modify.
timeoutto wait for lock.
Returns
true if successful, false if timeout.

Definition at line 740 of file thread.h.

Here is the call graph for this function:

static bool ucommon::ThreadLock::reader ( void *  object,
timeout_t  timeout = Timer::inf 
)
static

Shared access to an arbitrary object.

This is based on the protect function of mutex.

Parameters
objectto share.
timeoutin milliseconds to wait for lock.
Returns
true if shared, false if timeout.
static void ucommon::ThreadLock::release ( void *  object)
static

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

Parameters
objectto release.
static void ucommon::ThreadLock::release ( ThreadLock lock)
inlinestatic

Convenience function to release a rwlock.

Parameters
lockto release.

Definition at line 756 of file thread.h.

Here is the call graph for this function:

static bool ucommon::ThreadLock::writer ( void *  object,
timeout_t  timeout = Timer::inf 
)
static

Write protect access to an arbitrary object.

This is like the protect function of mutex.

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

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