2016-08-22 06:40:48 +02:00
|
|
|
<?php declare(strict_types = 1);
|
2015-08-29 08:40:10 +02:00
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
namespace Amp\Concurrent\Threading;
|
|
|
|
|
|
|
|
use Amp\Concurrent\Sync\Semaphore as SyncSemaphore;
|
2016-08-19 00:36:58 +02:00
|
|
|
use Interop\Async\Awaitable;
|
2015-08-07 22:32:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An asynchronous semaphore based on pthreads' synchronization methods.
|
|
|
|
*
|
|
|
|
* This is an implementation of a thread-safe semaphore that has non-blocking
|
|
|
|
* acquire methods. There is a small tradeoff for asynchronous semaphores; you
|
|
|
|
* may not acquire a lock immediately when one is available and there may be a
|
|
|
|
* small delay. However, the small delay will not block the thread.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
class Semaphore implements SyncSemaphore {
|
2015-08-07 22:32:18 +02:00
|
|
|
/**
|
2015-08-31 00:52:00 +02:00
|
|
|
* @var Internal\Semaphore
|
2015-08-07 22:32:18 +02:00
|
|
|
*/
|
2015-08-10 05:30:11 +02:00
|
|
|
private $semaphore;
|
2015-08-07 22:32:18 +02:00
|
|
|
|
2015-09-04 01:31:29 +02:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private $maxLocks;
|
|
|
|
|
2015-08-07 22:32:18 +02:00
|
|
|
/**
|
2015-08-31 00:52:00 +02:00
|
|
|
* Creates a new semaphore with a given number of locks.
|
|
|
|
*
|
2015-08-31 20:49:26 +02:00
|
|
|
* @param int $locks The maximum number of locks that can be acquired from the semaphore.
|
2015-08-07 22:32:18 +02:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function __construct(int $locks) {
|
2015-09-04 01:31:29 +02:00
|
|
|
$this->init($locks);
|
2015-08-07 22:32:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-09-04 01:31:29 +02:00
|
|
|
* Initializes the semaphore with a given number of locks.
|
2015-08-07 22:32:18 +02:00
|
|
|
*
|
2015-09-04 01:31:29 +02:00
|
|
|
* @param int $locks
|
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
private function init(int $locks) {
|
2015-09-04 01:31:29 +02:00
|
|
|
if ($locks < 1) {
|
2016-08-23 01:25:19 +02:00
|
|
|
throw new \Error("The number of locks should be a positive integer");
|
2015-09-04 01:31:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->semaphore = new Internal\Semaphore($locks);
|
|
|
|
$this->maxLocks = $locks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2015-08-07 22:32:18 +02:00
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function count(): int {
|
2015-08-10 05:30:11 +02:00
|
|
|
return $this->semaphore->count();
|
2015-08-07 22:32:18 +02:00
|
|
|
}
|
|
|
|
|
2015-09-04 01:31:29 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function getSize(): int {
|
2015-09-04 01:31:29 +02:00
|
|
|
return $this->maxLocks;
|
|
|
|
}
|
|
|
|
|
2015-08-07 22:32:18 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function acquire(): Awaitable {
|
2015-08-10 05:30:11 +02:00
|
|
|
return $this->semaphore->acquire();
|
2015-08-07 22:32:18 +02:00
|
|
|
}
|
2015-09-04 01:31:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clones the semaphore, creating a new instance with the same number of locks, all available.
|
|
|
|
*/
|
2016-08-19 00:36:58 +02:00
|
|
|
public function __clone() {
|
2015-09-04 01:31:29 +02:00
|
|
|
$this->init($this->getSize());
|
|
|
|
}
|
2015-08-31 00:52:00 +02:00
|
|
|
}
|