fesa-core  5.0.1
fesa::Semaphore Class Reference

This class is a wrapper around POSIX semaphores. More...

#include <Semaphore.h>

Inherits noncopyable.

Public Member Functions

 Semaphore (const std::string &name, int value)
 Constructor Create a new or open an existing semaphore identified by its name. More...
 
 Semaphore (int value)
 
 ~Semaphore ()
 Destructor Close and unlink the semaphore.
 
void wait ()
 Wait in this thread until the value of the semaphore is greater than 0. Then decrement (lock) the semaphore. More...
 
int tryWait ()
 Decrement (lock) the semaphore if its value is greater than 0; otherwise return. More...
 
int wait (std::time_t timeoutSec, std::time_t timeoutNanoSec)
 Waits in this thread until the value of the semaphore is greater than 0 or the timeout is over. More...
 
void post ()
 Increment (unlock) the semaphore. More...
 
int getValue ()
 Return the current value of the semaphore. More...
 
void unlink ()
 Destroy the named semaphore. The semaphore name is removed immediately. The semaphore is destroyed once all other processes that have the semaphore open close it. More...
 

Detailed Description

This class is a wrapper around POSIX semaphores.

Constructor & Destructor Documentation

fesa::Semaphore::Semaphore ( const std::string &  name,
int  value 
)

Constructor Create a new or open an existing semaphore identified by its name.

Parameters
nameThe name of the semaphore
valueThe initial value of the semaphore
Exceptions
FesaExceptionif the semaphore couldn't be created or opened
fesa::Semaphore::Semaphore ( int  value)

Constructor which creates an unnamed, process private semaphore.

Member Function Documentation

int fesa::Semaphore::getValue ( )

Return the current value of the semaphore.

Exceptions
FesaException
void fesa::Semaphore::post ( )

Increment (unlock) the semaphore.

Exceptions
FesaException
int fesa::Semaphore::tryWait ( )

Decrement (lock) the semaphore if its value is greater than 0; otherwise return.

Returns
0 if the semaphore was decremented
-1 if the semaphore wasn't decremented, i.e. its value is 0
Exceptions
FesaException
void fesa::Semaphore::unlink ( )

Destroy the named semaphore. The semaphore name is removed immediately. The semaphore is destroyed once all other processes that have the semaphore open close it.

Warning
Never use this memeber function nor remove it (it is here for testing and documentation purposes). Using it may create race conditions.
Consider the following example:
The RT process is shutting down, the server process is starting up.
The RT process opens the named inter-process semaphore to destroy the IPC message queues, the sever process opens the semaphore to start the initialization
(e.g. to create the IPC message queues and the shared memory).
The RT process is the first to lock the semaphore, the server process is waiting for it.
The RT process signals the semaphore, unlinks it, and shuts down, the sever process locks the semaphore.
The RT process starts up and opens a semaphore with the same name as the previous semaphore but a different object.
As a result, the initializatio may be broken, e.g. the shared memory may be corrupted.
void fesa::Semaphore::wait ( )

Wait in this thread until the value of the semaphore is greater than 0. Then decrement (lock) the semaphore.

Exceptions
FesaException
int fesa::Semaphore::wait ( std::time_t  timeoutSec,
std::time_t  timeoutNanoSec 
)

Waits in this thread until the value of the semaphore is greater than 0 or the timeout is over.

Parameters
timeoutSecmaximum number of seconds to wait
timeoutNanoSecmaximum number of nanoseconds to wait
Exceptions
FesaException
Returns
0 if semaphore could be decremented before the timeout elapsed; ETIMEDOUT otherwise.

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