UCommon
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes
ucommon::Semaphore Class Reference

A portable counting semaphore class. More...

#include <condition.h>

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

Public Types

typedef autoshared< Semaphoreautosync
 

Public Member Functions

void operator++ (void)
 Convenience operator to wait on a counting semaphore.
 
void operator-- (void)
 Convenience operator to release a counting semaphore.
 
void release (void)
 Release the semaphore after waiting for it.
 
 Semaphore (unsigned count=0)
 Construct a semaphore with an initial count of threads to permit. More...
 
 Semaphore (unsigned count, unsigned avail)
 Alternate onstructor with ability to preset available slots. More...
 
void set (unsigned count)
 Alter semaphore limit at runtime. More...
 
void wait (void)
 Wait until the semphore usage count is less than the thread limit. More...
 
bool wait (timeout_t timeout)
 Wait until the semphore usage count is less than the thread limit. More...
 
- Public Member Functions inherited from ucommon::SharedProtocol
virtual void exclusive (void)
 Convert object to an exclusive lock. More...
 
virtual void share (void)
 Share the lock with other referencers. More...
 

Protected Member Functions

virtual void _share (void)
 Access interface to share lock the object.
 
virtual void _unshare (void)
 
Semaphoreoperator= (const Semaphore &)
 
 Semaphore (const Semaphore &)
 
- Protected Member Functions inherited from ucommon::Conditional
void broadcast (void)
 Signal the conditional to release all waiting threads.
 
 Conditional ()
 Initialize and construct conditional.
 
void signal (void)
 Signal the conditional to release one waiting thread.
 
bool wait (timeout_t timeout)
 Conditional wait for signal on millisecond timeout. More...
 
bool wait (struct timespec *timeout)
 Conditional wait for signal on timespec timeout. More...
 
void wait (void)
 Wait (block) until signalled.
 
 ~Conditional ()
 Destroy conditional, release any blocked threads.
 
- Protected Member Functions inherited from ucommon::ConditionMutex
 ConditionMutex ()
 Initialize and construct conditional.
 
void lock (void)
 Lock the conditional's supporting mutex.
 
void unlock (void)
 Unlock the conditional's supporting mutex.
 
 ~ConditionMutex ()
 Destroy conditional, release any blocked threads.
 

Protected Attributes

unsigned count
 
unsigned used
 
unsigned waits
 
- Protected Attributes inherited from ucommon::Conditional
pthread_cond_t cond
 
- Protected Attributes inherited from ucommon::ConditionMutex
pthread_mutex_t mutex
 

Additional Inherited Members

- Static Protected Member Functions inherited from ucommon::Conditional
static pthread_condattr_t * initializer (void)
 Support function for getting conditional attributes for realtime scheduling. More...
 
static void set (struct timespec *hires, timeout_t timeout)
 Convert a millisecond timeout into use for high resolution conditional timers. More...
 
- Static Protected Attributes inherited from ucommon::Conditional
static attribute attr
 

Detailed Description

A portable counting semaphore class.

A semaphore will allow threads to pass through it until the count is reached, and blocks further threads. Unlike pthread semaphore, our semaphore class supports it's count limit to be altered during runtime and the use of timed waits. This class also implements the shared_lock protocol.

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

Definition at line 655 of file condition.h.

Constructor & Destructor Documentation

◆ Semaphore() [1/2]

ucommon::Semaphore::Semaphore ( unsigned  count = 0)

Construct a semaphore with an initial count of threads to permit.

Parameters
countof threads to permit, or special case 0 group release.

◆ Semaphore() [2/2]

ucommon::Semaphore::Semaphore ( unsigned  count,
unsigned  avail 
)

Alternate onstructor with ability to preset available slots.

Parameters
countof threads to permit.
availinstances not pre-locked.

Member Function Documentation

◆ set()

void ucommon::Semaphore::set ( unsigned  count)

Alter semaphore limit at runtime.

Parameters
countof threads to allow.

◆ wait() [1/2]

void ucommon::Semaphore::wait ( void  )

Wait until the semphore usage count is less than the thread limit.

Increase used count for our thread when unblocked.

◆ wait() [2/2]

bool ucommon::Semaphore::wait ( timeout_t  timeout)

Wait until the semphore usage count is less than the thread limit.

Increase used count for our thread when unblocked, or return without changing if timed out.

Parameters
timeoutto wait in millseconds.
Returns
true if success, false if timeout.

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