2016-12-30 02:16:04 +01:00
|
|
|
<?php
|
2015-08-11 05:27:10 +02:00
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
namespace Amp\Parallel\Sync;
|
2016-08-18 18:04:48 +02:00
|
|
|
|
|
|
|
use Amp\{ Coroutine, Pause };
|
2016-08-23 23:47:40 +02:00
|
|
|
use Amp\Parallel\SemaphoreException;
|
2017-03-16 23:03:59 +01:00
|
|
|
use Amp\Promise;
|
2015-08-11 05:27:10 +02:00
|
|
|
|
|
|
|
/**
|
2015-08-31 11:18:14 +02:00
|
|
|
* A non-blocking, interprocess POSIX semaphore.
|
2015-08-11 05:27:10 +02:00
|
|
|
*
|
2015-08-31 11:18:14 +02:00
|
|
|
* Uses a POSIX message queue to store a queue of permits in a lock-free data structure. This semaphore implementation
|
|
|
|
* is preferred over other implementations when available, as it provides the best performance.
|
2015-08-11 05:27:10 +02:00
|
|
|
*
|
2015-08-31 11:18:14 +02:00
|
|
|
* Not compatible with Windows.
|
2015-08-11 05:27:10 +02:00
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
class PosixSemaphore implements Semaphore, \Serializable {
|
2016-08-22 06:40:48 +02:00
|
|
|
const LATENCY_TIMEOUT = 10;
|
2015-08-11 05:27:10 +02:00
|
|
|
|
2016-08-26 17:10:03 +02:00
|
|
|
/** @var int The semaphore key. */
|
2015-08-11 05:27:10 +02:00
|
|
|
private $key;
|
|
|
|
|
2016-08-26 17:10:03 +02:00
|
|
|
/** @var int The number of total locks. */
|
2015-09-04 00:20:58 +02:00
|
|
|
private $maxLocks;
|
|
|
|
|
2016-08-26 17:10:03 +02:00
|
|
|
/** @var resource A message queue of available locks. */
|
2015-08-31 11:18:14 +02:00
|
|
|
private $queue;
|
2015-08-11 05:27:10 +02:00
|
|
|
|
|
|
|
/**
|
2015-08-31 00:52:00 +02:00
|
|
|
* Creates a new semaphore with a given number of locks.
|
2015-08-11 05:27:10 +02:00
|
|
|
*
|
2015-08-31 00:52:00 +02:00
|
|
|
* @param int $maxLocks The maximum number of locks that can be acquired from the semaphore.
|
|
|
|
* @param int $permissions Permissions to access the semaphore.
|
2015-08-31 11:18:14 +02:00
|
|
|
*
|
|
|
|
* @throws SemaphoreException If the semaphore could not be created due to an internal error.
|
2015-08-11 05:27:10 +02:00
|
|
|
*/
|
2016-09-02 06:51:48 +02:00
|
|
|
public function __construct(int $maxLocks, int $permissions = 0600) {
|
2016-08-18 18:04:48 +02:00
|
|
|
if (!\extension_loaded("sysvmsg")) {
|
|
|
|
throw new \Error(__CLASS__ . " requires the sysvmsg extension.");
|
2015-11-11 08:07:59 +01:00
|
|
|
}
|
|
|
|
|
2015-09-04 01:31:29 +02:00
|
|
|
$this->init($maxLocks, $permissions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $maxLocks The maximum number of locks that can be acquired from the semaphore.
|
|
|
|
* @param int $permissions Permissions to access the semaphore.
|
|
|
|
*
|
|
|
|
* @throws SemaphoreException If the semaphore could not be created due to an internal error.
|
|
|
|
*/
|
2016-09-02 06:51:48 +02:00
|
|
|
private function init(int $maxLocks, int $permissions) {
|
2015-09-04 01:31:29 +02:00
|
|
|
if ($maxLocks < 1) {
|
|
|
|
$maxLocks = 1;
|
2015-09-04 00:20:58 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
$this->key = \abs(\crc32(\spl_object_hash($this)));
|
2015-09-04 00:20:58 +02:00
|
|
|
$this->maxLocks = $maxLocks;
|
2015-08-11 05:27:10 +02:00
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
$this->queue = \msg_get_queue($this->key, $permissions);
|
2015-08-31 11:18:14 +02:00
|
|
|
if (!$this->queue) {
|
2015-08-11 05:27:10 +02:00
|
|
|
throw new SemaphoreException('Failed to create the semaphore.');
|
|
|
|
}
|
|
|
|
|
2015-08-31 11:18:14 +02:00
|
|
|
// Fill the semaphore with locks.
|
|
|
|
while (--$maxLocks >= 0) {
|
|
|
|
$this->release();
|
|
|
|
}
|
2015-08-11 05:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-31 11:18:14 +02:00
|
|
|
* Checks if the semaphore has been freed.
|
2015-08-11 05:27:10 +02:00
|
|
|
*
|
2015-08-31 11:18:14 +02:00
|
|
|
* @return bool True if the semaphore has been freed, otherwise false.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function isFreed(): bool {
|
|
|
|
return !\is_resource($this->queue) || !\msg_queue_exists($this->key);
|
2015-08-31 11:18:14 +02:00
|
|
|
}
|
|
|
|
|
2015-09-04 00:20:58 +02:00
|
|
|
/**
|
|
|
|
* Gets the maximum number of locks held by the semaphore.
|
|
|
|
*
|
|
|
|
* @return int The maximum number of locks held by the semaphore.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function getSize(): int {
|
2015-09-04 00:20:58 +02:00
|
|
|
return $this->maxLocks;
|
|
|
|
}
|
|
|
|
|
2015-08-31 19:12:35 +02:00
|
|
|
/**
|
|
|
|
* Gets the access permissions of the semaphore.
|
|
|
|
*
|
|
|
|
* @return int A permissions mode.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function getPermissions(): int {
|
|
|
|
$stat = \msg_stat_queue($this->queue);
|
2015-08-31 19:12:35 +02:00
|
|
|
return $stat['msg_perm.mode'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the access permissions of the semaphore.
|
|
|
|
*
|
|
|
|
* The current user must have access to the semaphore in order to change the permissions.
|
|
|
|
*
|
|
|
|
* @param int $mode A permissions mode to set.
|
|
|
|
*
|
|
|
|
* @throws SemaphoreException If the operation failed.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function setPermissions(int $mode) {
|
|
|
|
if (!\msg_set_queue($this->queue, [
|
2015-08-31 19:12:35 +02:00
|
|
|
'msg_perm.mode' => $mode
|
|
|
|
])) {
|
|
|
|
throw new SemaphoreException('Failed to change the semaphore permissions.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-31 11:18:14 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function count(): int {
|
|
|
|
$stat = \msg_stat_queue($this->queue);
|
2015-08-31 11:18:14 +02:00
|
|
|
return $stat['msg_qnum'];
|
|
|
|
}
|
|
|
|
|
2016-11-15 00:43:44 +01:00
|
|
|
public function acquire(): Promise {
|
2016-08-18 18:04:48 +02:00
|
|
|
return new Coroutine($this->doAcquire());
|
|
|
|
}
|
|
|
|
|
2015-08-31 11:18:14 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2015-08-11 05:27:10 +02:00
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
private function doAcquire(): \Generator {
|
2015-09-04 06:23:12 +02:00
|
|
|
do {
|
2015-08-31 11:18:14 +02:00
|
|
|
// Attempt to acquire a lock from the semaphore.
|
2016-08-23 01:25:19 +02:00
|
|
|
if (@\msg_receive($this->queue, 0, $type, 1, $chr, false, \MSG_IPC_NOWAIT, $errno)) {
|
2015-08-31 11:18:14 +02:00
|
|
|
// A free lock was found, so resolve with a lock object that can
|
|
|
|
// be used to release the lock.
|
2016-01-23 07:00:56 +01:00
|
|
|
return new Lock(function (Lock $lock) {
|
2015-08-31 11:18:14 +02:00
|
|
|
$this->release();
|
|
|
|
});
|
2015-08-11 05:27:10 +02:00
|
|
|
}
|
|
|
|
|
2015-08-31 11:18:14 +02:00
|
|
|
// Check for unusual errors.
|
2016-08-23 01:25:19 +02:00
|
|
|
if ($errno !== \MSG_ENOMSG) {
|
2015-08-31 11:18:14 +02:00
|
|
|
throw new SemaphoreException('Failed to acquire a lock.');
|
|
|
|
}
|
2016-08-19 00:36:58 +02:00
|
|
|
} while (yield new Pause(self::LATENCY_TIMEOUT, true));
|
2015-08-11 05:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the semaphore if it still exists.
|
2015-08-31 11:18:14 +02:00
|
|
|
*
|
|
|
|
* @throws SemaphoreException If the operation failed.
|
2015-08-11 05:27:10 +02:00
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function free() {
|
|
|
|
if (\is_resource($this->queue) && \msg_queue_exists($this->key)) {
|
|
|
|
if (!\msg_remove_queue($this->queue)) {
|
2015-08-31 11:18:14 +02:00
|
|
|
throw new SemaphoreException('Failed to free the semaphore.');
|
2015-08-11 05:27:10 +02:00
|
|
|
}
|
|
|
|
|
2015-08-31 19:48:07 +02:00
|
|
|
$this->queue = null;
|
2015-08-31 11:18:14 +02:00
|
|
|
}
|
2015-08-11 05:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serializes the semaphore.
|
|
|
|
*
|
|
|
|
* @return string The serialized semaphore.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function serialize(): string {
|
|
|
|
return \serialize([$this->key, $this->maxLocks]);
|
2015-08-11 05:27:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unserializes a serialized semaphore.
|
|
|
|
*
|
|
|
|
* @param string $serialized The serialized semaphore.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function unserialize($serialized) {
|
2015-08-31 11:18:14 +02:00
|
|
|
// Get the semaphore key and attempt to re-connect to the semaphore in memory.
|
2016-08-18 18:04:48 +02:00
|
|
|
list($this->key, $this->maxLocks) = \unserialize($serialized);
|
2015-08-11 05:27:10 +02:00
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
if (\msg_queue_exists($this->key)) {
|
|
|
|
$this->queue = \msg_get_queue($this->key);
|
2015-08-11 05:27:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-04 00:20:58 +02:00
|
|
|
/**
|
|
|
|
* Clones the semaphore, creating a new semaphore with the same size and permissions.
|
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
public function __clone() {
|
2015-09-04 01:31:29 +02:00
|
|
|
$this->init($this->maxLocks, $this->getPermissions());
|
2015-09-04 00:20:58 +02:00
|
|
|
}
|
|
|
|
|
2015-08-11 05:27:10 +02:00
|
|
|
/**
|
2015-08-31 11:18:14 +02:00
|
|
|
* Releases a lock from the semaphore.
|
|
|
|
*
|
|
|
|
* @throws SemaphoreException If the operation failed.
|
2015-08-11 05:27:10 +02:00
|
|
|
*/
|
2016-08-18 18:04:48 +02:00
|
|
|
protected function release() {
|
2015-08-31 18:42:30 +02:00
|
|
|
// Call send in non-blocking mode. If the call fails because the queue
|
|
|
|
// is full, then the number of locks configured is too large.
|
2016-08-18 18:04:48 +02:00
|
|
|
if (!@\msg_send($this->queue, 1, "\0", false, false, $errno)) {
|
2016-08-23 01:25:19 +02:00
|
|
|
if ($errno === \MSG_EAGAIN) {
|
2015-08-31 18:42:30 +02:00
|
|
|
throw new SemaphoreException('The semaphore size is larger than the system allows.');
|
|
|
|
}
|
|
|
|
|
2015-08-31 11:18:14 +02:00
|
|
|
throw new SemaphoreException('Failed to release the lock.');
|
2015-08-11 05:27:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|