mirror of
https://github.com/danog/parallel.git
synced 2025-01-22 22:11:11 +01:00
Rename Synchronizable to Synchronized and move locking to Synchronized
The ability to call synchronized functions now belongs to any Synchronized object.
This commit is contained in:
parent
50c3620a6f
commit
1698aeefcf
@ -3,7 +3,6 @@ namespace Icicle\Concurrent\Forking;
|
||||
|
||||
use Icicle\Concurrent\Context;
|
||||
use Icicle\Concurrent\ContextAbortException;
|
||||
use Icicle\Concurrent\Semaphore;
|
||||
use Icicle\Loop;
|
||||
use Icicle\Promise\Deferred;
|
||||
use Icicle\Socket\Stream\DuplexStream;
|
||||
@ -21,7 +20,6 @@ abstract class ForkContext extends Synchronizable implements Context
|
||||
private $pid = 0;
|
||||
private $isChild = false;
|
||||
private $deferred;
|
||||
private $semaphore;
|
||||
|
||||
/**
|
||||
* Creates a new fork context.
|
||||
@ -33,7 +31,6 @@ abstract class ForkContext extends Synchronizable implements Context
|
||||
$this->deferred = new Deferred(function (\Exception $exception) {
|
||||
$this->stop();
|
||||
});
|
||||
$this->semaphore = new Semaphore();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -162,32 +159,6 @@ abstract class ForkContext extends Synchronizable implements Context
|
||||
return $this->deferred->getPromise();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function lock()
|
||||
{
|
||||
$this->semaphore->lock();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function unlock()
|
||||
{
|
||||
$this->semaphore->unlock();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function synchronized(callable $callback)
|
||||
{
|
||||
$this->lock();
|
||||
$callback($this);
|
||||
$this->unlock();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -1,13 +1,24 @@
|
||||
<?php
|
||||
namespace Icicle\Concurrent\Forking;
|
||||
|
||||
abstract class Synchronizable
|
||||
use Icicle\Concurrent\Semaphore;
|
||||
|
||||
/**
|
||||
* A synchronized object that safely shares its state across processes and
|
||||
* provides methods for process synchronization.
|
||||
*/
|
||||
abstract class Synchronized
|
||||
{
|
||||
private $memoryBlock;
|
||||
private $memoryKey;
|
||||
private $semaphore;
|
||||
|
||||
/**
|
||||
* Creates a new synchronized object.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->semaphore = new Semaphore();
|
||||
$this->memoryKey = abs(crc32(spl_object_hash($this)));
|
||||
$this->memoryBlock = shm_attach($this->memoryKey, 8192);
|
||||
if (!is_resource($this->memoryBlock)) {
|
||||
@ -15,12 +26,49 @@ abstract class Synchronizable
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Locks the object for read or write for the calling context.
|
||||
*/
|
||||
public function lock()
|
||||
{
|
||||
$this->semaphore->lock();
|
||||
}
|
||||
|
||||
/**
|
||||
* Unlocks the object.
|
||||
*/
|
||||
public function unlock()
|
||||
{
|
||||
$this->semaphore->unlock();
|
||||
}
|
||||
|
||||
/**
|
||||
* Invokes a function while maintaining a lock for the calling context.
|
||||
*
|
||||
* @param callable $callback The function to invoke.
|
||||
*
|
||||
* @return mixed The value returned by the callback.
|
||||
*/
|
||||
public function synchronized(callable $callback)
|
||||
{
|
||||
$this->lock();
|
||||
$returnValue = $callback($this);
|
||||
$this->unlock();
|
||||
return $returnValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function __isset($name)
|
||||
{
|
||||
$key = abs(crc32($name));
|
||||
return shm_has_var($this->memoryBlock, $key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function __get($name)
|
||||
{
|
||||
$key = abs(crc32($name));
|
||||
@ -30,6 +78,9 @@ abstract class Synchronizable
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function __set($name, $value)
|
||||
{
|
||||
$key = abs(crc32($name));
|
||||
@ -38,6 +89,9 @@ abstract class Synchronizable
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
public function __unset($name)
|
||||
{
|
||||
$key = abs(crc32($name));
|
Loading…
x
Reference in New Issue
Block a user