mutex = new Mutex(); $this->storage = new Internal\Storage(); $this->storage->set($value); } /** * {@inheritdoc} */ public function unwrap() { return $this->storage->get(); } /** * {@inheritdoc} */ public function wrap($value) { $this->storage->set($value); } /** * @coroutine * * Asynchronously invokes a callable while maintaining an exclusive lock on * the container. * * @param callable $function The function to invoke. The value in the * container will be passed as the first * argument. * * @return \Generator */ public function synchronized(callable $function) { /** @var \Icicle\Concurrent\Sync\Lock $lock */ $lock = (yield $this->mutex->acquire()); try { $value = (yield $function($this->storage->get())); $this->storage->set($value); } finally { $lock->release(); } yield $value; } /** * {@inheritdoc} */ public function __clone() { $this->storage = clone $this->storage; $this->mutex = clone $this->mutex; } }