Initial Commit

This commit is contained in:
David Stone
2024-11-30 18:24:12 -07:00
commit e8f7955c1c
5432 changed files with 1397750 additions and 0 deletions

View File

@ -0,0 +1,649 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Loop;
use WP_Ultimo\Dependencies\Amp\Coroutine;
use WP_Ultimo\Dependencies\Amp\Promise;
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
use function WP_Ultimo\Dependencies\Amp\Promise\rethrow;
/**
* Event loop driver which implements all basic operations to allow interoperability.
*
* Watchers (enabled or new watchers) MUST immediately be marked as enabled, but only be activated (i.e. callbacks can
* be called) right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
*
* All registered callbacks MUST NOT be called from a file with strict types enabled (`declare(strict_types=1)`).
*/
abstract class Driver
{
// Don't use 1e3 / 1e6, they result in a float instead of int
const MILLISEC_PER_SEC = 1000;
const MICROSEC_PER_SEC = 1000000;
/** @var string */
private $nextId = "a";
/** @var Watcher[] */
private $watchers = [];
/** @var Watcher[] */
private $enableQueue = [];
/** @var Watcher[] */
private $deferQueue = [];
/** @var Watcher[] */
private $nextTickQueue = [];
/** @var callable(\Throwable):void|null */
private $errorHandler;
/** @var bool */
private $running = \false;
/** @var array */
private $registry = [];
/**
* Run the event loop.
*
* One iteration of the loop is called one "tick". A tick covers the following steps:
*
* 1. Activate watchers created / enabled in the last tick / before `run()`.
* 2. Execute all enabled defer watchers.
* 3. Execute all due timer, pending signal and actionable stream callbacks, each only once per tick.
*
* The loop MUST continue to run until it is either stopped explicitly, no referenced watchers exist anymore, or an
* exception is thrown that cannot be handled. Exceptions that cannot be handled are exceptions thrown from an
* error handler or exceptions that would be passed to an error handler but none exists to handle them.
*
* @return void
*/
public function run()
{
$this->running = \true;
try {
while ($this->running) {
if ($this->isEmpty()) {
return;
}
$this->tick();
}
} finally {
$this->stop();
}
}
/**
* @return bool True if no enabled and referenced watchers remain in the loop.
*/
private function isEmpty() : bool
{
foreach ($this->watchers as $watcher) {
if ($watcher->enabled && $watcher->referenced) {
return \false;
}
}
return \true;
}
/**
* Executes a single tick of the event loop.
*
* @return void
*/
private function tick()
{
if (empty($this->deferQueue)) {
$this->deferQueue = $this->nextTickQueue;
} else {
$this->deferQueue = \array_merge($this->deferQueue, $this->nextTickQueue);
}
$this->nextTickQueue = [];
$this->activate($this->enableQueue);
$this->enableQueue = [];
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]);
try {
/** @var mixed $result */
$result = ($watcher->callback)($watcher->id, $watcher->data);
if ($result === null) {
continue;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
}
/** @psalm-suppress RedundantCondition */
$this->dispatch(empty($this->nextTickQueue) && empty($this->enableQueue) && $this->running && !$this->isEmpty());
}
/**
* Activates (enables) all the given watchers.
*
* @param Watcher[] $watchers
*
* @return void
*/
protected abstract function activate(array $watchers);
/**
* Dispatches any pending read/write, timer, and signal events.
*
* @param bool $blocking
*
* @return void
*/
protected abstract function dispatch(bool $blocking);
/**
* Stop the event loop.
*
* When an event loop is stopped, it continues with its current tick and exits the loop afterwards. Multiple calls
* to stop MUST be ignored and MUST NOT raise an exception.
*
* @return void
*/
public function stop()
{
$this->running = \false;
}
/**
* Defer the execution of a callback.
*
* The deferred callable MUST be executed before any other type of watcher in a tick. Order of enabling MUST be
* preserved when executing the callbacks.
*
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
*
* @param callable (string $watcherId, mixed $data) $callback The callback to defer. The `$watcherId` will be
* invalidated before the callback call.
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
*
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
*/
public function defer(callable $callback, $data = null) : string
{
/** @psalm-var Watcher<null> $watcher */
$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;
}
/**
* Delay the execution of a callback.
*
* The delay is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be determined by which
* timers expire first, but timers with the same expiration time MAY be executed in any order.
*
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
*
* @param int $delay The amount of time, in milliseconds, to delay the execution for.
* @param callable (string $watcherId, mixed $data) $callback The callback to delay. The `$watcherId` will be
* invalidated before the callback call.
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
*
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
*/
public function delay(int $delay, callable $callback, $data = null) : string
{
if ($delay < 0) {
throw new \Error("Delay must be greater than or equal to zero");
}
/** @psalm-var Watcher<int> $watcher */
$watcher = new Watcher();
$watcher->type = Watcher::DELAY;
$watcher->id = $this->nextId++;
$watcher->callback = $callback;
$watcher->value = $delay;
$watcher->expiration = $this->now() + $delay;
$watcher->data = $data;
$this->watchers[$watcher->id] = $watcher;
$this->enableQueue[$watcher->id] = $watcher;
return $watcher->id;
}
/**
* Repeatedly execute a callback.
*
* The interval between executions is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be
* determined by which timers expire first, but timers with the same expiration time MAY be executed in any order.
* The first execution is scheduled after the first interval period.
*
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
*
* @param int $interval The time interval, in milliseconds, to wait between executions.
* @param callable (string $watcherId, mixed $data) $callback The callback to repeat.
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
*
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
*/
public function repeat(int $interval, callable $callback, $data = null) : string
{
if ($interval < 0) {
throw new \Error("Interval must be greater than or equal to zero");
}
/** @psalm-var Watcher<int> $watcher */
$watcher = new Watcher();
$watcher->type = Watcher::REPEAT;
$watcher->id = $this->nextId++;
$watcher->callback = $callback;
$watcher->value = $interval;
$watcher->expiration = $this->now() + $interval;
$watcher->data = $data;
$this->watchers[$watcher->id] = $watcher;
$this->enableQueue[$watcher->id] = $watcher;
return $watcher->id;
}
/**
* Execute a callback when a stream resource becomes readable or is closed for reading.
*
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
* watcher when closing the resource locally. Drivers MAY choose to notify the user if there are watchers on invalid
* resources, but are not required to, due to the high performance impact. Watchers on closed resources are
* therefore undefined behavior.
*
* Multiple watchers on the same stream MAY be executed in any order.
*
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
*
* @param resource $stream The stream to monitor.
* @param callable (string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
*
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
*/
public function onReadable($stream, callable $callback, $data = null) : string
{
/** @psalm-var Watcher<resource> $watcher */
$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;
}
/**
* Execute a callback when a stream resource becomes writable or is closed for writing.
*
* Warning: Closing resources locally, e.g. with `fclose`, might not invoke the callback. Be sure to `cancel` the
* watcher when closing the resource locally. Drivers MAY choose to notify the user if there are watchers on invalid
* resources, but are not required to, due to the high performance impact. Watchers on closed resources are
* therefore undefined behavior.
*
* Multiple watchers on the same stream MAY be executed in any order.
*
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
*
* @param resource $stream The stream to monitor.
* @param callable (string $watcherId, resource $stream, mixed $data) $callback The callback to execute.
* @param mixed $data Arbitrary data given to the callback function as the `$data` parameter.
*
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
*/
public function onWritable($stream, callable $callback, $data = null) : string
{
/** @psalm-var Watcher<resource> $watcher */
$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;
}
/**
* Execute a callback when a signal is received.
*
* Warning: Installing the same signal on different instances of this interface is deemed undefined behavior.
* Implementations MAY try to detect this, if possible, but are not required to. This is due to technical
* limitations of the signals being registered globally per process.
*
* Multiple watchers on the same signal MAY be executed in any order.
*
* The created watcher MUST immediately be marked as enabled, but only be activated (i.e. callback can be called)
* right before the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
*
* @param int $signo The signal number to monitor.
* @param callable (string $watcherId, int $signo, mixed $data) $callback The callback to execute.
* @param mixed $data Arbitrary data given to the callback function as the $data parameter.
*
* @return string An unique identifier that can be used to cancel, enable or disable the watcher.
*
* @throws UnsupportedFeatureException If signal handling is not supported.
*/
public function onSignal(int $signo, callable $callback, $data = null) : string
{
/** @psalm-var Watcher<int> $watcher */
$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;
}
/**
* Enable a watcher to be active starting in the next tick.
*
* Watchers MUST immediately be marked as enabled, but only be activated (i.e. callbacks can be called) right before
* the next tick. Callbacks of watchers MUST NOT be called in the tick they were enabled.
*
* @param string $watcherId The watcher identifier.
*
* @return void
*
* @throws InvalidWatcherError If the watcher identifier is invalid.
*/
public function enable(string $watcherId)
{
if (!isset($this->watchers[$watcherId])) {
throw new InvalidWatcherError($watcherId, "Cannot enable an invalid watcher identifier: '{$watcherId}'");
}
$watcher = $this->watchers[$watcherId];
if ($watcher->enabled) {
return;
// Watcher already enabled.
}
$watcher->enabled = \true;
switch ($watcher->type) {
case Watcher::DEFER:
$this->nextTickQueue[$watcher->id] = $watcher;
break;
case Watcher::REPEAT:
case Watcher::DELAY:
\assert(\is_int($watcher->value));
$watcher->expiration = $this->now() + $watcher->value;
$this->enableQueue[$watcher->id] = $watcher;
break;
default:
$this->enableQueue[$watcher->id] = $watcher;
break;
}
}
/**
* Cancel a watcher.
*
* This will detach the event loop from all resources that are associated to the watcher. After this operation the
* watcher is permanently invalid. Calling this function MUST NOT fail, even if passed an invalid watcher.
*
* @param string $watcherId The watcher identifier.
*
* @return void
*/
public function cancel(string $watcherId)
{
$this->disable($watcherId);
unset($this->watchers[$watcherId]);
}
/**
* Disable a watcher immediately.
*
* A watcher MUST be disabled immediately, e.g. if a defer watcher disables a later defer watcher, the second defer
* watcher isn't executed in this tick.
*
* Disabling a watcher MUST NOT invalidate the watcher. Calling this function MUST NOT fail, even if passed an
* invalid watcher.
*
* @param string $watcherId The watcher identifier.
*
* @return void
*/
public function disable(string $watcherId)
{
if (!isset($this->watchers[$watcherId])) {
return;
}
$watcher = $this->watchers[$watcherId];
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;
}
}
/**
* Deactivates (disables) the given watcher.
*
* @param Watcher $watcher
*
* @return void
*/
protected abstract function deactivate(Watcher $watcher);
/**
* Reference a watcher.
*
* This will keep the event loop alive whilst the watcher is still being monitored. Watchers have this state by
* default.
*
* @param string $watcherId The watcher identifier.
*
* @return void
*
* @throws InvalidWatcherError If the watcher identifier is invalid.
*/
public function reference(string $watcherId)
{
if (!isset($this->watchers[$watcherId])) {
throw new InvalidWatcherError($watcherId, "Cannot reference an invalid watcher identifier: '{$watcherId}'");
}
$this->watchers[$watcherId]->referenced = \true;
}
/**
* Unreference a watcher.
*
* The event loop should exit the run method when only unreferenced watchers are still being monitored. Watchers
* are all referenced by default.
*
* @param string $watcherId The watcher identifier.
*
* @return void
*/
public function unreference(string $watcherId)
{
if (!isset($this->watchers[$watcherId])) {
return;
}
$this->watchers[$watcherId]->referenced = \false;
}
/**
* Stores information in the loop bound registry.
*
* Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages
* MUST use their namespace as prefix for keys. They may do so by using `SomeClass::class` as key.
*
* If packages want to expose loop bound state to consumers other than the package, they SHOULD provide a dedicated
* interface for that purpose instead of sharing the storage key.
*
* @param string $key The namespaced storage key.
* @param mixed $value The value to be stored.
*
* @return void
*/
public final function setState(string $key, $value)
{
if ($value === null) {
unset($this->registry[$key]);
} else {
$this->registry[$key] = $value;
}
}
/**
* Gets information stored bound to the loop.
*
* Stored information is package private. Packages MUST NOT retrieve the stored state of other packages. Packages
* MUST use their namespace as prefix for keys. They may do so by using `SomeClass::class` as key.
*
* If packages want to expose loop bound state to consumers other than the package, they SHOULD provide a dedicated
* interface for that purpose instead of sharing the storage key.
*
* @param string $key The namespaced storage key.
*
* @return mixed The previously stored value or `null` if it doesn't exist.
*/
public final function getState(string $key)
{
return isset($this->registry[$key]) ? $this->registry[$key] : null;
}
/**
* Set a callback to be executed when an error occurs.
*
* The callback receives the error as the first and only parameter. The return value of the callback gets ignored.
* If it can't handle the error, it MUST throw the error. Errors thrown by the callback or during its invocation
* MUST be thrown into the `run` loop and stop the driver.
*
* Subsequent calls to this method will overwrite the previous handler.
*
* @param callable(\Throwable $error):void|null $callback The callback to execute. `null` will clear the
* current handler.
*
* @return callable(\Throwable $error):void|null The previous handler, `null` if there was none.
*/
public function setErrorHandler(callable $callback = null)
{
$previous = $this->errorHandler;
$this->errorHandler = $callback;
return $previous;
}
/**
* Invokes the error handler with the given exception.
*
* @param \Throwable $exception The exception thrown from a watcher callback.
*
* @return void
* @throws \Throwable If no error handler has been set.
*/
protected function error(\Throwable $exception)
{
if ($this->errorHandler === null) {
throw $exception;
}
($this->errorHandler)($exception);
}
/**
* Returns the current loop time in millisecond increments. Note this value does not necessarily correlate to
* wall-clock time, rather the value returned is meant to be used in relative comparisons to prior values returned
* by this method (intervals, expiration calculations, etc.) and is only updated once per loop tick.
*
* Extending classes should override this function to return a value cached once per loop tick.
*
* @return int
*/
public function now() : int
{
return (int) (\microtime(\true) * self::MILLISEC_PER_SEC);
}
/**
* Get the underlying loop handle.
*
* Example: the `uv_loop` resource for `libuv` or the `EvLoop` object for `libev` or `null` for a native driver.
*
* Note: This function is *not* exposed in the `Loop` class. Users shall access it directly on the respective loop
* instance.
*
* @return null|object|resource The loop handle the event loop operates on. `null` if there is none.
*/
public abstract function getHandle();
/**
* Returns the same array of data as getInfo().
*
* @return array
*/
public function __debugInfo()
{
// @codeCoverageIgnoreStart
return $this->getInfo();
// @codeCoverageIgnoreEnd
}
/**
* Retrieve an associative array of information about the event loop driver.
*
* The returned array MUST contain the following data describing the driver's currently registered watchers:
*
* [
* "defer" => ["enabled" => int, "disabled" => int],
* "delay" => ["enabled" => int, "disabled" => int],
* "repeat" => ["enabled" => int, "disabled" => int],
* "on_readable" => ["enabled" => int, "disabled" => int],
* "on_writable" => ["enabled" => int, "disabled" => int],
* "on_signal" => ["enabled" => int, "disabled" => int],
* "enabled_watchers" => ["referenced" => int, "unreferenced" => int],
* "running" => bool
* ];
*
* Implementations MAY optionally add more information in the array but at minimum the above `key => value` format
* MUST always be provided.
*
* @return array Statistics about the loop in the described format.
*/
public function getInfo() : array
{
$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:
// @codeCoverageIgnoreStart
throw new \Error("Unknown watcher type");
}
if ($watcher->enabled) {
++$array["enabled"];
if ($watcher->referenced) {
++$watchers["referenced"];
} else {
++$watchers["unreferenced"];
}
} else {
++$array["disabled"];
}
}
return ["enabled_watchers" => $watchers, "defer" => $defer, "delay" => $delay, "repeat" => $repeat, "on_readable" => $onReadable, "on_writable" => $onWritable, "on_signal" => $onSignal, "running" => (bool) $this->running];
}
}

View File

@ -0,0 +1,55 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Loop;
// @codeCoverageIgnoreStart
class DriverFactory
{
/**
* Creates a new loop instance and chooses the best available driver.
*
* @return Driver
*
* @throws \Error If an invalid class has been specified via AMP_LOOP_DRIVER
*/
public function create() : Driver
{
$driver = (function () {
if ($driver = $this->createDriverFromEnv()) {
return $driver;
}
if (UvDriver::isSupported()) {
return new UvDriver();
}
if (EvDriver::isSupported()) {
return new EvDriver();
}
if (EventDriver::isSupported()) {
return new EventDriver();
}
return new NativeDriver();
})();
if (\getenv("AMP_DEBUG_TRACE_WATCHERS")) {
return new TracingDriver($driver);
}
return $driver;
}
/**
* @return Driver|null
*/
private function createDriverFromEnv()
{
$driver = \getenv("AMP_LOOP_DRIVER");
if (!$driver) {
return null;
}
if (!\class_exists($driver)) {
throw new \Error(\sprintf("Driver '%s' does not exist.", $driver));
}
if (!\is_subclass_of($driver, Driver::class)) {
throw new \Error(\sprintf("Driver '%s' is not a subclass of '%s'.", $driver, Driver::class));
}
return new $driver();
}
}
// @codeCoverageIgnoreEnd

View File

@ -0,0 +1,258 @@
<?php
/** @noinspection PhpComposerExtensionStubsInspection */
namespace WP_Ultimo\Dependencies\Amp\Loop;
use WP_Ultimo\Dependencies\Amp\Coroutine;
use WP_Ultimo\Dependencies\Amp\Promise;
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
use function WP_Ultimo\Dependencies\Amp\Internal\getCurrentTime;
use function WP_Ultimo\Dependencies\Amp\Promise\rethrow;
class EvDriver extends Driver
{
/** @var \EvSignal[]|null */
private static $activeSignals;
public static function isSupported() : bool
{
return \extension_loaded("ev");
}
/** @var \EvLoop */
private $handle;
/** @var \EvWatcher[] */
private $events = [];
/** @var callable */
private $ioCallback;
/** @var callable */
private $timerCallback;
/** @var callable */
private $signalCallback;
/** @var \EvSignal[] */
private $signals = [];
/** @var int Internal timestamp for now. */
private $now;
/** @var int Loop time offset */
private $nowOffset;
public function __construct()
{
$this->handle = new \EvLoop();
$this->nowOffset = getCurrentTime();
$this->now = \random_int(0, $this->nowOffset);
$this->nowOffset -= $this->now;
if (self::$activeSignals === null) {
self::$activeSignals =& $this->signals;
}
/**
* @param \EvIO $event
*
* @return void
*/
$this->ioCallback = function (\EvIO $event) {
/** @var Watcher $watcher */
$watcher = $event->data;
try {
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
if ($result === null) {
return;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
};
/**
* @param \EvTimer $event
*
* @return void
*/
$this->timerCallback = function (\EvTimer $event) {
/** @var Watcher $watcher */
$watcher = $event->data;
if ($watcher->type & Watcher::DELAY) {
$this->cancel($watcher->id);
} elseif ($watcher->value === 0) {
// Disable and re-enable so it's not executed repeatedly in the same tick
// See https://github.com/amphp/amp/issues/131
$this->disable($watcher->id);
$this->enable($watcher->id);
}
try {
$result = ($watcher->callback)($watcher->id, $watcher->data);
if ($result === null) {
return;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
};
/**
* @param \EvSignal $event
*
* @return void
*/
$this->signalCallback = function (\EvSignal $event) {
/** @var Watcher $watcher */
$watcher = $event->data;
try {
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
if ($result === null) {
return;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
};
}
/**
* {@inheritdoc}
*/
public function cancel(string $watcherId)
{
parent::cancel($watcherId);
unset($this->events[$watcherId]);
}
public function __destruct()
{
foreach ($this->events as $event) {
/** @psalm-suppress all */
if ($event !== null) {
// Events may have been nulled in extension depending on destruct order.
$event->stop();
}
}
// We need to clear all references to events manually, see
// https://bitbucket.org/osmanov/pecl-ev/issues/31/segfault-in-ev_timer_stop
$this->events = [];
}
/**
* {@inheritdoc}
*/
public function run()
{
$active = self::$activeSignals;
\assert($active !== null);
foreach ($active as $event) {
$event->stop();
}
self::$activeSignals =& $this->signals;
foreach ($this->signals as $event) {
$event->start();
}
try {
parent::run();
} finally {
foreach ($this->signals as $event) {
$event->stop();
}
self::$activeSignals =& $active;
foreach ($active as $event) {
$event->start();
}
}
}
/**
* {@inheritdoc}
*/
public function stop()
{
$this->handle->stop();
parent::stop();
}
/**
* {@inheritdoc}
*/
public function now() : int
{
$this->now = getCurrentTime() - $this->nowOffset;
return $this->now;
}
/**
* {@inheritdoc}
*/
public function getHandle() : \EvLoop
{
return $this->handle;
}
/**
* {@inheritdoc}
*
* @return void
*/
protected function dispatch(bool $blocking)
{
$this->handle->run($blocking ? \Ev::RUN_ONCE : \Ev::RUN_ONCE | \Ev::RUN_NOWAIT);
}
/**
* {@inheritdoc}
*
* @return void
*/
protected function activate(array $watchers)
{
$this->handle->nowUpdate();
$now = $this->now();
foreach ($watchers as $watcher) {
if (!isset($this->events[$id = $watcher->id])) {
switch ($watcher->type) {
case Watcher::READABLE:
\assert(\is_resource($watcher->value));
$this->events[$id] = $this->handle->io($watcher->value, \Ev::READ, $this->ioCallback, $watcher);
break;
case Watcher::WRITABLE:
\assert(\is_resource($watcher->value));
$this->events[$id] = $this->handle->io($watcher->value, \Ev::WRITE, $this->ioCallback, $watcher);
break;
case Watcher::DELAY:
case Watcher::REPEAT:
\assert(\is_int($watcher->value));
$interval = $watcher->value / self::MILLISEC_PER_SEC;
$this->events[$id] = $this->handle->timer(\max(0, ($watcher->expiration - $now) / self::MILLISEC_PER_SEC), $watcher->type & Watcher::REPEAT ? $interval : 0, $this->timerCallback, $watcher);
break;
case Watcher::SIGNAL:
\assert(\is_int($watcher->value));
$this->events[$id] = $this->handle->signal($watcher->value, $this->signalCallback, $watcher);
break;
default:
// @codeCoverageIgnoreStart
throw new \Error("Unknown watcher type");
}
} else {
$this->events[$id]->start();
}
if ($watcher->type === Watcher::SIGNAL) {
/** @psalm-suppress PropertyTypeCoercion */
$this->signals[$id] = $this->events[$id];
}
}
}
/**
* {@inheritdoc}
*
* @return void
*/
protected function deactivate(Watcher $watcher)
{
if (isset($this->events[$id = $watcher->id])) {
$this->events[$id]->stop();
if ($watcher->type === Watcher::SIGNAL) {
unset($this->signals[$id]);
}
}
}
}

View File

@ -0,0 +1,281 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Loop;
use WP_Ultimo\Dependencies\Amp\Coroutine;
use WP_Ultimo\Dependencies\Amp\Promise;
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
use function WP_Ultimo\Dependencies\Amp\Internal\getCurrentTime;
use function WP_Ultimo\Dependencies\Amp\Promise\rethrow;
class EventDriver extends Driver
{
/** @var \Event[]|null */
private static $activeSignals;
/** @var \EventBase */
private $handle;
/** @var \Event[] */
private $events = [];
/** @var callable */
private $ioCallback;
/** @var callable */
private $timerCallback;
/** @var callable */
private $signalCallback;
/** @var \Event[] */
private $signals = [];
/** @var int Internal timestamp for now. */
private $now;
/** @var int Loop time offset */
private $nowOffset;
public function __construct()
{
$config = new \EventConfig();
if (\DIRECTORY_SEPARATOR !== '\\') {
$config->requireFeatures(\EventConfig::FEATURE_FDS);
}
$this->handle = new \EventBase($config);
$this->nowOffset = getCurrentTime();
$this->now = \random_int(0, $this->nowOffset);
$this->nowOffset -= $this->now;
if (self::$activeSignals === null) {
self::$activeSignals =& $this->signals;
}
/**
* @param $resource
* @param $what
* @param Watcher $watcher
*
* @return void
*/
$this->ioCallback = function ($resource, $what, Watcher $watcher) {
\assert(\is_resource($watcher->value));
try {
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
if ($result === null) {
return;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
};
/**
* @param $resource
* @param $what
* @param Watcher $watcher
*
* @return void
*/
$this->timerCallback = function ($resource, $what, Watcher $watcher) {
\assert(\is_int($watcher->value));
if ($watcher->type & Watcher::DELAY) {
$this->cancel($watcher->id);
} else {
$this->events[$watcher->id]->add($watcher->value / self::MILLISEC_PER_SEC);
}
try {
$result = ($watcher->callback)($watcher->id, $watcher->data);
if ($result === null) {
return;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
};
/**
* @param $signum
* @param $what
* @param Watcher $watcher
*
* @return void
*/
$this->signalCallback = function ($signum, $what, Watcher $watcher) {
try {
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
if ($result === null) {
return;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
};
}
/**
* {@inheritdoc}
*/
public function cancel(string $watcherId)
{
parent::cancel($watcherId);
if (isset($this->events[$watcherId])) {
$this->events[$watcherId]->free();
unset($this->events[$watcherId]);
}
}
public static function isSupported() : bool
{
return \extension_loaded("event");
}
/**
* @codeCoverageIgnore
*/
public function __destruct()
{
// Unset here, otherwise $event->del() in the loop may fail with a warning, because __destruct order isn't defined.
// Related https://github.com/amphp/amp/issues/159.
$events = $this->events;
$this->events = [];
foreach ($events as $event) {
if ($event !== null) {
// Events may have been nulled in extension depending on destruct order.
$event->free();
}
}
// Manually free the loop handle to fully release loop resources.
// See https://github.com/amphp/amp/issues/177.
if ($this->handle !== null) {
$this->handle->free();
$this->handle = null;
}
}
/**
* {@inheritdoc}
*/
public function run()
{
$active = self::$activeSignals;
\assert($active !== null);
foreach ($active as $event) {
$event->del();
}
self::$activeSignals =& $this->signals;
foreach ($this->signals as $event) {
/** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
$event->add();
}
try {
parent::run();
} finally {
foreach ($this->signals as $event) {
$event->del();
}
self::$activeSignals =& $active;
foreach ($active as $event) {
/** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
$event->add();
}
}
}
/**
* {@inheritdoc}
*/
public function stop()
{
$this->handle->stop();
parent::stop();
}
/**
* {@inheritdoc}
*/
public function now() : int
{
$this->now = getCurrentTime() - $this->nowOffset;
return $this->now;
}
/**
* {@inheritdoc}
*/
public function getHandle() : \EventBase
{
return $this->handle;
}
/**
* {@inheritdoc}
*
* @return void
*/
protected function dispatch(bool $blocking)
{
$this->handle->loop($blocking ? \EventBase::LOOP_ONCE : \EventBase::LOOP_ONCE | \EventBase::LOOP_NONBLOCK);
}
/**
* {@inheritdoc}
*
* @return void
*/
protected function activate(array $watchers)
{
$now = $this->now();
foreach ($watchers as $watcher) {
if (!isset($this->events[$id = $watcher->id])) {
switch ($watcher->type) {
case Watcher::READABLE:
\assert(\is_resource($watcher->value));
$this->events[$id] = new \Event($this->handle, $watcher->value, \Event::READ | \Event::PERSIST, $this->ioCallback, $watcher);
break;
case Watcher::WRITABLE:
\assert(\is_resource($watcher->value));
$this->events[$id] = new \Event($this->handle, $watcher->value, \Event::WRITE | \Event::PERSIST, $this->ioCallback, $watcher);
break;
case Watcher::DELAY:
case Watcher::REPEAT:
\assert(\is_int($watcher->value));
$this->events[$id] = new \Event($this->handle, -1, \Event::TIMEOUT, $this->timerCallback, $watcher);
break;
case Watcher::SIGNAL:
\assert(\is_int($watcher->value));
$this->events[$id] = new \Event($this->handle, $watcher->value, \Event::SIGNAL | \Event::PERSIST, $this->signalCallback, $watcher);
break;
default:
// @codeCoverageIgnoreStart
throw new \Error("Unknown watcher type");
}
}
switch ($watcher->type) {
case Watcher::DELAY:
case Watcher::REPEAT:
\assert(\is_int($watcher->value));
$interval = \max(0, $watcher->expiration - $now);
$this->events[$id]->add($interval > 0 ? $interval / self::MILLISEC_PER_SEC : 0);
break;
case Watcher::SIGNAL:
$this->signals[$id] = $this->events[$id];
// no break
default:
/** @psalm-suppress TooFewArguments https://github.com/JetBrains/phpstorm-stubs/pull/763 */
$this->events[$id]->add();
break;
}
}
}
/**
* {@inheritdoc}
*
* @return void
*/
protected function deactivate(Watcher $watcher)
{
if (isset($this->events[$id = $watcher->id])) {
$this->events[$id]->del();
if ($watcher->type === Watcher::SIGNAL) {
unset($this->signals[$id]);
}
}
}
}

View File

@ -0,0 +1,147 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Loop\Internal;
use WP_Ultimo\Dependencies\Amp\Loop\Watcher;
/**
* Uses a binary tree stored in an array to implement a heap.
*/
final class TimerQueue
{
/** @var Watcher[] */
private $data = [];
/** @var int[] */
private $pointers = [];
/**
* @param int $node Rebuild the data array from the given node upward.
*
* @return void
*/
private function heapifyUp(int $node)
{
$entry = $this->data[$node];
while ($node !== 0 && $entry->expiration < $this->data[$parent = $node - 1 >> 1]->expiration) {
$this->swap($node, $parent);
$node = $parent;
}
}
/**
* @param int $node Rebuild the data array from the given node downward.
*
* @return void
*/
private function heapifyDown(int $node)
{
$length = \count($this->data);
while (($child = ($node << 1) + 1) < $length) {
if ($this->data[$child]->expiration < $this->data[$node]->expiration && ($child + 1 >= $length || $this->data[$child]->expiration < $this->data[$child + 1]->expiration)) {
// Left child is less than parent and right child.
$swap = $child;
} elseif ($child + 1 < $length && $this->data[$child + 1]->expiration < $this->data[$node]->expiration) {
// Right child is less than parent and left child.
$swap = $child + 1;
} else {
// Left and right child are greater than parent.
break;
}
$this->swap($node, $swap);
$node = $swap;
}
}
private function swap(int $left, int $right)
{
$temp = $this->data[$left];
$this->data[$left] = $this->data[$right];
$this->pointers[$this->data[$right]->id] = $left;
$this->data[$right] = $temp;
$this->pointers[$temp->id] = $right;
}
/**
* Inserts the watcher into the queue. Time complexity: O(log(n)).
*
* @param Watcher $watcher
*
* @psalm-param Watcher<int> $watcher
*
* @return void
*/
public function insert(Watcher $watcher)
{
\assert($watcher->expiration !== null);
\assert(!isset($this->pointers[$watcher->id]));
$node = \count($this->data);
$this->data[$node] = $watcher;
$this->pointers[$watcher->id] = $node;
$this->heapifyUp($node);
}
/**
* Removes the given watcher from the queue. Time complexity: O(log(n)).
*
* @param Watcher $watcher
*
* @psalm-param Watcher<int> $watcher
*
* @return void
*/
public function remove(Watcher $watcher)
{
$id = $watcher->id;
if (!isset($this->pointers[$id])) {
return;
}
$this->removeAndRebuild($this->pointers[$id]);
}
/**
* Deletes and returns the Watcher on top of the heap if it has expired, otherwise null is returned.
* Time complexity: O(log(n)).
*
* @param int $now Current loop time.
*
* @return Watcher|null Expired watcher at the top of the heap or null if the watcher has not expired.
*
* @psalm-return Watcher<int>|null
*/
public function extract(int $now)
{
if (empty($this->data)) {
return null;
}
$watcher = $this->data[0];
if ($watcher->expiration > $now) {
return null;
}
$this->removeAndRebuild(0);
return $watcher;
}
/**
* Returns the expiration time value at the top of the heap. Time complexity: O(1).
*
* @return int|null Expiration time of the watcher at the top of the heap or null if the heap is empty.
*/
public function peek()
{
return isset($this->data[0]) ? $this->data[0]->expiration : null;
}
/**
* @param int $node Remove the given node and then rebuild the data array.
*
* @return void
*/
private function removeAndRebuild(int $node)
{
$length = \count($this->data) - 1;
$id = $this->data[$node]->id;
$left = $this->data[$node] = $this->data[$length];
$this->pointers[$left->id] = $node;
unset($this->data[$length], $this->pointers[$id]);
if ($node < $length) {
// don't need to do anything if we removed the last element
$parent = $node - 1 >> 1;
if ($parent >= 0 && $this->data[$node]->expiration < $this->data[$parent]->expiration) {
$this->heapifyUp($node);
} else {
$this->heapifyDown($node);
}
}
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Loop;
/**
* MUST be thrown if any operation (except disable() and cancel()) is attempted with an invalid watcher identifier.
*
* An invalid watcher identifier is any identifier that is not yet emitted by the driver or cancelled by the user.
*/
class InvalidWatcherError extends \Error
{
/** @var string */
private $watcherId;
/**
* @param string $watcherId The watcher identifier.
* @param string $message The exception message.
*/
public function __construct(string $watcherId, string $message)
{
$this->watcherId = $watcherId;
parent::__construct($message);
}
/**
* @return string The watcher identifier.
*/
public function getWatcherId()
{
return $this->watcherId;
}
}

View File

@ -0,0 +1,365 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Loop;
use WP_Ultimo\Dependencies\Amp\CallableMaker;
use WP_Ultimo\Dependencies\Amp\Coroutine;
use WP_Ultimo\Dependencies\Amp\Promise;
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
use function WP_Ultimo\Dependencies\Amp\Internal\getCurrentTime;
use function WP_Ultimo\Dependencies\Amp\Promise\rethrow;
class NativeDriver extends Driver
{
use CallableMaker;
/** @var resource[] */
private $readStreams = [];
/** @var Watcher[][] */
private $readWatchers = [];
/** @var resource[] */
private $writeStreams = [];
/** @var Watcher[][] */
private $writeWatchers = [];
/** @var Internal\TimerQueue */
private $timerQueue;
/** @var Watcher[][] */
private $signalWatchers = [];
/** @var int Internal timestamp for now. */
private $now;
/** @var int Loop time offset */
private $nowOffset;
/** @var bool */
private $signalHandling;
/** @var callable */
private $streamSelectErrorHandler;
/** @var bool */
private $streamSelectIgnoreResult = \false;
public function __construct()
{
$this->timerQueue = new Internal\TimerQueue();
$this->signalHandling = \extension_loaded("pcntl");
$this->nowOffset = getCurrentTime();
$this->now = \random_int(0, $this->nowOffset);
$this->nowOffset -= $this->now;
$this->streamSelectErrorHandler = function ($errno, $message) {
// Casing changed in PHP 8 from 'unable' to 'Unable'
if (\stripos($message, "stream_select(): unable to select [4]: ") === 0) {
// EINTR
$this->streamSelectIgnoreResult = \true;
return;
}
if (\strpos($message, 'FD_SETSIZE') !== \false) {
$message = \str_replace(["\r\n", "\n", "\r"], " ", $message);
$pattern = '(stream_select\\(\\): You MUST recompile PHP with a larger value of FD_SETSIZE. It is set to (\\d+), but you have descriptors numbered at least as high as (\\d+)\\.)';
if (\preg_match($pattern, $message, $match)) {
$helpLink = 'https://amphp.org/amp/event-loop/#implementations';
$message = 'You have reached the limits of stream_select(). It has a FD_SETSIZE of ' . $match[1] . ', but you have file descriptors numbered at least as high as ' . $match[2] . '. ' . "You can install one of the extensions listed on {$helpLink} to support a higher number of " . "concurrent file descriptors. If a large number of open file descriptors is unexpected, you " . "might be leaking file descriptors that aren't closed correctly.";
}
}
throw new \Exception($message, $errno);
};
}
/**
* {@inheritdoc}
*
* @throws \Amp\Loop\UnsupportedFeatureException If the pcntl extension is not available.
*/
public function onSignal(int $signo, callable $callback, $data = null) : string
{
if (!$this->signalHandling) {
throw new UnsupportedFeatureException("Signal handling requires the pcntl extension");
}
return parent::onSignal($signo, $callback, $data);
}
/**
* {@inheritdoc}
*/
public function now() : int
{
$this->now = getCurrentTime() - $this->nowOffset;
return $this->now;
}
/**
* {@inheritdoc}
*/
public function getHandle()
{
return null;
}
/**
* @param bool $blocking
*
* @return void
*
* @throws \Throwable
*/
protected function dispatch(bool $blocking)
{
$this->selectStreams($this->readStreams, $this->writeStreams, $blocking ? $this->getTimeout() : 0);
$now = $this->now();
while ($watcher = $this->timerQueue->extract($now)) {
if ($watcher->type & Watcher::REPEAT) {
$watcher->enabled = \false;
// Trick base class into adding to enable queue when calling enable()
$this->enable($watcher->id);
} else {
$this->cancel($watcher->id);
}
try {
// Execute the timer.
$result = ($watcher->callback)($watcher->id, $watcher->data);
if ($result === null) {
continue;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
}
if ($this->signalHandling) {
\pcntl_signal_dispatch();
}
}
/**
* {@inheritdoc}
*
* @return void
*/
protected function activate(array $watchers)
{
foreach ($watchers as $watcher) {
switch ($watcher->type) {
case Watcher::READABLE:
\assert(\is_resource($watcher->value));
$streamId = (int) $watcher->value;
$this->readWatchers[$streamId][$watcher->id] = $watcher;
$this->readStreams[$streamId] = $watcher->value;
break;
case Watcher::WRITABLE:
\assert(\is_resource($watcher->value));
$streamId = (int) $watcher->value;
$this->writeWatchers[$streamId][$watcher->id] = $watcher;
$this->writeStreams[$streamId] = $watcher->value;
break;
case Watcher::DELAY:
case Watcher::REPEAT:
\assert(\is_int($watcher->value));
$this->timerQueue->insert($watcher);
break;
case Watcher::SIGNAL:
\assert(\is_int($watcher->value));
if (!isset($this->signalWatchers[$watcher->value])) {
if (!@\pcntl_signal($watcher->value, $this->callableFromInstanceMethod('handleSignal'))) {
$message = "Failed to register signal handler";
if ($error = \error_get_last()) {
$message .= \sprintf("; Errno: %d; %s", $error["type"], $error["message"]);
}
throw new \Error($message);
}
}
$this->signalWatchers[$watcher->value][$watcher->id] = $watcher;
break;
default:
// @codeCoverageIgnoreStart
throw new \Error("Unknown watcher type");
}
}
}
/**
* {@inheritdoc}
*
* @return void
*/
protected function deactivate(Watcher $watcher)
{
switch ($watcher->type) {
case Watcher::READABLE:
$streamId = (int) $watcher->value;
unset($this->readWatchers[$streamId][$watcher->id]);
if (empty($this->readWatchers[$streamId])) {
unset($this->readWatchers[$streamId], $this->readStreams[$streamId]);
}
break;
case Watcher::WRITABLE:
$streamId = (int) $watcher->value;
unset($this->writeWatchers[$streamId][$watcher->id]);
if (empty($this->writeWatchers[$streamId])) {
unset($this->writeWatchers[$streamId], $this->writeStreams[$streamId]);
}
break;
case Watcher::DELAY:
case Watcher::REPEAT:
$this->timerQueue->remove($watcher);
break;
case Watcher::SIGNAL:
\assert(\is_int($watcher->value));
if (isset($this->signalWatchers[$watcher->value])) {
unset($this->signalWatchers[$watcher->value][$watcher->id]);
if (empty($this->signalWatchers[$watcher->value])) {
unset($this->signalWatchers[$watcher->value]);
@\pcntl_signal($watcher->value, \SIG_DFL);
}
}
break;
default:
// @codeCoverageIgnoreStart
throw new \Error("Unknown watcher type");
}
}
/**
* @param resource[] $read
* @param resource[] $write
* @param int $timeout
*
* @return void
*/
private function selectStreams(array $read, array $write, int $timeout)
{
$timeout /= self::MILLISEC_PER_SEC;
if (!empty($read) || !empty($write)) {
// Use stream_select() if there are any streams in the loop.
if ($timeout >= 0) {
$seconds = (int) $timeout;
$microseconds = (int) (($timeout - $seconds) * self::MICROSEC_PER_SEC);
} else {
$seconds = null;
$microseconds = null;
}
// Failed connection attempts are indicated via except on Windows
// @link https://github.com/reactphp/event-loop/blob/8bd064ce23c26c4decf186c2a5a818c9a8209eb0/src/StreamSelectLoop.php#L279-L287
// @link https://docs.microsoft.com/de-de/windows/win32/api/winsock2/nf-winsock2-select
$except = null;
if (\DIRECTORY_SEPARATOR === '\\') {
$except = $write;
}
\set_error_handler($this->streamSelectErrorHandler);
try {
$result = \stream_select($read, $write, $except, $seconds, $microseconds);
} finally {
\restore_error_handler();
}
if ($this->streamSelectIgnoreResult || $result === 0) {
$this->streamSelectIgnoreResult = \false;
return;
}
if (!$result) {
$this->error(new \Exception('Unknown error during stream_select'));
return;
}
foreach ($read as $stream) {
$streamId = (int) $stream;
if (!isset($this->readWatchers[$streamId])) {
continue;
// All read watchers disabled.
}
foreach ($this->readWatchers[$streamId] as $watcher) {
if (!isset($this->readWatchers[$streamId][$watcher->id])) {
continue;
// Watcher disabled by another IO watcher.
}
try {
$result = ($watcher->callback)($watcher->id, $stream, $watcher->data);
if ($result === null) {
continue;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
}
}
\assert(\is_array($write));
// See https://github.com/vimeo/psalm/issues/3036
if ($except) {
foreach ($except as $key => $socket) {
$write[$key] = $socket;
}
}
foreach ($write as $stream) {
$streamId = (int) $stream;
if (!isset($this->writeWatchers[$streamId])) {
continue;
// All write watchers disabled.
}
foreach ($this->writeWatchers[$streamId] as $watcher) {
if (!isset($this->writeWatchers[$streamId][$watcher->id])) {
continue;
// Watcher disabled by another IO watcher.
}
try {
$result = ($watcher->callback)($watcher->id, $stream, $watcher->data);
if ($result === null) {
continue;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
}
}
return;
}
if ($timeout < 0) {
// Only signal watchers are enabled, so sleep indefinitely.
\usleep(\PHP_INT_MAX);
return;
}
if ($timeout > 0) {
// Sleep until next timer expires.
\usleep((int) ($timeout * self::MICROSEC_PER_SEC));
}
}
/**
* @return int Milliseconds until next timer expires or -1 if there are no pending times.
*/
private function getTimeout() : int
{
$expiration = $this->timerQueue->peek();
if ($expiration === null) {
return -1;
}
$expiration -= getCurrentTime() - $this->nowOffset;
return $expiration > 0 ? $expiration : 0;
}
/**
* @param int $signo
*
* @return void
*/
private function handleSignal(int $signo)
{
foreach ($this->signalWatchers[$signo] as $watcher) {
if (!isset($this->signalWatchers[$signo][$watcher->id])) {
continue;
}
try {
$result = ($watcher->callback)($watcher->id, $signo, $watcher->data);
if ($result === null) {
continue;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
}
}
}

View File

@ -0,0 +1,197 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Loop;
use function WP_Ultimo\Dependencies\Amp\Internal\formatStacktrace;
final class TracingDriver extends Driver
{
/** @var Driver */
private $driver;
/** @var true[] */
private $enabledWatchers = [];
/** @var true[] */
private $unreferencedWatchers = [];
/** @var string[] */
private $creationTraces = [];
/** @var string[] */
private $cancelTraces = [];
public function __construct(Driver $driver)
{
$this->driver = $driver;
}
public function run()
{
$this->driver->run();
}
public function stop()
{
$this->driver->stop();
}
public function defer(callable $callback, $data = null) : string
{
$id = $this->driver->defer(function (...$args) use($callback) {
$this->cancel($args[0]);
return $callback(...$args);
}, $data);
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
$this->enabledWatchers[$id] = \true;
return $id;
}
public function delay(int $delay, callable $callback, $data = null) : string
{
$id = $this->driver->delay($delay, function (...$args) use($callback) {
$this->cancel($args[0]);
return $callback(...$args);
}, $data);
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
$this->enabledWatchers[$id] = \true;
return $id;
}
public function repeat(int $interval, callable $callback, $data = null) : string
{
$id = $this->driver->repeat($interval, $callback, $data);
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
$this->enabledWatchers[$id] = \true;
return $id;
}
public function onReadable($stream, callable $callback, $data = null) : string
{
$id = $this->driver->onReadable($stream, $callback, $data);
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
$this->enabledWatchers[$id] = \true;
return $id;
}
public function onWritable($stream, callable $callback, $data = null) : string
{
$id = $this->driver->onWritable($stream, $callback, $data);
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
$this->enabledWatchers[$id] = \true;
return $id;
}
public function onSignal(int $signo, callable $callback, $data = null) : string
{
$id = $this->driver->onSignal($signo, $callback, $data);
$this->creationTraces[$id] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
$this->enabledWatchers[$id] = \true;
return $id;
}
public function enable(string $watcherId)
{
try {
$this->driver->enable($watcherId);
$this->enabledWatchers[$watcherId] = \true;
} catch (InvalidWatcherError $e) {
throw new InvalidWatcherError($watcherId, $e->getMessage() . "\r\n\r\n" . $this->getTraces($watcherId));
}
}
public function cancel(string $watcherId)
{
$this->driver->cancel($watcherId);
if (!isset($this->cancelTraces[$watcherId])) {
$this->cancelTraces[$watcherId] = formatStacktrace(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS));
}
unset($this->enabledWatchers[$watcherId], $this->unreferencedWatchers[$watcherId]);
}
public function disable(string $watcherId)
{
$this->driver->disable($watcherId);
unset($this->enabledWatchers[$watcherId]);
}
public function reference(string $watcherId)
{
try {
$this->driver->reference($watcherId);
unset($this->unreferencedWatchers[$watcherId]);
} catch (InvalidWatcherError $e) {
throw new InvalidWatcherError($watcherId, $e->getMessage() . "\r\n\r\n" . $this->getTraces($watcherId));
}
}
public function unreference(string $watcherId)
{
$this->driver->unreference($watcherId);
$this->unreferencedWatchers[$watcherId] = \true;
}
public function setErrorHandler(callable $callback = null)
{
return $this->driver->setErrorHandler($callback);
}
/** @inheritdoc */
public function getHandle()
{
$this->driver->getHandle();
}
public function dump() : string
{
$dump = "Enabled, referenced watchers keeping the loop running: ";
foreach ($this->enabledWatchers as $watcher => $_) {
if (isset($this->unreferencedWatchers[$watcher])) {
continue;
}
$dump .= "Watcher ID: " . $watcher . "\r\n";
$dump .= $this->getCreationTrace($watcher);
$dump .= "\r\n\r\n";
}
return \rtrim($dump);
}
public function getInfo() : array
{
return $this->driver->getInfo();
}
public function __debugInfo()
{
return $this->driver->__debugInfo();
}
public function now() : int
{
return $this->driver->now();
}
protected function error(\Throwable $exception)
{
$this->driver->error($exception);
}
/**
* @inheritdoc
*
* @return void
*/
protected function activate(array $watchers)
{
// nothing to do in a decorator
}
/**
* @inheritdoc
*
* @return void
*/
protected function dispatch(bool $blocking)
{
// nothing to do in a decorator
}
/**
* @inheritdoc
*
* @return void
*/
protected function deactivate(Watcher $watcher)
{
// nothing to do in a decorator
}
private function getTraces(string $watcherId) : string
{
return "Creation Trace:\r\n" . $this->getCreationTrace($watcherId) . "\r\n\r\n" . "Cancellation Trace:\r\n" . $this->getCancelTrace($watcherId);
}
private function getCreationTrace(string $watcher) : string
{
if (!isset($this->creationTraces[$watcher])) {
return 'No creation trace, yet.';
}
return $this->creationTraces[$watcher];
}
private function getCancelTrace(string $watcher) : string
{
if (!isset($this->cancelTraces[$watcher])) {
return 'No cancellation trace, yet.';
}
return $this->cancelTraces[$watcher];
}
}

View File

@ -0,0 +1,12 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Loop;
/**
* MUST be thrown if a feature is not supported by the system.
*
* This might happen if ext-pcntl is missing and the loop driver doesn't support another way to dispatch signals.
*/
class UnsupportedFeatureException extends \Exception
{
}

View File

@ -0,0 +1,285 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Loop;
use WP_Ultimo\Dependencies\Amp\Coroutine;
use WP_Ultimo\Dependencies\Amp\Promise;
use WP_Ultimo\Dependencies\React\Promise\PromiseInterface as ReactPromise;
use function WP_Ultimo\Dependencies\Amp\Promise\rethrow;
class UvDriver extends Driver
{
/** @var resource A uv_loop resource created with uv_loop_new() */
private $handle;
/** @var resource[] */
private $events = [];
/** @var Watcher[][] */
private $watchers = [];
/** @var resource[] */
private $streams = [];
/** @var callable */
private $ioCallback;
/** @var callable */
private $timerCallback;
/** @var callable */
private $signalCallback;
public function __construct()
{
$this->handle = \uv_loop_new();
/**
* @param $event
* @param $status
* @param $events
* @param $resource
*
* @return void
*/
$this->ioCallback = function ($event, $status, $events, $resource) {
$watchers = $this->watchers[(int) $event];
switch ($status) {
case 0:
// OK
break;
default:
// Invoke the callback on errors, as this matches behavior with other loop back-ends.
// Re-enable watcher as libuv disables the watcher on non-zero status.
$flags = 0;
foreach ($watchers as $watcher) {
$flags |= $watcher->enabled ? $watcher->type : 0;
}
\uv_poll_start($event, $flags, $this->ioCallback);
break;
}
foreach ($watchers as $watcher) {
// $events is OR'ed with 4 to trigger watcher if no events are indicated (0) or on UV_DISCONNECT (4).
// http://docs.libuv.org/en/v1.x/poll.html
if (!($watcher->enabled && ($watcher->type & $events || ($events | 4) === 4))) {
continue;
}
try {
$result = ($watcher->callback)($watcher->id, $resource, $watcher->data);
if ($result === null) {
continue;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
}
};
/**
* @param $event
*
* @return void
*/
$this->timerCallback = function ($event) {
$watcher = $this->watchers[(int) $event][0];
if ($watcher->type & Watcher::DELAY) {
unset($this->events[$watcher->id], $this->watchers[(int) $event]);
// Avoid call to uv_is_active().
$this->cancel($watcher->id);
// Remove reference to watcher in parent.
} elseif ($watcher->value === 0) {
// Disable and re-enable so it's not executed repeatedly in the same tick
// See https://github.com/amphp/amp/issues/131
$this->disable($watcher->id);
$this->enable($watcher->id);
}
try {
$result = ($watcher->callback)($watcher->id, $watcher->data);
if ($result === null) {
return;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
};
/**
* @param $event
* @param $signo
*
* @return void
*/
$this->signalCallback = function ($event, $signo) {
$watcher = $this->watchers[(int) $event][0];
try {
$result = ($watcher->callback)($watcher->id, $signo, $watcher->data);
if ($result === null) {
return;
}
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
};
}
/**
* {@inheritdoc}
*/
public function cancel(string $watcherId)
{
parent::cancel($watcherId);
if (!isset($this->events[$watcherId])) {
return;
}
$event = $this->events[$watcherId];
$eventId = (int) $event;
if (isset($this->watchers[$eventId][0])) {
// All except IO watchers.
unset($this->watchers[$eventId]);
} elseif (isset($this->watchers[$eventId][$watcherId])) {
$watcher = $this->watchers[$eventId][$watcherId];
unset($this->watchers[$eventId][$watcherId]);
if (empty($this->watchers[$eventId])) {
unset($this->watchers[$eventId], $this->streams[(int) $watcher->value]);
}
}
unset($this->events[$watcherId]);
}
public static function isSupported() : bool
{
return \extension_loaded("uv");
}
/**
* {@inheritdoc}
*/
public function now() : int
{
\uv_update_time($this->handle);
/** @psalm-suppress TooManyArguments */
return \uv_now($this->handle);
}
/**
* {@inheritdoc}
*/
public function getHandle()
{
return $this->handle;
}
/**
* {@inheritdoc}
*
* @return void
*/
protected function dispatch(bool $blocking)
{
/** @psalm-suppress TooManyArguments */
\uv_run($this->handle, $blocking ? \UV::RUN_ONCE : \UV::RUN_NOWAIT);
}
/**
* {@inheritdoc}
*
* @return void
*/
protected function activate(array $watchers)
{
$now = $this->now();
foreach ($watchers as $watcher) {
$id = $watcher->id;
switch ($watcher->type) {
case Watcher::READABLE:
case Watcher::WRITABLE:
\assert(\is_resource($watcher->value));
$streamId = (int) $watcher->value;
if (isset($this->streams[$streamId])) {
$event = $this->streams[$streamId];
} elseif (isset($this->events[$id])) {
$event = $this->streams[$streamId] = $this->events[$id];
} else {
/** @psalm-suppress UndefinedFunction */
$event = $this->streams[$streamId] = \WP_Ultimo\Dependencies\uv_poll_init_socket($this->handle, $watcher->value);
}
$eventId = (int) $event;
$this->events[$id] = $event;
$this->watchers[$eventId][$id] = $watcher;
$flags = 0;
foreach ($this->watchers[$eventId] as $w) {
$flags |= $w->enabled ? $w->type : 0;
}
\uv_poll_start($event, $flags, $this->ioCallback);
break;
case Watcher::DELAY:
case Watcher::REPEAT:
\assert(\is_int($watcher->value));
if (isset($this->events[$id])) {
$event = $this->events[$id];
} else {
$event = $this->events[$id] = \uv_timer_init($this->handle);
}
$this->watchers[(int) $event] = [$watcher];
\uv_timer_start($event, \max(0, $watcher->expiration - $now), $watcher->type & Watcher::REPEAT ? $watcher->value : 0, $this->timerCallback);
break;
case Watcher::SIGNAL:
\assert(\is_int($watcher->value));
if (isset($this->events[$id])) {
$event = $this->events[$id];
} else {
/** @psalm-suppress UndefinedFunction */
$event = $this->events[$id] = \WP_Ultimo\Dependencies\uv_signal_init($this->handle);
}
$this->watchers[(int) $event] = [$watcher];
/** @psalm-suppress UndefinedFunction */
\WP_Ultimo\Dependencies\uv_signal_start($event, $this->signalCallback, $watcher->value);
break;
default:
// @codeCoverageIgnoreStart
throw new \Error("Unknown watcher type");
}
}
}
/**
* {@inheritdoc}
*
* @return void
*/
protected function deactivate(Watcher $watcher)
{
$id = $watcher->id;
if (!isset($this->events[$id])) {
return;
}
$event = $this->events[$id];
if (!\uv_is_active($event)) {
return;
}
switch ($watcher->type) {
case Watcher::READABLE:
case Watcher::WRITABLE:
$flags = 0;
foreach ($this->watchers[(int) $event] as $w) {
$flags |= $w->enabled ? $w->type : 0;
}
if ($flags) {
\uv_poll_start($event, $flags, $this->ioCallback);
} else {
\uv_poll_stop($event);
}
break;
case Watcher::DELAY:
case Watcher::REPEAT:
\uv_timer_stop($event);
break;
case Watcher::SIGNAL:
\uv_signal_stop($event);
break;
default:
// @codeCoverageIgnoreStart
throw new \Error("Unknown watcher type");
}
}
}

View File

@ -0,0 +1,47 @@
<?php
namespace WP_Ultimo\Dependencies\Amp\Loop;
use WP_Ultimo\Dependencies\Amp\Struct;
/**
* @template TValue as (int|resource|null)
*
* @psalm-suppress MissingConstructor
*/
class Watcher
{
use Struct;
const IO = 0b11;
const READABLE = 0b1;
const WRITABLE = 0b10;
const DEFER = 0b100;
const TIMER = 0b11000;
const DELAY = 0b1000;
const REPEAT = 0b10000;
const SIGNAL = 0b100000;
/** @var int */
public $type;
/** @var bool */
public $enabled = \true;
/** @var bool */
public $referenced = \true;
/** @var string */
public $id;
/** @var callable */
public $callback;
/**
* Data provided to the watcher callback.
*
* @var mixed
*/
public $data;
/**
* Watcher-dependent value storage. Stream for IO watchers, signal number for signal watchers, interval for timers.
*
* @var resource|int|null
* @psalm-var TValue
*/
public $value;
/** @var int|null */
public $expiration;
}