From 5d53c66e7a35fd6bf7670fe2b79c2f0f89f6ed11 Mon Sep 17 00:00:00 2001 From: Aaron Stephens Date: Wed, 12 Jun 2013 09:34:18 +0100 Subject: [PATCH] [EventLoop] Changed StreamSelectLoop to use non-blocking behavior on tick() --- src/React/EventLoop/StreamSelectLoop.php | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/React/EventLoop/StreamSelectLoop.php b/src/React/EventLoop/StreamSelectLoop.php index 4c9f1262..62cb63be 100644 --- a/src/React/EventLoop/StreamSelectLoop.php +++ b/src/React/EventLoop/StreamSelectLoop.php @@ -122,19 +122,23 @@ protected function sleepOnPendingTimers() } } - protected function runStreamSelect() + protected function runStreamSelect($block) { $read = $this->readStreams ?: null; $write = $this->writeStreams ?: null; $except = null; if (!$read && !$write) { - $this->sleepOnPendingTimers(); + if ($block) { + $this->sleepOnPendingTimers(); + } return; } - if (stream_select($read, $write, $except, 0, $this->getNextEventTimeInMicroSeconds()) > 0) { + $timeout = $block ? $this->getNextEventTimeInMicroSeconds() : 0; + + if (stream_select($read, $write, $except, 0, $timeout) > 0) { if ($read) { foreach ($read as $stream) { $listener = $this->readListeners[(int) $stream]; @@ -155,21 +159,23 @@ protected function runStreamSelect() } } - public function tick() + protected function loop($block = true) { $this->timers->tick(); - $this->runStreamSelect(); + $this->runStreamSelect($block); return $this->running; } + public function tick() + { + return $this->loop(false); + } + public function run() { $this->running = true; - - while ($this->tick()) { - // NOOP - } + while ($this->loop()); } public function stop()