CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
cmlabs::utils::Semaphore Class Reference

Counting semaphore, optionally named for cross-process use. More...

#include <Utils.h>

Public Member Functions

 Semaphore (uint32 maxCount=100)
 Create an anonymous semaphore.
 Semaphore (const char *name, uint32 maxCount=100)
 Create or open a named, cross-process semaphore.
 ~Semaphore ()
bool wait ()
 Block until the semaphore can be decremented.
bool wait (uint32 timeout)
 Wait with a timeout.
bool signal ()
 Increment (release) the semaphore, waking one waiter.

Public Attributes

char * name

Protected Attributes

sem_t * semaphore

Detailed Description

Counting semaphore, optionally named for cross-process use.

On macOS an unnamed semaphore uses libdispatch (dispatch_semaphore_t) while named ones are emulated in shared memory, because POSIX unnamed semaphores are unsupported there — hence the void* semaphore_impl member.

Definition at line 536 of file Utils.h.

Constructor & Destructor Documentation

◆ Semaphore() [1/2]

cmlabs::utils::Semaphore::Semaphore ( uint32 maxCount = 100)

Create an anonymous semaphore.

Parameters
maxCountMaximum count (Windows honours it strictly).

Definition at line 1876 of file Utils.cpp.

References name, and semaphore.

◆ Semaphore() [2/2]

cmlabs::utils::Semaphore::Semaphore ( const char * name,
uint32 maxCount = 100 )

Create or open a named, cross-process semaphore.

Parameters
nameGlobal name.
maxCountMaximum count.

Definition at line 1897 of file Utils.cpp.

References MAXKEYNAMELEN, MAXSHMEMNAMELEN, name, semaphore, cmlabs::utils::SetSemaphoreOwnsName(), cmlabs::utils::strcpyavail(), and WINSHMEMSPACE.

◆ ~Semaphore()

cmlabs::utils::Semaphore::~Semaphore ( )

Definition at line 1930 of file Utils.cpp.

References MAXSHMEMNAMELEN, name, semaphore, and cmlabs::utils::TakeSemaphoreOwnsName().

Member Function Documentation

◆ signal()

bool cmlabs::utils::Semaphore::signal ( )

Increment (release) the semaphore, waking one waiter.

Returns
true on success.

Definition at line 2006 of file Utils.cpp.

References name, and semaphore.

Referenced by cmlabs::utils::SignalSemaphore(), cmlabs::utils::UnitTest_SemaphoreUserDeath(), and cmlabs::utils::UnitTest_SharedNames().

◆ wait() [1/2]

bool cmlabs::utils::Semaphore::wait ( )

Block until the semaphore can be decremented.

Returns
true on success.

Definition at line 1969 of file Utils.cpp.

References name, and semaphore.

Referenced by cmlabs::utils::UnitTest_SemaphoreUserDeath(), cmlabs::utils::UnitTest_SharedNames(), and cmlabs::utils::WaitForSemaphore().

◆ wait() [2/2]

bool cmlabs::utils::Semaphore::wait ( uint32 timeout)

Wait with a timeout.

Parameters
timeoutMax wait in milliseconds.
Returns
true when acquired, false on timeout.

Definition at line 1986 of file Utils.cpp.

References cmlabs::utils::CalcTimeout(), name, and semaphore.

Member Data Documentation

◆ name

◆ semaphore

sem_t* cmlabs::utils::Semaphore::semaphore
protected

Definition at line 558 of file Utils.h.

Referenced by Semaphore(), Semaphore(), signal(), wait(), wait(), and ~Semaphore().


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