Yate: Semaphore Class Reference

Yate

Public Member Functions | Static Public Member Functions | Friends | List of all members
Semaphore Class Reference

Semaphore implementation. More...

`#include <yateclass.h>`

Inheritance diagram for Semaphore:

## Public Member Functions
| Semaphore (unsigned int maxcount=1, const char *name=0, unsigned int initialCount=1)
| Semaphore (const Semaphore &original)
| ~Semaphore ()
Semaphore & | operator= (const Semaphore &original)
virtual bool | lock (long maxwait=-1)
virtual bool | unlock ()
virtual bool | locked () const
virtual Semaphore * | lockableSemaphore ()
Public Member Functions inherited from Lockable
virtual | ~Lockable ()
virtual bool | check (long maxwait=-1)
virtual bool | unlockAll ()
virtual Mutex * | lockableMutex ()
virtual RWLock * | lockableRWLock ()

## Static Public Member Functions
static int | count ()
static int | locks ()
static bool | efficientTimedLock ()
Static Public Member Functions inherited from Lockable
static void | wait (unsigned long maxwait)
static unsigned long | wait ()
static void | startUsingNow ()
static void | enableSafety (bool safe=true)
static bool | safety ()

## Friends
class | SemaphorePrivate

## Detailed Description

Semaphore implementation.

A semaphore object for synchronizing threads, can also be used as a token bucket

## Constructor & Destructor Documentation

## ◆ Semaphore() [1/2]

Semaphore | ( | unsigned int | maxcount = 1,
|  | const char * | name = 0,
|  | unsigned int | initialCount = 1 )
| explicit

Construct a new unlocked semaphore

Parameters

  maxcount | Maximum unlock count, must be strictly positive
  name | Static name of the semaphore (for debugging purpose only)
  initialCount | Initial semaphore count, must not be greater than maxcount

Referenced by lockableSemaphore(), operator=(), and Semaphore().

## ◆ Semaphore() [2/2]

Semaphore | ( | const Semaphore & | original | ) |

Copy constructor, creates a shared semaphore

Parameters

  original | Reference of the semaphore to share

References Semaphore().

## ◆ ~Semaphore()

~Semaphore | ( |  | ) |

Destroy the semaphore

## Member Function Documentation

## ◆ count()

int count | ( |  | ) |
| static

Get the number of semaphores counting the shared ones only once

Returns
  Count of individual semaphores

## ◆ efficientTimedLock()

bool efficientTimedLock | ( |  | ) |
| static

Check if a timed lock() is efficient on this platform

Returns
  True if a lock with a maxwait parameter is efficiently implemented

## ◆ lock()

virtual bool lock | ( | long | maxwait = -1 | ) |
| virtual

Attempt to get a lock on the semaphore and eventually wait for it

Parameters

  maxwait | Time in microseconds to wait, -1 wait forever

Returns
  True if successfully locked, false on failure

Implements Lockable.

## ◆ lockableSemaphore()

virtual Semaphore * lockableSemaphore | ( |  | ) |
| inlinevirtual

Retrieve a Semaphore from this object

Returns
  Semaphore pointer

Reimplemented from Lockable.

References Semaphore().

## ◆ locked()

virtual bool locked | ( |  | ) | const
| virtual

Check if the semaphore is currently locked (waiting) - as it's asynchronous it guarantees nothing if other thread changes status

Returns
  True if the semaphore was locked when the function was called

Implements Lockable.

## ◆ locks()

int locks | ( |  | ) |
| static

Get the number of currently locked (waiting) semaphores

Returns
  Count of locked semaphores, -1 if unknown (not tracked)

## ◆ operator=()

Semaphore & operator= | ( | const Semaphore & | original | ) |

Assignment operator makes the semaphore shared with the original

Parameters

  original | Reference of the semaphore to share

References Semaphore().

## ◆ unlock()

virtual bool unlock | ( |  | ) |
| virtual

Unlock the semaphore, does never wait nor get over counter maximum

Returns
  True if successfully unlocked

Implements Lockable.

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

- yateclass.h