2016-08-22 06:40:48 +02:00
|
|
|
<?php declare(strict_types = 1);
|
2016-08-18 18:04:48 +02:00
|
|
|
|
|
|
|
namespace Amp\Concurrent\Sync;
|
|
|
|
|
|
|
|
use Interop\Async\Awaitable;
|
2015-08-02 04:51:45 +02:00
|
|
|
|
|
|
|
/**
|
2015-08-31 11:19:49 +02:00
|
|
|
* A non-blocking counting semaphore.
|
2015-08-02 04:51:45 +02:00
|
|
|
*
|
|
|
|
* Objects that implement this interface should guarantee that all operations
|
2015-08-31 19:26:11 +02:00
|
|
|
* are atomic. Implementations do not have to guarantee that acquiring a lock
|
|
|
|
* is first-come, first serve.
|
2015-08-02 04:51:45 +02:00
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
interface Semaphore extends \Countable {
|
2015-08-31 11:19:49 +02:00
|
|
|
/**
|
|
|
|
* Gets the number of currently available locks.
|
|
|
|
*
|
|
|
|
* @return int The number of available locks.
|
|
|
|
*/
|
2016-01-25 05:59:14 +01:00
|
|
|
public function count(): int;
|
2015-08-31 11:19:49 +02:00
|
|
|
|
2015-09-04 01:31:29 +02:00
|
|
|
/**
|
|
|
|
* Gets the total number of locks on the semaphore (not the number of available locks).
|
|
|
|
*
|
|
|
|
* @return int The total number of locks.
|
|
|
|
*/
|
2016-01-25 05:59:14 +01:00
|
|
|
public function getSize(): int;
|
2015-09-04 01:31:29 +02:00
|
|
|
|
2015-08-02 04:51:45 +02:00
|
|
|
/**
|
2015-08-10 05:30:11 +02:00
|
|
|
* @coroutine
|
|
|
|
*
|
2015-08-07 22:32:18 +02:00
|
|
|
* Acquires a lock from the semaphore asynchronously.
|
2015-08-02 04:51:45 +02:00
|
|
|
*
|
2015-10-17 06:32:01 +02:00
|
|
|
* If there are one or more locks available, this function resolves immediately with a lock and the lock count is
|
2015-08-31 11:19:49 +02:00
|
|
|
* decreased. If no locks are available, the semaphore waits asynchronously for a lock to become available.
|
|
|
|
*
|
2016-08-18 18:04:48 +02:00
|
|
|
* @return \Interop\Async\Awaitable<\Amp\Concurrent\Sync\Lock> Resolves with a lock object when the acquire is
|
|
|
|
* successful.
|
2015-08-02 04:51:45 +02:00
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function acquire(): Awaitable;
|
2015-08-02 04:51:45 +02:00
|
|
|
}
|