Why: Semaphore#

class Semaphore#

Public Functions

void give()#

Increment the semaphore count (signal/post operation).

This operation is safe to call from interrupt service routines (ISRs).

void take()#

Decrement the semaphore count, blocking if necessary.

If the count is greater than zero, decrements it and returns immediately. Otherwise, blocks until another thread calls give().

std::expected<void, OSError> take_noblock()#

Attempt to decrement the semaphore count without blocking.

This is the non-blocking version of take(). Safe to call from ISRs.

Returns:

std::expected<void, OSError> Success if the count was decremented, or an OSError with errno EAGAIN if the count was zero.

inline int _pollfd() const#

Get the file descriptor for polling/event loop integration.

Returns:

int The underlying eventfd file descriptor.

Public Static Functions

static std::expected<Semaphore, OSError> create(unsigned int initial)#

Create a semaphore with an initial count.

Returns:

std::expected<Semaphore, OSError> A Semaphore object on success, or an OSError on failure.