2017-11-29 21:40:07 +01:00
|
|
|
<?php
|
|
|
|
|
2017-11-29 22:01:32 +01:00
|
|
|
namespace Amp\Parallel\Sync;
|
2017-11-29 21:40:07 +01:00
|
|
|
|
|
|
|
use Amp\Promise;
|
|
|
|
use Amp\Success;
|
|
|
|
use Amp\Sync\ThreadedMutex;
|
|
|
|
use function Amp\call;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A thread-safe container that shares a value between multiple threads.
|
|
|
|
*/
|
2018-10-21 17:54:46 +02:00
|
|
|
final class ThreadedParcel implements Parcel
|
2018-10-07 16:50:45 +02:00
|
|
|
{
|
2017-11-29 21:40:07 +01:00
|
|
|
/** @var \Amp\Sync\ThreadedMutex */
|
|
|
|
private $mutex;
|
|
|
|
|
|
|
|
/** @var \Threaded */
|
|
|
|
private $storage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new shared object container.
|
|
|
|
*
|
|
|
|
* @param mixed $value The value to store in the container.
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function __construct($value)
|
|
|
|
{
|
2017-11-29 21:40:07 +01:00
|
|
|
$this->mutex = new ThreadedMutex;
|
2017-11-29 22:05:15 +01:00
|
|
|
$this->storage = new Internal\ParcelStorage($value);
|
2017-11-29 21:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function unwrap(): Promise
|
|
|
|
{
|
2017-11-29 21:40:07 +01:00
|
|
|
return new Success($this->storage->get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Amp\Promise
|
|
|
|
*/
|
2018-10-07 16:50:45 +02:00
|
|
|
public function synchronized(callable $callback): Promise
|
|
|
|
{
|
2017-11-29 21:40:07 +01:00
|
|
|
return call(function () use ($callback) {
|
|
|
|
/** @var \Amp\Sync\Lock $lock */
|
|
|
|
$lock = yield $this->mutex->acquire();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$result = yield call($callback, $this->storage->get());
|
|
|
|
|
|
|
|
if ($result !== null) {
|
|
|
|
$this->storage->set($result);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
$lock->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|