1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-27 04:44:56 +01:00

Update parcels

This commit is contained in:
Aaron Piotrowski 2015-09-03 18:11:58 -05:00
parent 3c93d2c383
commit d47e56a840
3 changed files with 36 additions and 15 deletions

View File

@ -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']);
}
/**

View File

@ -11,6 +11,14 @@ class Storage extends \Threaded
*/
private $value;
/**
* @param mixed $value
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* @return mixed
*/

View File

@ -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<mixed> $function The function to invoke. The value in the
* container will be passed as the first
* argument.
* @param callable<mixed> $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());
}
}