2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2015-08-10 05:30:11 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Threading\Internal;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
2016-08-23 01:25:19 +02:00
|
|
|
use Amp\{ Coroutine, Pause };
|
2016-08-23 23:47:40 +02:00
|
|
|
use Amp\Parallel\Sync\Lock;
|
2017-01-09 18:11:25 +01:00
|
|
|
use AsyncInterop\Promise;
|
2015-08-10 05:30:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2015-08-10 05:30:11 +02:00
|
|
|
|
2016-08-26 17:10:03 +02:00
|
|
|
/** @var int The number of available locks. */
|
2015-08-10 05:30:11 +02:00
|
|
|
private $locks;
|
|
|
|
|
|
|
|
/**
|
2015-08-31 00:52:00 +02:00
|
|
|
* Creates a new semaphore with a given number of locks.
|
2015-08-10 05:30:11 +02:00
|
|
|
*
|
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-10 05:30:11 +02:00
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function __construct(int $locks) {
|
2015-09-04 01:31:29 +02:00
|
|
|
$this->locks = $locks;
|
2015-08-10 05:30:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
2015-08-10 05:30:11 +02:00
|
|
|
return $this->locks;
|
|
|
|
}
|
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
/**
|
2017-01-09 18:11:25 +01:00
|
|
|
* @return \AsyncInterop\Promise
|
2016-08-18 18:04:48 +02:00
|
|
|
*/
|
2016-11-15 00:43:44 +01:00
|
|
|
public function acquire(): Promise {
|
2016-08-18 18:04:48 +02:00
|
|
|
return new Coroutine($this->doAcquire());
|
|
|
|
}
|
|
|
|
|
2015-08-10 05:30:11 +02:00
|
|
|
/**
|
|
|
|
* 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 {
|
2015-09-01 23:19:59 +02:00
|
|
|
$tsl = function () {
|
2015-08-10 05:30:11 +02:00
|
|
|
// If there are no locks available or the wait queue is not empty,
|
|
|
|
// we need to wait our turn to acquire a lock.
|
2015-09-01 23:19:59 +02:00
|
|
|
if ($this->locks > 0) {
|
2015-08-10 05:30:11 +02:00
|
|
|
--$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);
|
2015-08-10 05:30:11 +02:00
|
|
|
}
|
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
return new Lock(function () {
|
2015-08-10 05:30:11 +02:00
|
|
|
$this->release();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Releases a lock from the semaphore.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
protected function release() {
|
2015-08-10 05:30:11 +02:00
|
|
|
$this->synchronized(function () {
|
|
|
|
++$this->locks;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|