8000 Simplify adding signal handlers · reactphp/event-loop@8afd1f3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8afd1f3

Browse files
committed
Simplify adding signal handlers
1 parent e6c7785 commit 8afd1f3

6 files changed

+56
-84
lines changed

src/ExtEventLoop.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +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-
45-
$this->signals = new SignalsHandler(
46-
$this,
47-
function ($signal) {
48-
$this->signalEvents[$signal] = Event::signal($this->eventBase, $signal, $f = function () use ($signal, &$f) {
49-
$this->signals->call($signal);
50-
// Ensure there are two copies of the callable around until it has been executed.
51-
// For more information see: https://bugs.php.net/bug.php?id=62452
52-
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
53-
$g = $f;
54-
$f = $g;
55-
});
56-
$this->signalEvents[$signal]->add();
57-
}
58-
);
44+
$this->signals = new SignalsHandler($this);
5945

6046
$this->createTimerCallback();
6147
$this->createStreamCallback();
@@ -161,6 +147,18 @@ public function futureTick($listener)
161147
public function addSignal($signal, $listener)
162148
{
163149
$this->signals->add($signal, $listener);
150+
151+
if (!isset($this->signalEvents[$signal])) {
152+
$this->signalEvents[$signal] = Event::signal($this->eventBase, $signal, $f = function () use ($signal, &$f) {
153+
$this->signals->call($signal);
154+
// Ensure there are two copies of the callable around until it has been executed.
155+
// For more information see: https://bugs.php.net/bug.php?id=62452
156+
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
157+
$g = $f;
158+
$f = $g;
159+
});
160+
$this->signalEvents[$signal]->add();
161+
}
164162
}
165163

166164
public function removeSignal($signal, $listener)

src/ExtLibevLoop.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,7 @@ public function __construct()
3939
$this->loop = new EventLoop();
4040
$this->futureTickQueue = new FutureTickQueue();
4141
$this->timerEvents = new SplObjectStorage();
42-
43-
$this->signals = new SignalsHandler(
44-
$this,
45-
function ($signal) {
46-
$this->signalEvents[$signal] = new SignalEvent($f = function () use ($signal, &$f) {
47-
$this->signals->call($signal);
48-
// Ensure there are two copies of the callable around until it has been executed.
49-
// For more information see: https://bugs.php.net/bug.php?id=62452
50-
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
51-
$g = $f;
52-
$f = $g;
53-
}, $signal);
54-
$this->loop->add($this->signalEvents[$signal]);
55-
}
56-
);
42+
$this->signals = new SignalsHandler($this);
5743
}
5844

5945
public function addReadStream($stream, $listener)
@@ -160,6 +146,18 @@ public function futureTick($listener)
160146
public function addSignal($signal, $listener)
161147
{
162148
$this->signals->add($signal, $listener);
149+
150+
if (!isset($this->signalEvents[$signal])) {
151+
$this->signalEvents[$signal] = new SignalEvent($f = function () use ($signal, &$f) {
152+
$this->signals->call($signal);
153+
// Ensure there are two copies of the callable around until it has been executed.
154+
// For more information see: https://bugs.php.net/bug.php?id=62452
155+
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
156+
$g = $f;
157+
$f = $g;
158+
}, $signal);
159+
$this->loop->add($this->signalEvents[$signal]);
160+
}
163161
}
164162

165163
public function removeSignal($signal, $listener)

src/ExtLibeventLoop.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,23 +55,7 @@ public function __construct()
5555
$this->eventBase = event_base_new();
5656
$this->futureTickQueue = new FutureTickQueue();
5757
$this->timerEvents = new SplObjectStorage();
58-
59-
$this->signals = new SignalsHandler(
60-
$this,
61-
function ($signal) {
62-
$this->signalEvents[$signal] = event_new();
63-
event_set($this->signalEvents[$signal], $signal, EV_PERSIST | EV_SIGNAL, $f = function () use ($signal, &$f) {
64-
$this->signals->call($signal);
65-
// Ensure there are two copies of the callable around until it has been executed.
66-
// For more information see: https://bugs.php.net/bug.php?id=62452
67-
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
68-
$g = $f;
69-
$f = $g;
70-
});
71-
event_base_set($this->signalEvents[$signal], $this->eventBase);
72-
event_add($this->signalEvents[$signal]);
73-
}
74-
);
58+
$this->signals = new SignalsHandler($this);
7559

7660
$this->createTimerCallback();
7761
$this->createStreamCallback();
@@ -178,6 +162,20 @@ public function futureTick($listener)
178162
public function addSignal($signal, $listener)
179163
{
180164
$this->signals->add($signal, $listener);
165+
166+
if (!isset($this->signalEvents[$signal])) {
167+
$this->signalEvents[$signal] = event_new();
168+
event_set($this->signalEvents[$signal], $signal, EV_PERSIST | EV_SIGNAL, $f = function () use ($signal, &$f) {
169+
$this->signals->call($signal);
170+
// Ensure there are two copies of the callable around until it has been executed.
171+
// For more information see: https://bugs.php.net/bug.php?id=62452
172+
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
173+
$g = $f;
174+
$f = $g;
175+
});
176+
event_base_set($this->signalEvents[$signal], $this->eventBase);
177+
event_add($this->signalEvents[$signal]);
178+
}
181179
}
182180

183181
public function removeSignal($signal, $listener)

src/SignalsHandler.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ final class SignalsHandler
1010
private $loop;
1111
private $timer;
1212
private $signals = [];
13-
private $on;
1413

15-
public function __construct(LoopInterface $loop, $on)
14+
public function __construct(LoopInterface $loop)
1615
{
1716
$this->loop = $loop;
18-
$this->on = $on;
1917
}
2018

2119
public function add($signal, $listener)
@@ -29,9 +27,6 @@ public function add($signal, $listener)
2927

3028
if (!isset($this->signals[$signal])) {
3129
$this->signals[$signal] = [];
32-
33-
$on = $this->on;
34-
$on($signal);
3530
}
3631

3732
if (in_array($listener, $this->signals[$signal])) {

src/StreamSelectLoop.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +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(
73-
$this,
74-
function ($signal) {
75-
\pcntl_signal($signal, $f = function ($signal) use (&$f) {
76-
$this->signals->call($signal);
77-
// Ensure there are two copies of the callable around until it has been executed.
78-
// For more information see: https://bugs.php.net/bug.php?id=62452
79-
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
80-
$g = $f;
81-
$f = $g;
82-
});
83-
}
84-
);
72+
$this->signals = new SignalsHandler($this);
8573
}
8674

8775
public function addReadStream($stream, $listener)
@@ -158,7 +146,19 @@ public function addSignal($signal, $listener)
158146
throw new \BadMethodCallException('Event loop feature "signals" isn\'t supported by the "StreamSelectLoop"');
159147
}
160148

149+
$first = $this->signals->count($signal) === 0;
161150
$this->signals->add($signal, $listener);
151+
152+
if ($first) {
153+
\pcntl_signal($signal, $f = function ($signal) use (&$f) {
154+
$this->signals->call($signal);
155+
// Ensure there are two copies of the callable around until it has been executed.
156+
// For more information see: https://bugs.php.net/bug.php?id=62452
157+
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
158+
$g = $f;
159+
$f = $g;
160+
});
161+
}
162162
}
163163

164164
public function removeSignal($signal, $listener)

tests/SignalsHandlerTest.php

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,66 +10,49 @@ final class SignalsHandlerTest extends TestCase
1010
public function testEmittedEventsAndCallHandling()
1111
{
1212
$callCount = 0;
13-
$onCount = 0;
1413
$func = function () use (&$callCount) {
1514
$callCount++;
1615
};
1716
$signals = new SignalsHandler(
18-
Factory::create(),
19-
function () use (&$onCount) {
20-
$onCount++;
21-
}
17+
Factory::create()
2218
);
2319

2420
$this->assertSame(0, $callCount);
25-
$this->assertSame(0, $onCount);
2621

2722
$signals->add(SIGUSR1, $func);
2823
$this->assertSame(0, $callCount);
29-
$this->assertSame(1, $onCount);
3024

3125
$signals->add(SIGUSR1, $func);
3226
$this->assertSame(0, $callCount);
33-
$this->assertSame(1, $onCount);
3427

3528
$signals->add(SIGUSR1, $func);
3629
$this->assertSame(0, $callCount);
37-
$this->assertSame(1, $onCount);
3830

3931
$signals->call(SIGUSR1);
4032
$this->assertSame(1, $callCount);
41-
$this->assertSame(1, $onCount);
4233

4334
$signals->add(SIGUSR2, $func);
4435
$this->assertSame(1, $callCount);
45-
$this->assertSame(2, $onCount);
4636

4737
$signals->add(SIGUSR2, $func);
4838
$this->assertSame(1, $callCount);
49-
$this->assertSame(2, $onCount);
5039

5140
$signals->call(SIGUSR2);
5241
$this->assertSame(2, $callCount);
53-
$this->assertSame(2, $onCount);
5442

5543
$signals->remove(SIGUSR2, $func);
5644
$this->assertSame(2, $callCount);
57-
$this->assertSame(2, $onCount);
5845

5946
$signals->remove(SIGUSR2, $func);
6047
$this->assertSame(2, $callCount);
61-
$this->assertSame(2, $onCount);
6248

6349
$signals->call(SIGUSR2);
6450
$this->assertSame(2, $callCount);
65-
$this->assertSame(2, $onCount);
6651

6752
$signals->remove(SIGUSR1, $func);
6853
$this->assertSame(2, $callCount);
69-
$this->assertSame(2, $onCount);
7054

7155
$signals->call(SIGUSR1);
7256
$this->assertSame(2, $callCount);
73-
$this->assertSame(2, $onCount);
7457
}
7558
}

0 commit comments

Comments
 (0)
0