2020-03-28 12:23:46 +01:00
|
|
|
<?php /** @noinspection PhpComposerExtensionStubsInspection */
|
2016-06-08 16:12:42 +02:00
|
|
|
|
|
|
|
namespace Amp\Loop;
|
|
|
|
|
2017-03-10 23:03:41 +01:00
|
|
|
use Amp\Coroutine;
|
|
|
|
use Amp\Promise;
|
2017-03-11 07:13:03 +01:00
|
|
|
use React\Promise\PromiseInterface as ReactPromise;
|
2018-12-10 20:07:11 +01:00
|
|
|
use function Amp\Internal\getCurrentTime;
|
2017-03-15 17:12:49 +01:00
|
|
|
use function Amp\Promise\rethrow;
|
2016-06-08 16:12:42 +02:00
|
|
|
|
2020-10-09 19:37:37 +02:00
|
|
|
final class EvDriver extends Driver
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2017-03-10 21:58:46 +01:00
|
|
|
/** @var \EvSignal[]|null */
|
2020-09-25 05:17:13 +02:00
|
|
|
private static ?array $activeSignals = null;
|
2018-01-06 03:32:57 +01:00
|
|
|
|
2020-03-28 12:23:46 +01:00
|
|
|
public static function isSupported(): bool
|
|
|
|
{
|
|
|
|
return \extension_loaded("ev");
|
|
|
|
}
|
|
|
|
|
2020-09-25 05:17:13 +02:00
|
|
|
private \EvLoop $handle;
|
|
|
|
|
2017-02-17 05:36:32 +01:00
|
|
|
/** @var \EvWatcher[] */
|
2020-09-25 05:17:13 +02:00
|
|
|
private array $events = [];
|
|
|
|
|
|
|
|
private \Closure $ioCallback;
|
|
|
|
|
|
|
|
private \Closure $timerCallback;
|
|
|
|
|
|
|
|
private \Closure $signalCallback;
|
|
|
|
|
2017-02-17 05:36:32 +01:00
|
|
|
/** @var \EvSignal[] */
|
2020-09-25 05:17:13 +02:00
|
|
|
private array $signals = [];
|
|
|
|
|
2018-01-06 03:32:57 +01:00
|
|
|
/** @var int Internal timestamp for now. */
|
2020-09-25 05:17:13 +02:00
|
|
|
private int $now;
|
|
|
|
|
2018-12-10 20:07:11 +01:00
|
|
|
/** @var int Loop time offset */
|
2020-09-25 05:17:13 +02:00
|
|
|
private int $nowOffset;
|
2018-01-06 03:32:57 +01:00
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
2016-06-08 16:12:42 +02:00
|
|
|
$this->handle = new \EvLoop;
|
2018-12-10 20:07:11 +01:00
|
|
|
$this->nowOffset = getCurrentTime();
|
2018-11-26 17:33:03 +01:00
|
|
|
$this->now = \random_int(0, $this->nowOffset);
|
|
|
|
$this->nowOffset -= $this->now;
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
if (self::$activeSignals === null) {
|
|
|
|
self::$activeSignals = &$this->signals;
|
|
|
|
}
|
2016-06-08 19:08:00 +02:00
|
|
|
|
2020-03-28 22:20:44 +01:00
|
|
|
/**
|
|
|
|
* @param \EvIO $event
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-09-25 05:17:13 +02:00
|
|
|
$this->ioCallback = function (\EvIO $event): void {
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @var Watcher $watcher */
|
2016-06-08 16:12:42 +02:00
|
|
|
$watcher = $event->data;
|
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
try {
|
|
|
|
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
|
2017-03-10 23:03:41 +01:00
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
if ($result === null) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-12 18:03:13 +01:00
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
if ($result instanceof \Generator) {
|
|
|
|
$result = new Coroutine($result);
|
|
|
|
}
|
2017-03-10 23:03:41 +01:00
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
|
|
|
rethrow($result);
|
|
|
|
}
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
$this->error($exception);
|
2017-03-10 23:03:41 +01:00
|
|
|
}
|
2016-06-08 16:12:42 +02:00
|
|
|
};
|
|
|
|
|
2020-03-28 22:20:44 +01:00
|
|
|
/**
|
|
|
|
* @param \EvTimer $event
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-09-25 05:17:13 +02:00
|
|
|
$this->timerCallback = function (\EvTimer $event): void {
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @var Watcher $watcher */
|
2016-06-08 16:12:42 +02:00
|
|
|
$watcher = $event->data;
|
|
|
|
|
|
|
|
if ($watcher->type & Watcher::DELAY) {
|
|
|
|
$this->cancel($watcher->id);
|
2017-05-23 20:17:34 +02:00
|
|
|
} elseif ($watcher->value === 0) {
|
2017-05-23 19:46:23 +02:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
try {
|
|
|
|
$result = ($watcher->callback)($watcher->id, $watcher->data);
|
2017-03-10 23:03:41 +01:00
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
if ($result === null) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-12 18:03:13 +01:00
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
if ($result instanceof \Generator) {
|
|
|
|
$result = new Coroutine($result);
|
|
|
|
}
|
2017-03-10 23:03:41 +01:00
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
|
|
|
rethrow($result);
|
|
|
|
}
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
$this->error($exception);
|
2017-03-10 23:03:41 +01:00
|
|
|
}
|
2016-06-08 16:12:42 +02:00
|
|
|
};
|
|
|
|
|
2020-03-28 22:20:44 +01:00
|
|
|
/**
|
|
|
|
* @param \EvSignal $event
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-09-25 05:17:13 +02:00
|
|
|
$this->signalCallback = function (\EvSignal $event): void {
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @var Watcher $watcher */
|
2016-06-08 16:12:42 +02:00
|
|
|
$watcher = $event->data;
|
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
try {
|
2017-03-15 08:33:04 +01:00
|
|
|
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
|
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
if ($result === null) {
|
|
|
|
return;
|
|
|
|
}
|
2017-03-12 18:03:13 +01:00
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
if ($result instanceof \Generator) {
|
|
|
|
$result = new Coroutine($result);
|
|
|
|
}
|
2017-03-10 23:03:41 +01:00
|
|
|
|
2017-03-14 06:20:05 +01:00
|
|
|
if ($result instanceof Promise || $result instanceof ReactPromise) {
|
|
|
|
rethrow($result);
|
|
|
|
}
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
$this->error($exception);
|
2017-03-10 23:03:41 +01:00
|
|
|
}
|
2016-06-08 16:12:42 +02:00
|
|
|
};
|
|
|
|
}
|
2017-01-06 00:38:33 +01:00
|
|
|
|
2017-03-10 21:58:46 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2020-09-24 18:52:22 +02:00
|
|
|
public function cancel(string $watcherId): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2017-03-10 21:58:46 +01:00
|
|
|
parent::cancel($watcherId);
|
|
|
|
unset($this->events[$watcherId]);
|
|
|
|
}
|
|
|
|
|
2018-06-18 20:00:01 +02:00
|
|
|
public function __destruct()
|
|
|
|
{
|
2017-01-06 00:38:33 +01:00
|
|
|
foreach ($this->events as $event) {
|
2020-03-28 12:23:46 +01:00
|
|
|
/** @psalm-suppress all */
|
2018-03-20 15:53:55 +01:00
|
|
|
if ($event !== null) { // Events may have been nulled in extension depending on destruct order.
|
|
|
|
$event->stop();
|
|
|
|
}
|
2017-01-06 00:38:33 +01:00
|
|
|
}
|
2017-12-19 17:53:00 +01:00
|
|
|
|
|
|
|
// 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 = [];
|
2017-01-06 00:38:33 +01:00
|
|
|
}
|
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2020-09-24 18:52:22 +02:00
|
|
|
public function run(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2017-01-06 00:32:03 +01:00
|
|
|
$active = self::$activeSignals;
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2020-03-28 21:55:44 +01:00
|
|
|
\assert($active !== null);
|
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
foreach ($active as $event) {
|
|
|
|
$event->stop();
|
|
|
|
}
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
self::$activeSignals = &$this->signals;
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
foreach ($this->signals as $event) {
|
|
|
|
$event->start();
|
|
|
|
}
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
try {
|
|
|
|
parent::run();
|
|
|
|
} finally {
|
|
|
|
foreach ($this->signals as $event) {
|
|
|
|
$event->stop();
|
|
|
|
}
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
self::$activeSignals = &$active;
|
2017-03-10 19:19:32 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
foreach ($active as $event) {
|
|
|
|
$event->start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-08 16:12:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2020-09-24 18:52:22 +02:00
|
|
|
public function stop(): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-06-08 16:12:42 +02:00
|
|
|
$this->handle->stop();
|
|
|
|
parent::stop();
|
|
|
|
}
|
2016-06-08 23:03:37 +02:00
|
|
|
|
2018-01-06 03:32:57 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function now(): int
|
|
|
|
{
|
2020-07-14 21:45:35 +02:00
|
|
|
$this->now = getCurrentTime() - $this->nowOffset;
|
2018-01-06 03:32:57 +01:00
|
|
|
|
|
|
|
return $this->now;
|
|
|
|
}
|
|
|
|
|
2017-03-10 21:58:46 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-06-18 20:00:01 +02:00
|
|
|
public function getHandle(): \EvLoop
|
|
|
|
{
|
2017-03-10 21:58:46 +01:00
|
|
|
return $this->handle;
|
|
|
|
}
|
|
|
|
|
2016-06-08 19:08:00 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2020-03-28 22:20:44 +01:00
|
|
|
*
|
|
|
|
* @return void
|
2016-06-08 19:08:00 +02:00
|
|
|
*/
|
2020-09-24 18:52:22 +02:00
|
|
|
protected function dispatch(bool $blocking): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-06-08 16:12:42 +02:00
|
|
|
$this->handle->run($blocking ? \Ev::RUN_ONCE : \Ev::RUN_ONCE | \Ev::RUN_NOWAIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2020-03-28 22:20:44 +01:00
|
|
|
*
|
|
|
|
* @return void
|
2016-06-08 16:12:42 +02:00
|
|
|
*/
|
2020-09-24 18:52:22 +02:00
|
|
|
protected function activate(array $watchers): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2020-07-14 21:45:35 +02:00
|
|
|
$this->handle->nowUpdate();
|
|
|
|
$now = $this->now();
|
|
|
|
|
2016-06-08 16:12:42 +02:00
|
|
|
foreach ($watchers as $watcher) {
|
|
|
|
if (!isset($this->events[$id = $watcher->id])) {
|
|
|
|
switch ($watcher->type) {
|
|
|
|
case Watcher::READABLE:
|
2020-03-28 21:55:44 +01:00
|
|
|
\assert(\is_resource($watcher->value));
|
|
|
|
|
2016-06-08 16:12:42 +02:00
|
|
|
$this->events[$id] = $this->handle->io($watcher->value, \Ev::READ, $this->ioCallback, $watcher);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Watcher::WRITABLE:
|
2020-03-28 21:55:44 +01:00
|
|
|
\assert(\is_resource($watcher->value));
|
|
|
|
|
2020-03-28 12:23:46 +01:00
|
|
|
$this->events[$id] = $this->handle->io(
|
|
|
|
$watcher->value,
|
|
|
|
\Ev::WRITE,
|
|
|
|
$this->ioCallback,
|
|
|
|
$watcher
|
|
|
|
);
|
2016-06-08 16:12:42 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Watcher::DELAY:
|
|
|
|
case Watcher::REPEAT:
|
2020-03-28 21:55:44 +01:00
|
|
|
\assert(\is_int($watcher->value));
|
|
|
|
|
2016-06-08 16:12:42 +02:00
|
|
|
$interval = $watcher->value / self::MILLISEC_PER_SEC;
|
|
|
|
$this->events[$id] = $this->handle->timer(
|
2020-07-14 21:45:35 +02:00
|
|
|
\max(0, ($watcher->expiration - $now) / self::MILLISEC_PER_SEC),
|
2020-03-28 12:23:46 +01:00
|
|
|
($watcher->type & Watcher::REPEAT) ? $interval : 0,
|
2016-06-08 16:12:42 +02:00
|
|
|
$this->timerCallback,
|
|
|
|
$watcher
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Watcher::SIGNAL:
|
2020-03-28 21:55:44 +01:00
|
|
|
\assert(\is_int($watcher->value));
|
|
|
|
|
2016-06-08 16:12:42 +02:00
|
|
|
$this->events[$id] = $this->handle->signal($watcher->value, $this->signalCallback, $watcher);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2017-03-14 17:44:19 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
throw new \Error("Unknown watcher type");
|
2018-06-18 20:00:01 +02:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-06-08 16:12:42 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->events[$id]->start();
|
|
|
|
}
|
2017-01-06 00:32:03 +01:00
|
|
|
|
|
|
|
if ($watcher->type === Watcher::SIGNAL) {
|
2020-03-28 21:55:44 +01:00
|
|
|
/** @psalm-suppress PropertyTypeCoercion */
|
2017-01-06 00:32:03 +01:00
|
|
|
$this->signals[$id] = $this->events[$id];
|
|
|
|
}
|
2016-06-08 16:12:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2020-03-28 22:20:44 +01:00
|
|
|
*
|
|
|
|
* @return void
|
2016-06-08 16:12:42 +02:00
|
|
|
*/
|
2020-09-24 18:52:22 +02:00
|
|
|
protected function deactivate(Watcher $watcher): void
|
2018-06-18 20:00:01 +02:00
|
|
|
{
|
2016-06-08 16:12:42 +02:00
|
|
|
if (isset($this->events[$id = $watcher->id])) {
|
|
|
|
$this->events[$id]->stop();
|
2017-12-19 17:53:00 +01:00
|
|
|
|
2017-01-06 00:32:03 +01:00
|
|
|
if ($watcher->type === Watcher::SIGNAL) {
|
|
|
|
unset($this->signals[$id]);
|
|
|
|
}
|
2016-06-08 16:12:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|