This class is a wrapper around POSIX semaphores.
More...
#include <Semaphore.h>
Inherits noncopyable.
|
| 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...
|
|
This class is a wrapper around POSIX semaphores.
fesa::Semaphore::Semaphore |
( |
const std::string & |
name, |
|
|
int |
value |
|
) |
| |
Constructor Create a new or open an existing semaphore identified by its name.
- Parameters
-
name | The name of the semaphore |
value | The initial value of the semaphore |
- Exceptions
-
fesa::Semaphore::Semaphore |
( |
int |
value | ) |
|
Constructor which creates an unnamed, process private semaphore.
int fesa::Semaphore::getValue |
( |
| ) |
|
Return the current value of the semaphore.
- Exceptions
-
void fesa::Semaphore::post |
( |
| ) |
|
Increment (unlock) the semaphore.
- Exceptions
-
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
-
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
-
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
-
timeoutSec | maximum number of seconds to wait |
timeoutNanoSec | maximum number of nanoseconds to wait |
- Exceptions
-
- Returns
- 0 if semaphore could be decremented before the timeout elapsed; ETIMEDOUT otherwise.
The documentation for this class was generated from the following files:
- Semaphore.h
- Semaphore.cpp