From a562d6b20fca383ebacc4a2b6c42a8fff66cf325 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Fri, 21 Apr 2017 10:54:53 -0500 Subject: [PATCH] Use same poll for read and write watchers Changed values of Watcher constants to match UV constants because it was easier that way. :-D --- lib/Loop/UvDriver.php | 64 +++++++++++++++---------------------------- lib/Loop/Watcher.php | 14 +++++----- 2 files changed, 29 insertions(+), 49 deletions(-) diff --git a/lib/Loop/UvDriver.php b/lib/Loop/UvDriver.php index 7f794fe..1be8001 100644 --- a/lib/Loop/UvDriver.php +++ b/lib/Loop/UvDriver.php @@ -18,10 +18,7 @@ class UvDriver extends Driver { private $watchers = []; /** @var resource[] */ - private $read = []; - - /** @var resource[] */ - private $write = []; + private $io = []; /** @var callable */ private $ioCallback; @@ -60,6 +57,10 @@ class UvDriver extends Driver { } foreach ($watchers as $watcher) { + if (!($watcher->type & $events)) { + continue; + } + try { $result = ($watcher->callback)($watcher->id, $resource, $watcher->data); @@ -175,42 +176,26 @@ class UvDriver extends Driver { switch ($watcher->type) { case Watcher::READABLE: - $streamId = (int) $watcher->value; - - if (isset($this->read[$streamId])) { - $event = $this->read[$streamId]; - } elseif (isset($this->events[$id])) { - $event = $this->read[$streamId] = $this->events[$id]; - } else { - $event = $this->read[$streamId] = \uv_poll_init_socket($this->handle, $watcher->value); - } - - $this->events[$id] = $event; - $this->watchers[(int) $event][$id] = $watcher; - - if (!\uv_is_active($event)) { - \uv_poll_start($event, \UV::READABLE, $this->ioCallback); - } - break; - case Watcher::WRITABLE: $streamId = (int) $watcher->value; - if (isset($this->write[$streamId])) { - $event = $this->write[$streamId]; + if (isset($this->io[$streamId])) { + $event = $this->io[$streamId]; } elseif (isset($this->events[$id])) { - $event = $this->write[$streamId] = $this->events[$id]; + $event = $this->io[$streamId] = $this->events[$id]; } else { - $event = $this->write[$streamId] = \uv_poll_init_socket($this->handle, $watcher->value); + $event = $this->io[$streamId] = \uv_poll_init_socket($this->handle, $watcher->value); } + $eventId = (int) $event; $this->events[$id] = $event; - $this->watchers[(int) $event][$id] = $watcher; + $this->watchers[$eventId][$id] = $watcher; - - if (!\uv_is_active($event)) { - \uv_poll_start($event, \UV::WRITABLE, $this->ioCallback); + $flags = 0; + foreach ($this->watchers[$eventId] as $watcher) { + $flags |= $watcher->type; } + \uv_poll_start($event, $flags, $this->ioCallback); break; case Watcher::DELAY: @@ -266,26 +251,21 @@ class UvDriver extends Driver { switch ($watcher->type) { case Watcher::READABLE: - unset($this->watchers[$eventId][$id]); - - if (empty($this->watchers[$eventId])) { - unset($this->watchers[$eventId]); - unset($this->read[(int) $watcher->value]); - if (\uv_is_active($event)) { - \uv_poll_stop($event); - } - } - break; - case Watcher::WRITABLE: unset($this->watchers[$eventId][$id]); if (empty($this->watchers[$eventId])) { unset($this->watchers[$eventId]); - unset($this->write[(int) $watcher->value]); + unset($this->io[(int) $watcher->value]); if (\uv_is_active($event)) { \uv_poll_stop($event); } + } else { + $flags = 0; + foreach ($this->watchers[$eventId] as $watcher) { + $flags |= $watcher->type; + } + \uv_poll_start($event, $flags, $this->ioCallback); } break; diff --git a/lib/Loop/Watcher.php b/lib/Loop/Watcher.php index 736d222..dd9553e 100644 --- a/lib/Loop/Watcher.php +++ b/lib/Loop/Watcher.php @@ -7,13 +7,13 @@ use Amp\Struct; class Watcher { use Struct; - const DEFER = 0b00000001; - const TIMER = 0b00000110; - const DELAY = 0b00000010; - const REPEAT = 0b00000100; - const IO = 0b00011000; - const READABLE = 0b00001000; - const WRITABLE = 0b00010000; + const IO = 0b00000011; + const READABLE = 0b00000001; + const WRITABLE = 0b00000010; + const DEFER = 0b00000100; + const TIMER = 0b00011000; + const DELAY = 0b00001000; + const REPEAT = 0b00010000; const SIGNAL = 0b00100000; /** @var int */