1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-12 17:17:20 +01:00
parallel/lib/Threading/Internal/Semaphore.php

86 lines
2.2 KiB
PHP
Raw Normal View History

<?php
2016-08-18 18:04:48 +02:00
namespace Amp\Concurrent\Threading\Internal;
use Amp\Concurrent\Sync\Lock;
use Amp\Coroutine;
use Amp\Pause;
use Interop\Async\Awaitable;
/**
* An asynchronous semaphore based on pthreads' synchronization methods.
*
* @internal
*/
2016-08-18 18:04:48 +02:00
class Semaphore extends \Threaded {
const LATENCY_TIMEOUT = 10;
/**
* @var int The number of available locks.
*/
private $locks;
/**
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.
*/
2016-08-18 18:04:48 +02:00
public function __construct(int $locks) {
$this->locks = $locks;
}
/**
* Gets the number of currently available locks.
*
* @return int The number of available locks.
*/
2016-08-18 18:04:48 +02:00
public function count(): int {
return $this->locks;
}
2016-08-18 18:04:48 +02:00
/**
* @return \Interop\Async\Awaitable
*/
public function acquire(): Awaitable {
return new Coroutine($this->doAcquire());
}
/**
* Uses a double locking mechanism to acquire a lock without blocking. A
* synchronous mutex is used to make sure that the semaphore is queried one
* at a time to preserve the integrity of the semaphore itself. Then a lock
* count is used to check if a lock is available without blocking.
*
* If a lock is not available, we add the request to a queue and set a timer
* to check again in the future.
*/
2016-08-18 18:04:48 +02:00
private function doAcquire(): \Generator {
$tsl = function () {
// If there are no locks available or the wait queue is not empty,
// we need to wait our turn to acquire a lock.
if ($this->locks > 0) {
--$this->locks;
return false;
}
return true;
};
2015-09-02 03:58:22 +02:00
while ($this->locks < 1 || $this->synchronized($tsl)) {
2016-08-18 18:04:48 +02:00
yield new Pause(self::LATENCY_TIMEOUT);
}
2016-01-23 07:00:56 +01:00
return new Lock(function () {
$this->release();
});
}
/**
* Releases a lock from the semaphore.
*/
2016-08-18 18:04:48 +02:00
protected function release() {
$this->synchronized(function () {
++$this->locks;
});
}
}