VLink  2.0.0
A high-performance communication middleware
vlink::SysSemaphore类 参考final

Counting semaphore that can be shared across processes through an OS name. 更多...

#include <sys_semaphore.h>

vlink::SysSemaphore 的协作图:

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.

构造及析构函数说明

◆ SysSemaphore()

vlink::SysSemaphore::SysSemaphore ( size_t  count = 0)
explicit

Constructs the wrapper with the initial count used when attach() creates the kernel object.

参数
countInitial permit count for first creation. Default: 0.

◆ ~SysSemaphore()

vlink::SysSemaphore::~SysSemaphore ( )

Destructor. Invokes detach(false) when the wrapper is still attached.

成员函数说明

◆ acquire()

bool vlink::SysSemaphore::acquire ( size_t  n = 1,
int  timeout_ms = kInfinite 
)

Decrements the kernel counter by n, blocking when permits are unavailable.

参数
nNumber of permits requested. Default: 1.
timeout_msMaximum wait in milliseconds; kInfinite waits forever.
返回
true when the permits were acquired; false on timeout or error.
前置条件
is_attached() must be true.

◆ attach()

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.

参数
namePlatform-specific semaphore name.
返回
true on success, false otherwise.

◆ detach()

bool vlink::SysSemaphore::detach ( bool  force = true)

Releases the kernel handle and optionally unlinks the named object.

参数
forceWhen true, POSIX backends unlink the name so it disappears from the kernel namespace. Ignored on Windows and macOS. Default: true.
返回
true on success.

◆ get_count()

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.

返回
Current permit count snapshot.

◆ is_attached()

bool vlink::SysSemaphore::is_attached ( ) const

Reports whether the wrapper currently owns a kernel handle.

返回
true when attach() succeeded and detach() has not been called yet.

◆ release()

void vlink::SysSemaphore::release ( size_t  n = 1)

Increments the kernel counter by n, waking blocked acquirers.

参数
nNumber of permits to add. Default: 1.
前置条件
is_attached() must be true.

类成员变量说明

◆ kInfinite

constexpr int vlink::SysSemaphore::kInfinite {-1}
staticconstexpr

Sentinel value for acquire() meaning wait indefinitely.


该类的文档由以下文件生成: