@@ -10,7 +10,7 @@ class LibEventLoop implements LoopInterface
10
10
private $ callback ;
11
11
12
12
private $ timers = array ();
13
- private $ timersGc = array () ;
13
+ private $ timersGc ;
14
14
15
15
private $ events = array ();
16
16
private $ flags = array ();
@@ -21,24 +21,18 @@ public function __construct()
21
21
{
22
22
$ this ->base = event_base_new ();
23
23
$ this ->callback = $ this ->createLibeventCallback ();
24
+ $ this ->timersGc = new \SplQueue ();
25
+ $ this ->timersGc ->setIteratorMode (\SplQueue::IT_MODE_DELETE );
24
26
}
25
27
26
28
protected function createLibeventCallback ()
27
29
{
28
- $ timersGc = &$ this ->timersGc ;
29
30
$ readCallbacks = &$ this ->readCallbacks ;
30
31
$ writeCallbacks = &$ this ->writeCallbacks ;
31
32
32
- return function ($ stream , $ flags , $ loop ) use (&$ timersGc , & $ readCallbacks , &$ writeCallbacks ) {
33
+ return function ($ stream , $ flags , $ loop ) use (&$ readCallbacks , &$ writeCallbacks ) {
33
34
$ id = (int ) $ stream ;
34
35
35
- if ($ timersGc ) {
36
- foreach ($ timersGc as $ signature => $ resource ) {
37
- event_free ($ resource );
38
- unset($ timersGc [$ signature ]);
39
- }
40
- }
41
-
42
36
try {
43
37
if (($ flags & EV_READ ) === EV_READ && isset ($ readCallbacks [$ id ])) {
44
38
if (call_user_func ($ readCallbacks [$ id ], $ stream , $ loop ) === false ) {
@@ -177,13 +171,19 @@ protected function addTimerInternal($interval, $callback, $periodic = false)
177
171
);
178
172
179
173
$ timer ->signature = spl_object_hash ($ timer );
174
+ $ timersGc = $ this ->timersGc ;
175
+ $ callback = function () use ($ timer , $ timersGc ) {
176
+ foreach ($ timersGc as $ resource ) {
177
+ event_free ($ resource );
178
+ }
180
179
181
- $ callback = function () use ($ timer ) {
182
180
if ($ timer ->cancelled === false ) {
183
181
call_user_func ($ timer ->callback , $ timer ->signature , $ timer ->loop );
184
182
185
183
if ($ timer ->periodic === true ) {
186
184
event_add ($ timer ->resource , $ timer ->interval );
185
+ } else {
186
+ $ this ->cancelTimer ($ timer ->signature );
187
187
}
188
188
}
189
189
};
@@ -214,7 +214,7 @@ public function cancelTimer($signature)
214
214
215
215
$ timer ->cancelled = true ;
216
216
event_del ($ timer ->resource );
217
- $ this ->timersGc [ $ signature ] = $ timer ->resource ;
217
+ $ this ->timersGc -> enqueue ( $ timer ->resource ) ;
218
218
unset($ this ->timers [$ signature ]);
219
219
}
220
220
}
0 commit comments