diff --git a/src/Sync/Parcel.php b/src/Sync/Parcel.php index bb04238..dce768d 100644 --- a/src/Sync/Parcel.php +++ b/src/Sync/Parcel.php @@ -66,6 +66,16 @@ class Parcel implements ParcelInterface, \Serializable * If not specified defaults to 0600. */ public function __construct($value, $size = 16384, $permissions = 0600) + { + $this->init($value, $size, $permissions); + } + + /** + * @param mixed $value + * @param int $size + * @param int $permissions + */ + private function init($value, $size = 16384, $permissions = 0600) { $this->key = abs(crc32(spl_object_hash($this))); $this->memOpen($this->key, 'n', $permissions, $size + self::MEM_DATA_OFFSET); @@ -164,6 +174,7 @@ class Parcel implements ParcelInterface, \Serializable */ public function synchronized(callable $callback) { + /** @var \Icicle\Concurrent\Sync\Lock $lock */ $lock = (yield $this->semaphore->acquire()); try { @@ -227,7 +238,8 @@ class Parcel implements ParcelInterface, \Serializable public function __clone() { $value = $this->unwrap(); - $this->__construct($value); + $header = $this->getHeader(); + $this->init($value, $header['size'], $header['permissions']); } /** diff --git a/src/Threading/Internal/Storage.php b/src/Threading/Internal/Storage.php index a994407..06aa1de 100644 --- a/src/Threading/Internal/Storage.php +++ b/src/Threading/Internal/Storage.php @@ -11,6 +11,14 @@ class Storage extends \Threaded */ private $value; + /** + * @param mixed $value + */ + public function __construct($value) + { + $this->value = $value; + } + /** * @return mixed */ diff --git a/src/Threading/Parcel.php b/src/Threading/Parcel.php index cc02923..b6fb76a 100644 --- a/src/Threading/Parcel.php +++ b/src/Threading/Parcel.php @@ -24,10 +24,17 @@ class Parcel implements ParcelInterface * @param mixed $value The value to store in the container. */ public function __construct($value) + { + $this->init($value); + } + + /** + * @param mixed $value + */ + private function init($value) { $this->mutex = new Mutex(); - $this->storage = new Internal\Storage(); - $this->storage->set($value); + $this->storage = new Internal\Storage($value); } /** @@ -49,28 +56,23 @@ class Parcel implements ParcelInterface /** * @coroutine * - * Asynchronously invokes a callable while maintaining an exclusive lock on - * the container. + * 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. + * @param callable $callback The function to invoke. The value in the container will be passed as the first + * argument. * * @return \Generator */ - public function synchronized(callable $function) + public function synchronized(callable $callback) { /** @var \Icicle\Concurrent\Sync\Lock $lock */ $lock = (yield $this->mutex->acquire()); try { - $value = (yield $function($this->storage->get())); - $this->storage->set($value); + yield $callback($this); } finally { $lock->release(); } - - yield $value; } /** @@ -78,7 +80,6 @@ class Parcel implements ParcelInterface */ public function __clone() { - $this->storage = clone $this->storage; - $this->mutex = clone $this->mutex; + $this->init($this->unwrap()); } }