Initial Commit
This commit is contained in:
31
dependencies/amphp/sync/src/SemaphoreMutex.php
vendored
Normal file
31
dependencies/amphp/sync/src/SemaphoreMutex.php
vendored
Normal file
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace WP_Ultimo\Dependencies\Amp\Sync;
|
||||
|
||||
use WP_Ultimo\Dependencies\Amp\Promise;
|
||||
use function WP_Ultimo\Dependencies\Amp\call;
|
||||
class SemaphoreMutex implements Mutex
|
||||
{
|
||||
/** @var Semaphore */
|
||||
private $semaphore;
|
||||
/**
|
||||
* @param Semaphore $semaphore A semaphore with a single lock.
|
||||
*/
|
||||
public function __construct(Semaphore $semaphore)
|
||||
{
|
||||
$this->semaphore = $semaphore;
|
||||
}
|
||||
/** {@inheritdoc} */
|
||||
public function acquire() : Promise
|
||||
{
|
||||
return call(function () : \Generator {
|
||||
/** @var \Amp\Sync\Lock $lock */
|
||||
$lock = (yield $this->semaphore->acquire());
|
||||
if ($lock->getId() !== 0) {
|
||||
$lock->release();
|
||||
throw new \Error("Cannot use a semaphore with more than a single lock");
|
||||
}
|
||||
return $lock;
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user