@@ -13,7 +13,7 @@ final class ExtUvLoop implements LoopInterface
13
13
{
14
14
private $ uv ;
15
15
private $ futureTickQueue ;
16
- private $ timerEvents ;
16
+ private $ timers ;
17
17
private $ streamEvents = array ();
18
18
private $ flags = array ();
19
19
private $ readStreams = array ();
@@ -31,7 +31,7 @@ public function __construct()
31
31
32
32
$ this ->uv = \uv_loop_new ();
33
33
$ this ->futureTickQueue = new FutureTickQueue ();
34
- $ this ->timerEvents = new SplObjectStorage ();
34
+ $ this ->timers = new SplObjectStorage ();
35
35
$ this ->streamListener = $ this ->createStreamListener ();
36
36
$ this ->signals = new SignalsHandler ();
37
37
}
@@ -96,7 +96,7 @@ public function addTimer($interval, $callback)
96
96
$ timer = new Timer ( $ interval , $ callback , false );
97
97
98
98
$ that = $ this ;
99
- $ timers = $ this ->timerEvents ;
99
+ $ timers = $ this ->timers ;
100
100
$ callback = function () use ($ timer , $ timers , $ that ) {
101
101
call_user_func ($ timer ->getCallback (), $ timer );
102
102
@@ -106,7 +106,7 @@ public function addTimer($interval, $callback)
106
106
};
107
107
108
108
$ event = \uv_timer_init ($ this ->uv );
109
- $ this ->timerEvents ->attach ($ timer , $ event );
109
+ $ this ->timers ->attach ($ timer , $ event );
110
110
\uv_timer_start (
111
111
$ event ,
112
112
(int )ceil ($ interval * 1000 ),
@@ -129,7 +129,7 @@ public function addPeriodicTimer($interval, $callback)
129
129
};
130
130
131
131
$ event = \uv_timer_init ($ this ->uv );
132
- $ this ->timerEvents ->attach ($ timer , $ event );
132
+ $ this ->timers ->attach ($ timer , $ event );
133
133
\uv_timer_start (
134
134
$ event ,
135
135
(int )ceil ($ interval * 1000 ),
@@ -145,9 +145,9 @@ public function addPeriodicTimer($interval, $callback)
145
145
*/
146
146
public function cancelTimer (TimerInterface $ timer )
147
147
{
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 );
151
151
}
152
152
}
153
153
@@ -192,14 +192,24 @@ public function run()
192
192
while ($ this ->running ) {
193
193
$ this ->futureTickQueue ->tick ();
194
194
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 ();
198
201
199
202
// Use UV::RUN_ONCE when there are only I/O events active in the loop and block until one of those triggers,
200
203
// otherwise use UV::RUN_NOWAIT.
201
204
// @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 );
203
213
}
204
214
}
205
215
0 commit comments