1
0
mirror of https://github.com/danog/amp.git synced 2024-12-12 09:29:45 +01:00
amp/lib/Loop/EvDriver.php

290 lines
7.7 KiB
PHP
Raw Normal View History

<?php
namespace Amp\Loop;
use Amp\Coroutine;
use Amp\Promise;
use React\Promise\PromiseInterface as ReactPromise;
use function Amp\Promise\rethrow;
2018-06-18 20:00:01 +02:00
class EvDriver extends Driver
{
/** @var \EvSignal[]|null */
private static $activeSignals;
2018-01-06 03:32:57 +01:00
2017-02-17 05:36:32 +01:00
/** @var \EvLoop */
private $handle;
2018-01-06 03:32:57 +01:00
2017-02-17 05:36:32 +01:00
/** @var \EvWatcher[] */
private $events = [];
2018-01-06 03:32:57 +01:00
2017-02-17 05:36:32 +01:00
/** @var callable */
private $ioCallback;
2018-01-06 03:32:57 +01:00
2017-02-17 05:36:32 +01:00
/** @var callable */
private $timerCallback;
2018-01-06 03:32:57 +01:00
2017-02-17 05:36:32 +01:00
/** @var callable */
private $signalCallback;
2018-01-06 03:32:57 +01:00
2017-02-17 05:36:32 +01:00
/** @var \EvSignal[] */
private $signals = [];
2017-03-10 19:19:32 +01:00
2018-01-06 03:32:57 +01:00
/** @var int Internal timestamp for now. */
2018-11-26 17:33:03 +01:00
private $now;
2018-01-06 03:32:57 +01:00
/** @var bool */
private $nowUpdateNeeded = false;
/** @var int Loop time offset from microtime() */
private $nowOffset;
2018-06-18 20:00:01 +02:00
public function __construct()
{
$this->handle = new \EvLoop;
2018-01-06 03:32:57 +01:00
$this->nowOffset = (int) (\microtime(true) * self::MILLISEC_PER_SEC);
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
if (self::$activeSignals === null) {
self::$activeSignals = &$this->signals;
}
2016-06-08 19:08:00 +02:00
$this->ioCallback = function (\EvIO $event) {
2017-04-18 16:38:16 +02:00
/** @var \Amp\Loop\Watcher $watcher */
$watcher = $event->data;
try {
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
if ($result === null) {
return;
}
2017-03-12 18:03:13 +01:00
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
};
$this->timerCallback = function (\EvTimer $event) {
2017-04-18 16:38:16 +02:00
/** @var \Amp\Loop\Watcher $watcher */
$watcher = $event->data;
if ($watcher->type & Watcher::DELAY) {
$this->cancel($watcher->id);
2017-05-23 20:17:34 +02:00
} 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;
}
2017-03-12 18:03:13 +01:00
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
};
$this->signalCallback = function (\EvSignal $event) {
2017-04-18 16:38:16 +02:00
/** @var \Amp\Loop\Watcher $watcher */
$watcher = $event->data;
try {
$result = ($watcher->callback)($watcher->id, $watcher->value, $watcher->data);
if ($result === null) {
return;
}
2017-03-12 18:03:13 +01:00
if ($result instanceof \Generator) {
$result = new Coroutine($result);
}
if ($result instanceof Promise || $result instanceof ReactPromise) {
rethrow($result);
}
} catch (\Throwable $exception) {
$this->error($exception);
}
};
}
/**
* {@inheritdoc}
*/
2018-06-18 20:00:01 +02:00
public function cancel(string $watcherId)
{
parent::cancel($watcherId);
unset($this->events[$watcherId]);
}
2018-06-18 20:00:01 +02:00
public static function isSupported(): bool
{
return \extension_loaded("ev");
}
2018-06-18 20:00:01 +02:00
public function __destruct()
{
foreach ($this->events as $event) {
2018-03-20 15:53:55 +01:00
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}
*/
2018-06-18 20:00:01 +02:00
public function run()
{
$active = self::$activeSignals;
2017-03-10 19:19:32 +01:00
foreach ($active as $event) {
$event->stop();
}
2017-03-10 19:19:32 +01:00
self::$activeSignals = &$this->signals;
2017-03-10 19:19:32 +01:00
foreach ($this->signals as $event) {
$event->start();
}
2017-03-10 19:19:32 +01:00
try {
parent::run();
} finally {
foreach ($this->signals as $event) {
$event->stop();
}
2017-03-10 19:19:32 +01:00
self::$activeSignals = &$active;
2017-03-10 19:19:32 +01:00
foreach ($active as $event) {
$event->start();
}
}
}
/**
* {@inheritdoc}
*/
2018-06-18 20:00:01 +02:00
public function stop()
{
$this->handle->stop();
parent::stop();
}
2018-01-06 03:32:57 +01:00
/**
* {@inheritdoc}
*/
public function now(): int
{
if ($this->nowUpdateNeeded) {
$this->now = (int) (\microtime(true) * self::MILLISEC_PER_SEC) - $this->nowOffset;
$this->nowUpdateNeeded = false;
}
return $this->now;
}
/**
* {@inheritdoc}
*/
2018-06-18 20:00:01 +02:00
public function getHandle(): \EvLoop
{
return $this->handle;
}
2016-06-08 19:08:00 +02:00
/**
* {@inheritdoc}
*/
2018-06-18 20:00:01 +02:00
protected function dispatch(bool $blocking)
{
2018-01-06 03:32:57 +01:00
$this->nowUpdateNeeded = true;
$this->handle->run($blocking ? \Ev::RUN_ONCE : \Ev::RUN_ONCE | \Ev::RUN_NOWAIT);
}
/**
* {@inheritdoc}
*/
2018-06-18 20:00:01 +02:00
protected function activate(array $watchers)
{
foreach ($watchers as $watcher) {
if (!isset($this->events[$id = $watcher->id])) {
switch ($watcher->type) {
case Watcher::READABLE:
$this->events[$id] = $this->handle->io($watcher->value, \Ev::READ, $this->ioCallback, $watcher);
break;
case Watcher::WRITABLE:
$this->events[$id] = $this->handle->io($watcher->value, \Ev::WRITE, $this->ioCallback, $watcher);
break;
case Watcher::DELAY:
case Watcher::REPEAT:
$interval = $watcher->value / self::MILLISEC_PER_SEC;
$this->events[$id] = $this->handle->timer(
$interval,
2016-06-08 19:08:00 +02:00
$watcher->type & Watcher::REPEAT ? $interval : 0,
$this->timerCallback,
$watcher
);
break;
case Watcher::SIGNAL:
$this->events[$id] = $this->handle->signal($watcher->value, $this->signalCallback, $watcher);
break;
default:
// @codeCoverageIgnoreStart
throw new \Error("Unknown watcher type");
2018-06-18 20:00:01 +02:00
// @codeCoverageIgnoreEnd
}
} else {
$this->events[$id]->start();
}
if ($watcher->type === Watcher::SIGNAL) {
$this->signals[$id] = $this->events[$id];
}
}
}
/**
* {@inheritdoc}
*/
2018-06-18 20:00:01 +02:00
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]);
}
}
}
}