|
VLink 2.0.0
A high-performance communication middleware
|
Named cross-process counting semaphore. 更多...
#include <sys_semaphore.h>
Public 成员函数 | |
| SysSemaphore (size_t count=0) | |
Constructs a SysSemaphore with the given initial count. | |
| ~SysSemaphore () | |
Destructor. Calls detach() if the semaphore is still attached. | |
| bool | attach (const std::string &name) |
| Creates or opens a named semaphore with the given name. | |
| bool | detach (bool force=true) |
| Closes the semaphore handle and optionally removes it from the OS namespace. | |
| bool | acquire (size_t n=1, int timeout_ms=kInfinite) |
Decrements the semaphore counter by n, blocking if necessary. | |
| void | release (size_t n=1) |
Increments the semaphore counter by n. | |
| bool | is_attached () const |
Returns true if the semaphore is currently attached to an OS name. | |
| size_t | get_count () const |
| Returns the current count of the semaphore. | |
静态 Public 属性 | |
| static constexpr int | kInfinite {-1} |
Sentinel value for acquire() meaning "wait indefinitely". | |
Named cross-process counting semaphore.
Backed by the OS named-semaphore API (POSIX sem_open or Win32 CreateSemaphore) to support synchronisation across process boundaries.
|
explicit |
Constructs a SysSemaphore with the given initial count.
The semaphore is not yet attached to any OS name. Call attach() before using acquire() or release().
| count | Initial count used when a new named semaphore is created by attach() (default: 0). |
| vlink::SysSemaphore::~SysSemaphore | ( | ) |
Destructor. Calls detach() if the semaphore is still attached.
| bool vlink::SysSemaphore::acquire | ( | size_t | n = 1, |
| int | timeout_ms = kInfinite ) |
Decrements the semaphore counter by n, blocking if necessary.
Blocks the caller until n permits are available or timeout_ms milliseconds elapse.
| n | Number of permits to acquire (default: 1). |
| timeout_ms | Maximum time to wait in milliseconds. Use kInfinite (-1) to wait indefinitely (default). |
true if permits were acquired, false on timeout or error.is_attached() must be true. | bool vlink::SysSemaphore::attach | ( | const std::string & | name | ) |
Creates or opens a named semaphore with the given name.
If a semaphore with name already exists in the OS namespace, it is opened and the constructor-provided initial count is ignored. If it does not exist it is created with that count.
| name | OS semaphore name (POSIX: must start with '/'). |
true on success, false if the semaphore could not be created or opened. | bool vlink::SysSemaphore::detach | ( | bool | force = true | ) |
Closes the semaphore handle and optionally removes it from the OS namespace.
After detach(), is_attached() returns false.
| force | If true, the semaphore is unlinked from the OS namespace (other processes lose access). Default: true. |
true on success, false if not attached or unlink failed.
|
nodiscard |
Returns the current count of the semaphore.
The value is a snapshot and may change immediately after the call. Intended for diagnostics only.
|
nodiscard |
Returns true if the semaphore is currently attached to an OS name.
true if attached, false otherwise. | void vlink::SysSemaphore::release | ( | size_t | n = 1 | ) |
Increments the semaphore counter by n.
Wakes at most n threads (from any process) blocked in acquire().
| n | Number of permits to release (default: 1). |
is_attached() must be true.
|
staticconstexpr |
Sentinel value for acquire() meaning "wait indefinitely".