2016-08-21 23:40:48 -05:00
|
|
|
<?php declare(strict_types = 1);
|
2015-08-29 01:40:10 -05:00
|
|
|
|
2016-08-23 16:47:40 -05:00
|
|
|
namespace Amp\Parallel\Threading;
|
2016-08-18 11:04:48 -05:00
|
|
|
|
|
|
|
use Amp\Coroutine;
|
2016-08-23 16:47:40 -05:00
|
|
|
use Amp\Parallel\Sync\Parcel as SyncParcel;
|
2016-08-18 11:04:48 -05:00
|
|
|
use Interop\Async\Awaitable;
|
2015-08-19 15:56:00 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A thread-safe container that shares a value between multiple threads.
|
|
|
|
*/
|
2016-08-18 11:04:48 -05:00
|
|
|
class Parcel implements SyncParcel {
|
2015-09-02 17:23:22 -05:00
|
|
|
/**
|
2016-08-23 16:47:40 -05:00
|
|
|
* @var \Amp\Parallel\Threading\Mutex
|
2015-09-02 17:23:22 -05:00
|
|
|
*/
|
2015-08-19 15:56:00 -05:00
|
|
|
private $mutex;
|
2015-09-02 17:23:22 -05:00
|
|
|
|
|
|
|
/**
|
2016-08-23 16:47:40 -05:00
|
|
|
* @var \Amp\Parallel\Threading\Internal\Storage
|
2015-09-02 17:23:22 -05:00
|
|
|
*/
|
2015-08-29 01:40:10 -05:00
|
|
|
private $storage;
|
2015-08-19 15:56:00 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new shared object container.
|
|
|
|
*
|
|
|
|
* @param mixed $value The value to store in the container.
|
|
|
|
*/
|
2016-08-18 17:36:58 -05:00
|
|
|
public function __construct($value) {
|
2015-09-03 18:11:58 -05:00
|
|
|
$this->init($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $value
|
|
|
|
*/
|
2016-08-18 17:36:58 -05:00
|
|
|
private function init($value) {
|
2016-08-22 18:25:19 -05:00
|
|
|
$this->mutex = new Mutex;
|
2015-09-03 18:11:58 -05:00
|
|
|
$this->storage = new Internal\Storage($value);
|
2015-08-19 15:56:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-20 14:13:01 -05:00
|
|
|
* {@inheritdoc}
|
2015-08-19 15:56:00 -05:00
|
|
|
*/
|
2016-08-18 11:04:48 -05:00
|
|
|
public function unwrap() {
|
2015-08-29 01:40:10 -05:00
|
|
|
return $this->storage->get();
|
2015-08-19 15:56:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-20 14:13:01 -05:00
|
|
|
* {@inheritdoc}
|
2015-08-19 15:56:00 -05:00
|
|
|
*/
|
2016-08-18 11:04:48 -05:00
|
|
|
protected function wrap($value) {
|
2015-08-29 01:40:10 -05:00
|
|
|
$this->storage->set($value);
|
2015-08-19 15:56:00 -05:00
|
|
|
}
|
|
|
|
|
2016-08-18 11:04:48 -05:00
|
|
|
/**
|
|
|
|
* @return \Interop\Async\Awaitable
|
|
|
|
*/
|
|
|
|
public function synchronized(callable $callback): Awaitable {
|
|
|
|
return new Coroutine($this->doSynchronized($callback));
|
|
|
|
}
|
|
|
|
|
2015-08-19 15:56:00 -05:00
|
|
|
/**
|
2015-08-29 01:40:10 -05:00
|
|
|
* @coroutine
|
|
|
|
*
|
2015-09-03 18:11:58 -05:00
|
|
|
* Asynchronously invokes a callable while maintaining an exclusive lock on the container.
|
2015-08-19 15:56:00 -05:00
|
|
|
*
|
2015-09-03 18:11:58 -05:00
|
|
|
* @param callable<mixed> $callback The function to invoke. The value in the container will be passed as the first
|
|
|
|
* argument.
|
2015-08-19 15:56:00 -05:00
|
|
|
*
|
|
|
|
* @return \Generator
|
|
|
|
*/
|
2016-08-18 11:04:48 -05:00
|
|
|
private function doSynchronized(callable $callback): \Generator {
|
2016-08-23 16:47:40 -05:00
|
|
|
/** @var \Amp\Parallel\Sync\Lock $lock */
|
2016-08-18 11:04:48 -05:00
|
|
|
$lock = yield $this->mutex->acquire();
|
2015-08-19 15:56:00 -05:00
|
|
|
|
|
|
|
try {
|
2015-10-18 02:12:46 -05:00
|
|
|
$value = $this->unwrap();
|
2016-08-18 11:04:48 -05:00
|
|
|
$result = $callback($value);
|
|
|
|
|
|
|
|
if ($result instanceof \Generator) {
|
|
|
|
$result = new Coroutine($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($result instanceof Awaitable) {
|
|
|
|
yield $result;
|
|
|
|
}
|
|
|
|
|
2015-10-18 02:12:46 -05:00
|
|
|
$this->wrap(null === $result ? $value : $result);
|
2015-08-19 15:56:00 -05:00
|
|
|
} finally {
|
|
|
|
$lock->release();
|
|
|
|
}
|
2016-01-23 00:00:56 -06:00
|
|
|
|
|
|
|
return $result;
|
2015-08-19 15:56:00 -05:00
|
|
|
}
|
2015-08-20 14:13:01 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 11:04:48 -05:00
|
|
|
public function __clone() {
|
2015-09-03 18:11:58 -05:00
|
|
|
$this->init($this->unwrap());
|
2015-08-20 14:13:01 -05:00
|
|
|
}
|
2015-08-19 15:56:00 -05:00
|
|
|
}
|