Skip to content

Commit 3e4421b

Browse files
committed
Simplify checking signal watchers and remove circular reference
1 parent 8afd1f3 commit 3e4421b

6 files changed

Lines changed: 15 additions & 32 deletions

File tree

src/ExtEventLoop.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(EventBaseConfig $config = null)
4141
$this->eventBase = new EventBase($config);
4242
$this->futureTickQueue = new FutureTickQueue();
4343
$this->timerEvents = new SplObjectStorage();
44-
$this->signals = new SignalsHandler($this);
44+
$this->signals = new SignalsHandler();
4545

4646
$this->createTimerCallback();
4747
$this->createStreamCallback();
@@ -181,7 +181,7 @@ public function run()
181181
$flags = EventBase::LOOP_ONCE;
182182
if (!$this->running || !$this->futureTickQueue->isEmpty()) {
183183
$flags |= EventBase::LOOP_NONBLOCK;
184-
} elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count()) {
184+
} elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count() && $this->signals->isEmpty()) {
185185
break;
186186
}
187187

src/ExtLibevLoop.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct()
3939
$this->loop = new EventLoop();
4040
$this->futureTickQueue = new FutureTickQueue();
4141
$this->timerEvents = new SplObjectStorage();
42-
$this->signals = new SignalsHandler($this);
42+
$this->signals = new SignalsHandler();
4343
}
4444

4545
public function addReadStream($stream, $listener)
@@ -181,7 +181,7 @@ public function run()
181181
$flags = EventLoop::RUN_ONCE;
182182
if (!$this->running || !$this->futureTickQueue->isEmpty()) {
183183
$flags |= EventLoop::RUN_NOWAIT;
184-
} elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count()) {
184+
} elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count() && $this->signals->isEmpty()) {
185185
break;
186186
}
187187

src/ExtLibeventLoop.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function __construct()
5555
$this->eventBase = event_base_new();
5656
$this->futureTickQueue = new FutureTickQueue();
5757
$this->timerEvents = new SplObjectStorage();
58-
$this->signals = new SignalsHandler($this);
58+
$this->signals = new SignalsHandler();
5959

6060
$this->createTimerCallback();
6161
$this->createStreamCallback();
@@ -199,7 +199,7 @@ public function run()
199199
$flags = EVLOOP_ONCE;
200200
if (!$this->running || !$this->futureTickQueue->isEmpty()) {
201201
$flags |= EVLOOP_NONBLOCK;
202-
} elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count()) {
202+
} elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count() && $this->signals->isEmpty()) {
203203
break;
204204
}
205205

src/SignalsHandler.php

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,10 @@
77
*/
88
final class SignalsHandler
99
{
10-
private $loop;
11-
private $timer;
1210
private $signals = [];
1311

14-
public function __construct(LoopInterface $loop)
15-
{
16-
$this->loop = $loop;
17-
}
18-
1912
public function add($signal, $listener)
2013
{
21-
if (empty($this->signals) && $this->timer === null) {
22-
/**
23-
* Timer to keep the loop alive as long as there are any signal handlers registered
24-
*/
25-
$this->timer = $this->loop->addPeriodicTimer(300, function () {});
26-
}
27-
2814
if (!isset($this->signals[$signal])) {
2915
$this->signals[$signal] = [];
3016
}
@@ -48,11 +34,6 @@ public function remove($signal, $listener)
4834
if (isset($this->signals[$signal]) && \count($this->signals[$signal]) === 0) {
4935
unset($this->signals[$signal]);
5036
}
51-
52-
if (empty($this->signals) && $this->timer instanceof TimerInterface) {
53-
$this->loop->cancelTimer($this->timer);
54-
$this->timer = null;
55-
}
5637
}
5738

5839
public function call($signal)
@@ -74,4 +55,9 @@ public function count($signal)
7455

7556
return \count($this->signals[$signal]);
7657
}
58+
59+
public function isEmpty()
60+
{
61+
return !$this->signals;
62+
}
7763
}

src/StreamSelectLoop.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function __construct()
6969
$this->futureTickQueue = new FutureTickQueue();
7070
$this->timers = new Timers();
7171
$this->pcntl = extension_loaded('pcntl');
72-
$this->signals = new SignalsHandler($this);
72+
$this->signals = new SignalsHandler();
7373
}
7474

7575
public function addReadStream($stream, $listener)
@@ -200,8 +200,8 @@ public function run()
200200
$timeout = $timeout > PHP_INT_MAX ? PHP_INT_MAX : (int)$timeout;
201201
}
202202

203-
// The only possible event is stream activity, so wait forever ...
204-
} elseif ($this->readStreams || $this->writeStreams) {
203+
// The only possible event is stream or signal activity, so wait forever ...
204+
} elseif ($this->readStreams || $this->writeStreams || !$this->signals->isEmpty()) {
205205
$timeout = null;
206206

207207
// There's nothing left to do ...

tests/SignalsHandlerTest.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace React\Tests\EventLoop;
44

5-
use React\EventLoop\Factory;
65
use React\EventLoop\SignalsHandler;
76

87
final class SignalsHandlerTest extends TestCase
@@ -13,9 +12,7 @@ public function testEmittedEventsAndCallHandling()
1312
$func = function () use (&$callCount) {
1413
$callCount++;
1514
};
16-
$signals = new SignalsHandler(
17-
Factory::create()
18-
);
15+
$signals = new SignalsHandler();
1916

2017
$this->assertSame(0, $callCount);
2118

0 commit comments

Comments
 (0)