2016-06-08 16:12:42 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Amp\Loop;
|
|
|
|
|
|
|
|
use Amp\Loop\Internal\Watcher;
|
2017-01-07 13:45:03 +01:00
|
|
|
use AsyncInterop\Loop\Driver;
|
|
|
|
use AsyncInterop\Loop\InvalidWatcherException;
|
2016-06-08 16:12:42 +02:00
|
|
|
|
2016-08-06 15:58:37 +02:00
|
|
|
abstract class Loop extends Driver {
|
2017-01-16 17:39:24 +01:00
|
|
|
// Don't use 1e3 / 1e6, they result in a float instead of int
|
|
|
|
const MILLISEC_PER_SEC = 1000;
|
|
|
|
const MICROSEC_PER_SEC = 1000000;
|
2016-06-08 16:12:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $nextId = "a";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Amp\Loop\Internal\Watcher[]
|
|
|
|
*/
|
|
|
|
private $watchers = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Amp\Loop\Internal\Watcher[]
|
|
|
|
*/
|
|
|
|
private $enableQueue = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Amp\Loop\Internal\Watcher[]
|
|
|
|
*/
|
|
|
|
private $deferQueue = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Amp\Loop\Internal\Watcher[]
|
|
|
|
*/
|
|
|
|
private $nextTickQueue = [];
|
|
|
|
|
|
|
|
/**
|
2017-01-16 17:39:24 +01:00
|
|
|
* @var callable|null
|
2016-06-08 16:12:42 +02:00
|
|
|
*/
|
|
|
|
private $errorHandler;
|
|
|
|
|
|
|
|
/**
|
2016-12-18 21:56:47 +01:00
|
|
|
* @var bool
|
2016-06-08 16:12:42 +02:00
|
|
|
*/
|
2016-12-18 21:56:47 +01:00
|
|
|
private $running = false;
|
2016-06-08 16:12:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function run() {
|
2016-12-18 21:56:47 +01:00
|
|
|
$previous = $this->running;
|
|
|
|
$this->running = true;
|
2016-06-08 16:12:42 +02:00
|
|
|
|
|
|
|
try {
|
2016-12-18 21:56:47 +01:00
|
|
|
while ($this->running) {
|
2016-06-08 16:12:42 +02:00
|
|
|
if ($this->isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->tick();
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
$this->running = $previous;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function stop() {
|
2016-12-18 21:56:47 +01:00
|
|
|
$this->running = false;
|
2016-06-08 16:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool True if no enabled and referenced watchers remain in the loop.
|
|
|
|
*/
|
|
|
|
private function isEmpty() {
|
|
|
|
foreach ($this->watchers as $watcher) {
|
|
|
|
if ($watcher->enabled && $watcher->referenced) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes a single tick of the event loop.
|
|
|
|
*/
|
|
|
|
private function tick() {
|
2017-01-06 17:15:54 +01:00
|
|
|
$this->deferQueue = \array_merge($this->deferQueue, $this->nextTickQueue);
|
|
|
|
$this->nextTickQueue = [];
|
|
|
|
|
2017-01-06 17:21:58 +01:00
|
|
|
$this->activate($this->enableQueue);
|
2017-01-06 17:15:54 +01:00
|
|
|
$this->enableQueue = [];
|
|
|
|
|
2016-06-08 16:12:42 +02:00
|
|
|
try {
|
|
|
|
foreach ($this->deferQueue as $watcher) {
|
|
|
|
if (!isset($this->deferQueue[$watcher->id])) {
|
|
|
|
continue; // Watcher disabled by another defer watcher.
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($this->watchers[$watcher->id], $this->deferQueue[$watcher->id]);
|
|
|
|
|
|
|
|
$callback = $watcher->callback;
|
|
|
|
$callback($watcher->id, $watcher->data);
|
|
|
|
}
|
|
|
|
|
2016-12-18 21:56:47 +01:00
|
|
|
$this->dispatch(empty($this->nextTickQueue) && empty($this->enableQueue) && $this->running);
|
2016-06-08 16:12:42 +02:00
|
|
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
if (null === $this->errorHandler) {
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
$errorHandler = $this->errorHandler;
|
|
|
|
$errorHandler($exception);
|
|
|
|
} catch (\Exception $exception) { // @todo Remove when PHP 5.x support is no longer needed.
|
|
|
|
if (null === $this->errorHandler) {
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
$errorHandler = $this->errorHandler;
|
|
|
|
$errorHandler($exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatches any pending read/write, timer, and signal events.
|
|
|
|
*
|
|
|
|
* @param bool $blocking
|
|
|
|
*/
|
|
|
|
abstract protected function dispatch($blocking);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Activates (enables) all the given watchers.
|
|
|
|
*
|
|
|
|
* @param \Amp\Loop\Internal\Watcher[] $watchers
|
|
|
|
*/
|
|
|
|
abstract protected function activate(array $watchers);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deactivates (disables) the given watcher.
|
|
|
|
*
|
|
|
|
* @param \Amp\Loop\Internal\Watcher $watcher
|
|
|
|
*/
|
|
|
|
abstract protected function deactivate(Watcher $watcher);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function defer(callable $callback, $data = null) {
|
|
|
|
$watcher = new Watcher;
|
|
|
|
$watcher->type = Watcher::DEFER;
|
|
|
|
$watcher->id = $this->nextId++;
|
|
|
|
$watcher->callback = $callback;
|
|
|
|
$watcher->data = $data;
|
|
|
|
|
|
|
|
$this->watchers[$watcher->id] = $watcher;
|
|
|
|
$this->nextTickQueue[$watcher->id] = $watcher;
|
|
|
|
|
|
|
|
return $watcher->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function delay($delay, callable $callback, $data = null) {
|
|
|
|
$delay = (int) $delay;
|
|
|
|
|
|
|
|
if ($delay < 0) {
|
|
|
|
throw new \InvalidArgumentException("Delay must be greater than or equal to zero");
|
|
|
|
}
|
|
|
|
|
|
|
|
$watcher = new Watcher;
|
|
|
|
$watcher->type = Watcher::DELAY;
|
|
|
|
$watcher->id = $this->nextId++;
|
|
|
|
$watcher->callback = $callback;
|
|
|
|
$watcher->value = $delay;
|
|
|
|
$watcher->data = $data;
|
|
|
|
|
|
|
|
$this->watchers[$watcher->id] = $watcher;
|
|
|
|
$this->enableQueue[$watcher->id] = $watcher;
|
|
|
|
|
|
|
|
return $watcher->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function repeat($interval, callable $callback, $data = null) {
|
|
|
|
$interval = (int) $interval;
|
|
|
|
|
|
|
|
if ($interval < 0) {
|
|
|
|
throw new \InvalidArgumentException("Interval must be greater than or equal to zero");
|
|
|
|
}
|
|
|
|
|
|
|
|
$watcher = new Watcher;
|
|
|
|
$watcher->type = Watcher::REPEAT;
|
|
|
|
$watcher->id = $this->nextId++;
|
|
|
|
$watcher->callback = $callback;
|
|
|
|
$watcher->value = $interval;
|
|
|
|
$watcher->data = $data;
|
|
|
|
|
|
|
|
$this->watchers[$watcher->id] = $watcher;
|
|
|
|
$this->enableQueue[$watcher->id] = $watcher;
|
|
|
|
|
|
|
|
return $watcher->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function onReadable($stream, callable $callback, $data = null) {
|
|
|
|
$watcher = new Watcher;
|
|
|
|
$watcher->type = Watcher::READABLE;
|
|
|
|
$watcher->id = $this->nextId++;
|
|
|
|
$watcher->callback = $callback;
|
|
|
|
$watcher->value = $stream;
|
|
|
|
$watcher->data = $data;
|
|
|
|
|
|
|
|
$this->watchers[$watcher->id] = $watcher;
|
|
|
|
$this->enableQueue[$watcher->id] = $watcher;
|
|
|
|
|
|
|
|
return $watcher->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function onWritable($stream, callable $callback, $data = null) {
|
|
|
|
$watcher = new Watcher;
|
|
|
|
$watcher->type = Watcher::WRITABLE;
|
|
|
|
$watcher->id = $this->nextId++;
|
|
|
|
$watcher->callback = $callback;
|
|
|
|
$watcher->value = $stream;
|
|
|
|
$watcher->data = $data;
|
|
|
|
|
|
|
|
$this->watchers[$watcher->id] = $watcher;
|
|
|
|
$this->enableQueue[$watcher->id] = $watcher;
|
|
|
|
|
|
|
|
return $watcher->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
2017-01-07 13:45:03 +01:00
|
|
|
* @throws \AsyncInterop\Loop\UnsupportedFeatureException If the pcntl extension is not available.
|
2016-06-08 16:12:42 +02:00
|
|
|
* @throws \RuntimeException If creating the backend signal handler fails.
|
|
|
|
*/
|
|
|
|
public function onSignal($signo, callable $callback, $data = null) {
|
|
|
|
$watcher = new Watcher;
|
|
|
|
$watcher->type = Watcher::SIGNAL;
|
|
|
|
$watcher->id = $this->nextId++;
|
|
|
|
$watcher->callback = $callback;
|
|
|
|
$watcher->value = $signo;
|
|
|
|
$watcher->data = $data;
|
|
|
|
|
|
|
|
$this->watchers[$watcher->id] = $watcher;
|
|
|
|
$this->enableQueue[$watcher->id] = $watcher;
|
|
|
|
|
|
|
|
return $watcher->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function enable($watcherIdentifier) {
|
|
|
|
if (!isset($this->watchers[$watcherIdentifier])) {
|
2017-01-11 14:28:27 +01:00
|
|
|
throw new InvalidWatcherException($watcherIdentifier, "Cannot enable an invalid watcher identifier: '{$watcherIdentifier}'");
|
2016-06-08 16:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$watcher = $this->watchers[$watcherIdentifier];
|
|
|
|
|
|
|
|
if ($watcher->enabled) {
|
|
|
|
return; // Watcher already enabled.
|
|
|
|
}
|
|
|
|
|
|
|
|
$watcher->enabled = true;
|
|
|
|
|
|
|
|
switch ($watcher->type) {
|
|
|
|
case Watcher::DEFER:
|
|
|
|
$this->nextTickQueue[$watcher->id] = $watcher;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$this->enableQueue[$watcher->id] = $watcher;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-09-10 00:38:22 +02:00
|
|
|
public function disable($watcherIdentifier) {
|
2016-06-08 16:12:42 +02:00
|
|
|
if (!isset($this->watchers[$watcherIdentifier])) {
|
2016-09-10 00:38:22 +02:00
|
|
|
return;
|
2016-06-08 16:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$watcher = $this->watchers[$watcherIdentifier];
|
|
|
|
|
|
|
|
if (!$watcher->enabled) {
|
|
|
|
return; // Watcher already disabled.
|
|
|
|
}
|
|
|
|
|
|
|
|
$watcher->enabled = false;
|
|
|
|
$id = $watcher->id;
|
|
|
|
|
|
|
|
switch ($watcher->type) {
|
|
|
|
case Watcher::DEFER:
|
|
|
|
if (isset($this->nextTickQueue[$id])) {
|
|
|
|
// Watcher was only queued to be enabled.
|
|
|
|
unset($this->nextTickQueue[$id]);
|
|
|
|
} else {
|
|
|
|
unset($this->deferQueue[$id]);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (isset($this->enableQueue[$id])) {
|
|
|
|
// Watcher was only queued to be enabled.
|
|
|
|
unset($this->enableQueue[$id]);
|
|
|
|
} else {
|
|
|
|
$this->deactivate($watcher);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function cancel($watcherIdentifier) {
|
|
|
|
$this->disable($watcherIdentifier);
|
|
|
|
unset($this->watchers[$watcherIdentifier]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function reference($watcherIdentifier) {
|
|
|
|
if (!isset($this->watchers[$watcherIdentifier])) {
|
2017-01-11 14:28:27 +01:00
|
|
|
throw new InvalidWatcherException($watcherIdentifier, "Cannot reference an invalid watcher identifier: '{$watcherIdentifier}'");
|
2016-06-08 16:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->watchers[$watcherIdentifier]->referenced = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function unreference($watcherIdentifier) {
|
|
|
|
if (!isset($this->watchers[$watcherIdentifier])) {
|
2017-01-11 14:28:27 +01:00
|
|
|
throw new InvalidWatcherException($watcherIdentifier, "Cannot unreference an invalid watcher identifier: '{$watcherIdentifier}'");
|
2016-06-08 16:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->watchers[$watcherIdentifier]->referenced = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function setErrorHandler(callable $callback = null) {
|
2016-12-28 19:59:04 +01:00
|
|
|
$previous = $this->errorHandler;
|
2016-06-08 16:12:42 +02:00
|
|
|
$this->errorHandler = $callback;
|
2016-12-28 19:59:04 +01:00
|
|
|
return $previous;
|
2016-06-08 16:12:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-12-28 19:59:04 +01:00
|
|
|
public function getInfo() {
|
2016-06-08 16:12:42 +02:00
|
|
|
$watchers = [
|
|
|
|
"referenced" => 0,
|
|
|
|
"unreferenced" => 0,
|
|
|
|
];
|
|
|
|
|
|
|
|
$defer = $delay = $repeat = $onReadable = $onWritable = $onSignal = [
|
|
|
|
"enabled" => 0,
|
|
|
|
"disabled" => 0,
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($this->watchers as $watcher) {
|
|
|
|
switch ($watcher->type) {
|
|
|
|
case Watcher::READABLE: $array = &$onReadable; break;
|
|
|
|
case Watcher::WRITABLE: $array = &$onWritable; break;
|
|
|
|
case Watcher::SIGNAL: $array = &$onSignal; break;
|
|
|
|
case Watcher::DEFER: $array = &$defer; break;
|
|
|
|
case Watcher::DELAY: $array = &$delay; break;
|
|
|
|
case Watcher::REPEAT: $array = &$repeat; break;
|
|
|
|
|
|
|
|
default: throw new \DomainException("Unknown watcher type");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($watcher->enabled) {
|
|
|
|
++$array["enabled"];
|
|
|
|
|
|
|
|
if ($watcher->referenced) {
|
|
|
|
++$watchers["referenced"];
|
|
|
|
} else {
|
|
|
|
++$watchers["unreferenced"];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
++$array["disabled"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
"watchers" => $watchers,
|
|
|
|
"defer" => $defer,
|
|
|
|
"delay" => $delay,
|
|
|
|
"repeat" => $repeat,
|
|
|
|
"on_readable" => $onReadable,
|
|
|
|
"on_writable" => $onWritable,
|
|
|
|
"on_signal" => $onSignal,
|
|
|
|
"running" => $this->running,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-01-05 19:39:34 +01:00
|
|
|
* Returns the same array of data as getInfo().
|
2016-06-08 16:12:42 +02:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function __debugInfo() {
|
2016-12-29 01:50:41 +01:00
|
|
|
return $this->getInfo();
|
2016-06-08 16:12:42 +02:00
|
|
|
}
|
|
|
|
}
|