|
VLink
2.0.0
A high-performance communication middleware
|
Counting semaphore that can be shared across processes through an OS name. 更多...
#include <sys_semaphore.h>
Public 成员函数 | |
| SysSemaphore (size_t count=0) | |
Constructs the wrapper with the initial count used when attach() creates the kernel object. 更多... | |
| ~SysSemaphore () | |
Destructor. Invokes detach(false) when the wrapper is still attached. 更多... | |
| bool | attach (const std::string &name) |
Creates or opens the kernel object identified by name. 更多... | |
| bool | detach (bool force=true) |
| Releases the kernel handle and optionally unlinks the named object. 更多... | |
| bool | acquire (size_t n=1, int timeout_ms=kInfinite) |
Decrements the kernel counter by n, blocking when permits are unavailable. 更多... | |
| void | release (size_t n=1) |
Increments the kernel counter by n, waking blocked acquirers. 更多... | |
| bool | is_attached () const |
| Reports whether the wrapper currently owns a kernel handle. 更多... | |
| size_t | get_count () const |
| Returns a non-atomic snapshot of the current counter value. 更多... | |
静态 Public 属性 | |
| static constexpr int | kInfinite {-1} |
Sentinel value for acquire() meaning wait indefinitely. 更多... | |
Counting semaphore that can be shared across processes through an OS name.
Resolves to a POSIX named semaphore, a Win32 named semaphore or a macOS dispatch semaphore depending on the host platform.
|
explicit |
Constructs the wrapper with the initial count used when attach() creates the kernel object.
| count | Initial permit count for first creation. Default: 0. |
| vlink::SysSemaphore::~SysSemaphore | ( | ) |
Destructor. Invokes detach(false) when the wrapper is still attached.
| bool vlink::SysSemaphore::acquire | ( | size_t | n = 1, |
| int | timeout_ms = kInfinite |
||
| ) |
Decrements the kernel counter by n, blocking when permits are unavailable.
| n | Number of permits requested. Default: 1. |
| timeout_ms | Maximum wait in milliseconds; kInfinite waits forever. |
true when the permits were acquired; false on timeout or error.is_attached() must be true. | bool vlink::SysSemaphore::attach | ( | const std::string & | name | ) |
Creates or opens the kernel object identified by name.
When the kernel object already exists the constructor-supplied initial count is ignored. POSIX backends require name to begin with '/'. macOS ignores name and produces a fresh in-process dispatch semaphore.
| name | Platform-specific semaphore name. |
true on success, false otherwise. | bool vlink::SysSemaphore::detach | ( | bool | force = true | ) |
Releases the kernel handle and optionally unlinks the named object.
| force | When true, POSIX backends unlink the name so it disappears from the kernel namespace. Ignored on Windows and macOS. Default: true. |
true on success. | size_t vlink::SysSemaphore::get_count | ( | ) | const |
Returns a non-atomic snapshot of the current counter value.
Some backends (notably macOS dispatch) do not expose the counter; 0 is returned in that case.
| bool vlink::SysSemaphore::is_attached | ( | ) | const |
| void vlink::SysSemaphore::release | ( | size_t | n = 1 | ) |
Increments the kernel counter by n, waking blocked acquirers.
| n | Number of permits to add. Default: 1. |
is_attached() must be true.
|
staticconstexpr |
Sentinel value for acquire() meaning wait indefinitely.