This is automatic generated document for things in Swow\Semaphore class

Class Semaphore

A counting semaphore that works across threads.

Methods

Method __construct

public function __construct ( int $value )

Create a semaphore. This method does not yield.

Param int $value initial permit count, from 0 to UINT_MAX

Throws \ValueError when the value is out of range. Throws \Swow\SyncException when the semaphore is already constructed.

Method __serialize

public function __serialize ( ) : array

Serialize the semaphore by shared object id.

The serialized form does not copy the semaphore state.

Throws \Swow\SyncException when the semaphore is not constructed.

Method __unserialize

public function __unserialize ( array $data ) : void

Restore the semaphore from a shared object id.

Throws \Swow\SyncException when the data is invalid or the object is gone.

Method acquire

public function acquire ( int $timeout ) : bool

Take one permit.

In a coroutine, this method suspends the current coroutine when the count is zero.

context switching may happen here

Param int $timeout timeout in milliseconds, -1 waits forever

Returns bool false on timeout

Throws \Swow\SyncException when the caller cannot wait for the semaphore.

Method getValue

public function getValue ( ) : int

Return the current permit count.

Method getWaiterCount

public function getWaiterCount ( ) : int

Return the number of waiters.

Method release

public function release ( int $count ) : void

Add permits and wake waiters.

A count of 0 does nothing.

Param int $count number of permits to add

Throws \ValueError when the count is negative.

Method tryAcquire

public function tryAcquire ( ) : bool

Try to take one permit without waiting.

Returns bool true when a permit was taken


Copyright 2022 the Swow contributors