1
0
mirror of https://github.com/danog/parallel.git synced 2025-01-22 05:51:14 +01:00

Keep track of PosixSemaphore size & handle cloning

This commit is contained in:
Stephen Coakley 2015-09-03 17:20:58 -05:00
parent 3e962d3a0e
commit 7e7639442f

View File

@ -21,6 +21,11 @@ class PosixSemaphore implements SemaphoreInterface, \Serializable
*/
private $key;
/**
* @var int The number of total locks.
*/
private $maxLocks;
/**
* @var resource A message queue of available locks.
*/
@ -36,7 +41,12 @@ class PosixSemaphore implements SemaphoreInterface, \Serializable
*/
public function __construct($maxLocks, $permissions = 0600)
{
if (!is_int($maxLocks) || $maxLocks < 0) {
throw new InvalidArgumentError('Max locks must be a non-negative integer.');
}
$this->key = abs(crc32(spl_object_hash($this)));
$this->maxLocks = $maxLocks;
$this->queue = msg_get_queue($this->key, $permissions);
if (!$this->queue) {
@ -59,6 +69,16 @@ class PosixSemaphore implements SemaphoreInterface, \Serializable
return !is_resource($this->queue) || !msg_queue_exists($this->key);
}
/**
* Gets the maximum number of locks held by the semaphore.
*
* @return int The maximum number of locks held by the semaphore.
*/
public function getSize()
{
return $this->maxLocks;
}
/**
* Gets the access permissions of the semaphore.
*
@ -148,7 +168,7 @@ class PosixSemaphore implements SemaphoreInterface, \Serializable
*/
public function serialize()
{
return serialize($this->key);
return serialize([$this->key, $this->maxLocks]);
}
/**
@ -159,13 +179,21 @@ class PosixSemaphore implements SemaphoreInterface, \Serializable
public function unserialize($serialized)
{
// Get the semaphore key and attempt to re-connect to the semaphore in memory.
$this->key = unserialize($serialized);
list($this->key, $this->maxLocks) = unserialize($serialized);
if (msg_queue_exists($this->key)) {
$this->queue = msg_get_queue($this->key);
}
}
/**
* Clones the semaphore, creating a new semaphore with the same size and permissions.
*/
public function __clone()
{
$this->__construct($this->maxLocks, $this->getPermissions());
}
/**
* Releases a lock from the semaphore.
*