class SyncSemaphore (View source)

SyncSemaphore

A cross-platform, native implementation of named and unnamed semaphore objects. A semaphore restricts access to a limited resource to a limited number of instances. Semaphores differ from mutexes in that they can allow more than one instance to access a resource at one time while a mutex only allows one instance at a time.

Methods

__construct(string $name, int $initialval = 1, bool $autounlock = true)

Constructs a new SyncSemaphore object

bool
lock(int $wait = -1)

Decreases the count of the semaphore or waits

bool
unlock(int $prevcount = 0)

Increases the count of the semaphore

Details

__construct(string $name, int $initialval = 1, bool $autounlock = true)

Constructs a new SyncSemaphore object

Constructs a named or unnamed semaphore.

Parameters

string $name

[optional] The name of the semaphore if this is a named semaphore object. Note: If the name already exists, it must be able to be opened by the current user that the process is running as or an exception will be thrown with a meaningless error message.

int $initialval

[optional] The initial value of the semaphore. This is the number of locks that may be obtained.

bool $autounlock

[optional] Specifies whether or not to automatically unlock the semaphore at the conclusion of the PHP script. Warning: If an object is: A named semaphore with an autounlock of FALSE, the object is locked, and the PHP script concludes before the object is unlocked, then the underlying semaphore will end up in an inconsistent state.

Exceptions

Exception

bool lock(int $wait = -1)

Decreases the count of the semaphore or waits

Decreases the count of a SyncSemaphore object or waits until the semaphore becomes non-zero.

Parameters

int $wait

The number of milliseconds to wait for the semaphore. A value of -1 is infinite.

Return Value

bool

TRUE if the lock operation was successful, FALSE otherwise.

See also

SyncSemaphore::unlock

bool unlock(int $prevcount = 0)

Increases the count of the semaphore

Increases the count of a SyncSemaphore object.

Parameters

int $prevcount

Returns the previous count of the semaphore.

Return Value

bool

TRUE if the unlock operation was successful, FALSE otherwise.

See also

SyncSemaphore::lock