Skip to content

Commit 2655e98

Browse files
committed
Took into account if the loop is still running and if there are any signal listeners attached for calling uv_run, modeled (copied) the bit from ExtEvLoop and had to rename $timerEvents to $timers (also makes more sense) as suggested by @clue at reactphp#112 (comment)
1 parent 8db0f57 commit 2655e98

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/ExtUvLoop.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ final class ExtUvLoop implements LoopInterface
1313
{
1414
private $uv;
1515
private $futureTickQueue;
16-
private $timerEvents;
16+
private $timers;
1717
private $streamEvents = array();
1818
private $flags = array();
1919
private $readStreams = array();
@@ -31,7 +31,7 @@ public function __construct()
3131

3232
$this->uv = \uv_loop_new();
3333
$this->futureTickQueue = new FutureTickQueue();
34-
$this->timerEvents = new SplObjectStorage();
34+
$this->timers = new SplObjectStorage();
3535
$this->streamListener = $this->createStreamListener();
3636
$this->signals = new SignalsHandler();
3737
}
@@ -96,7 +96,7 @@ public function addTimer($interval, $callback)
9696
$timer = new Timer( $interval, $callback, false);
9797

9898
$that = $this;
99-
$timers = $this->timerEvents;
99+
$timers = $this->timers;
100100
$callback = function () use ($timer, $timers, $that) {
101101
call_user_func($timer->getCallback(), $timer);
102102

@@ -106,7 +106,7 @@ public function addTimer($interval, $callback)
106106
};
107107

108108
$event = \uv_timer_init($this->uv);
109-
$this->timerEvents->attach($timer, $event);
109+
$this->timers->attach($timer, $event);
110110
\uv_timer_start(
111111
$event,
112112
(int)ceil($interval * 1000),
@@ -129,7 +129,7 @@ public function addPeriodicTimer($interval, $callback)
129129
};
130130

131131
$event = \uv_timer_init($this->uv);
132-
$this->timerEvents->attach($timer, $event);
132+
$this->timers->attach($timer, $event);
133133
\uv_timer_start(
134134
$event,
135135
(int)ceil($interval * 1000),
@@ -145,9 +145,9 @@ public function addPeriodicTimer($interval, $callback)
145145
*/
146146
public function cancelTimer(TimerInterface $timer)
147147
{
148-
if (isset($this->timerEvents[$timer])) {
149-
@\uv_timer_stop($this->timerEvents[$timer]);
150-
$this->timerEvents->detach($timer);
148+
if (isset($this->timers[$timer])) {
149+
@\uv_timer_stop($this->timers[$timer]);
150+
$this->timers->detach($timer);
151151
}
152152
}
153153

@@ -192,14 +192,24 @@ public function run()
192192
while ($this->running) {
193193
$this->futureTickQueue->tick();
194194

195-
if ($this->futureTickQueue->isEmpty() && empty($this->streamEvents) && $this->timerEvents->count() === 0) {
196-
break;
197-
}
195+
$hasPendingCallbacks = !$this->futureTickQueue->isEmpty();
196+
$wasJustStopped = !$this->running;
197+
$nothingLeftToDo = !$this->readStreams
198+
&& !$this->writeStreams
199+
&& !$this->timers->count()
200+
&& $this->signals->isEmpty();
198201

199202
// Use UV::RUN_ONCE when there are only I/O events active in the loop and block until one of those triggers,
200203
// otherwise use UV::RUN_NOWAIT.
201204
// @link http://docs.libuv.org/en/v1.x/loop.html#c.uv_run
202-
\uv_run($this->uv, $this->futureTickQueue->isEmpty() && $this->timerEvents->count() === 0 ? \UV::RUN_ONCE : \UV::RUN_NOWAIT);
205+
$flags = \UV::RUN_ONCE;
206+
if ($wasJustStopped || $hasPendingCallbacks) {
207+
$flags = \UV::RUN_NOWAIT;
208+
} elseif ($nothingLeftToDo) {
209+
break;
210+
}
211+
212+
\uv_run($this->uv, $flags);
203213
}
204214
}
205215

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy