44
55use Event ;
66use EventBase ;
7+ use React \EventLoop \Signal \Pcntl ;
78use React \EventLoop \Tick \FutureTickQueue ;
89use React \EventLoop \Timer \Timer ;
910use React \EventLoop \Timer \TimerInterface ;
@@ -26,13 +27,39 @@ class LibEventLoop implements LoopInterface
2627 private $ readListeners = [];
2728 private $ writeListeners = [];
2829 private $ running ;
30+ private $ signals ;
31+ private $ signalEvents = [];
2932
3033 public function __construct ()
3134 {
3235 $ this ->eventBase = event_base_new ();
3336 $ this ->futureTickQueue = new FutureTickQueue ();
3437 $ this ->timerEvents = new SplObjectStorage ();
3538
39+ $ this ->signals = new SignalsHandler (
40+ $ this ,
41+ function ($ signal ) {
42+ $ this ->signalEvents [$ signal ] = event_new ();
43+ event_set ($ this ->signalEvents [$ signal ], $ signal , EV_PERSIST | EV_SIGNAL , $ f = function () use ($ signal , &$ f ) {
44+ $ this ->signals ->call ($ signal );
45+ // Ensure there are two copies of the callable around until it has been executed.
46+ // For more information see: https://bugs.php.net/bug.php?id=62452
47+ // Only an issue for PHP 5, this hack can be removed once PHP 5 suppose has been dropped.
48+ $ g = $ f ;
49+ $ f = $ g ;
50+ });
51+ event_base_set ($ this ->signalEvents [$ signal ], $ this ->eventBase );
52+ event_add ($ this ->signalEvents [$ signal ]);
53+ },
54+ function ($ signal ) {
55+ if ($ this ->signals ->count ($ signal ) === 0 ) {
56+ event_del ($ this ->signalEvents [$ signal ]);
57+ event_free ($ this ->signalEvents [$ signal ]);
58+ unset($ this ->signalEvents [$ signal ]);
59+ }
60+ }
61+ );
62+
3663 $ this ->createTimerCallback ();
3764 $ this ->createStreamCallback ();
3865 }
@@ -166,6 +193,22 @@ public function futureTick(callable $listener)
166193 $ this ->futureTickQueue ->add ($ listener );
167194 }
168195
196+ /**
197+ * {@inheritdoc}
198+ */
199+ public function addSignal ($ signal , callable $ listener )
200+ {
201+ $ this ->signals ->add ($ signal , $ listener );
202+ }
203+
204+ /**
205+ * {@inheritdoc}
206+ */
207+ public function removeSignal ($ signal , callable $ listener )
208+ {
209+ $ this ->signals ->remove ($ signal , $ listener );
210+ }
211+
169212 /**
170213 * {@inheritdoc}
171214 */
0 commit comments