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

Skip to content

Commit e6c7785

Browse files
committed
Simplify removing signal handlers
1 parent 0f8c6b4 commit e6c7785

File tree

7 files changed

+32
-56
lines changed

7 files changed

+32
-56
lines changed

src/ExtEventLoop.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,6 @@ function ($signal) {
5454
$f = $g;
5555
});
5656
$this->signalEvents[$signal]->add();
57-
},
58-
function ($signal) {
59-
if ($this->signals->count($signal) === 0) {
60-
$this->signalEvents[$signal]->free();
61-
unset($this->signalEvents[$signal]);
62-
}
6357
}
6458
);
6559

@@ -172,6 +166,11 @@ public function addSignal($signal, $listener)
172166
public function removeSignal($signal, $listener)
173167
{
174168
$this->signals->remove($signal, $listener);
169+
170+
if (isset($this->signalEvents[$signal]) && $this->signals->count($signal) === 0) {
171+
$this->signalEvents[$signal]->free();
172+
unset($this->signalEvents[$signal]);
173+
}
175174
}
176175

177176
public function run()

src/ExtLibevLoop.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ function ($signal) {
5252
$f = $g;
5353
}, $signal);
5454
$this->loop->add($this->signalEvents[$signal]);
55-
},
56-
function ($signal) {
57-
if ($this->signals->count($signal) === 0) {
58-
$this->signalEvents[$signal]->stop();
59-
$this->loop->remove($this->signalEvents[$signal]);
60-
unset($this->signalEvents[$signal]);
61-
}
6255
}
6356
);
6457
}
@@ -172,6 +165,12 @@ public function addSignal($signal, $listener)
172165
public function removeSignal($signal, $listener)
173166
{
174167
$this->signals->remove($signal, $listener);
168+
169+
if (isset($this->signalEvents[$signal]) && $this->signals->count($signal) === 0) {
170+
$this->signalEvents[$signal]->stop();
171+
$this->loop->remove($this->signalEvents[$signal]);
172+
unset($this->signalEvents[$signal]);
173+
}
175174
}
176175

177176
public function run()

src/ExtLibeventLoop.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,6 @@ function ($signal) {
7070
});
7171
event_base_set($this->signalEvents[$signal], $this->eventBase);
7272
event_add($this->signalEvents[$signal]);
73-
},
74-
function ($signal) {
75-
if ($this->signals->count($signal) === 0) {
76-
event_del($this->signalEvents[$signal]);
77-
event_free($this->signalEvents[$signal]);
78-
unset($this->signalEvents[$signal]);
79-
}
8073
}
8174
);
8275

@@ -190,6 +183,12 @@ public function addSignal($signal, $listener)
190183
public function removeSignal($signal, $listener)
191184
{
192185
$this->signals->remove($signal, $listener);
186+
187+
if (isset($this->signalEvents[$signal]) && $this->signals->count($signal) === 0) {
188+
event_del($this->signalEvents[$signal]);
189+
event_free($this->signalEvents[$signal]);
190+
unset($this->signalEvents[$signal]);
191+
}
193192
}
194193

195194
public function run()

src/SignalsHandler.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,11 @@ final class SignalsHandler
1111
private $timer;
1212
private $signals = [];
1313
private $on;
14-
private $off;
1514

16-
public function __construct(LoopInterface $loop, $on, $off)
15+
public function __construct(LoopInterface $loop, $on)
1716
{
1817
$this->loop = $loop;
1918
$this->on = $on;
20-
$this->off = $off;
21-
}
22-
23-
public function __destruct()
24-
{
25-
$off = $this->off;
26-
foreach ($this->signals as $signal => $listeners) {
27-
$off($signal);
28-
}
2919
}
3020

3121
public function add($signal, $listener)
@@ -62,9 +52,6 @@ public function remove($signal, $listener)
6252

6353
if (isset($this->signals[$signal]) && \count($this->signals[$signal]) === 0) {
6454
unset($this->signals[$signal]);
65-
66-
$off = $this->off;
67-
$off($signal);
6855
}
6956

7057
if (empty($this->signals) && $this->timer instanceof TimerInterface) {

src/StreamSelectLoop.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,6 @@ function ($signal) {
8080
$g = $f;
8181
$f = $g;
8282
});
83-
},
84-
function ($signal) {
85-
if ($this->signals->count($signal) === 0) {
86-
\pcntl_signal($signal, SIG_DFL);
87-
}
8883
}
8984
);
9085
}
@@ -168,7 +163,15 @@ public function addSignal($signal, $listener)
168163

169164
public function removeSignal($signal, $listener)
170165
{
166+
if (!$this->signals->count($signal)) {
167+
return;
168+
}
169+
171170
$this->signals->remove($signal, $listener);
171+
172+
if ($this->signals->count($signal) === 0) {
173+
\pcntl_signal($signal, SIG_DFL);
174+
}
172175
}
173176

174177
public function run()

tests/AbstractLoopTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,12 @@ function () {
475475
$this->loop->run();
476476
}
477477

478+
public function testRemoveSignalNotRegisteredIsNoOp()
479+
{
480+
$this->loop->removeSignal(SIGINT, function () { });
481+
$this->assertTrue(true);
482+
}
483+
478484
public function testSignal()
479485
{
480486
if (!function_exists('posix_kill') || !function_exists('posix_getpid')) {

tests/SignalsHandlerTest.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,82 +11,65 @@ public function testEmittedEventsAndCallHandling()
1111
{
1212
$callCount = 0;
1313
$onCount = 0;
14-
$offCount = 0;
1514
$func = function () use (&$callCount) {
1615
$callCount++;
1716
};
1817
$signals = new SignalsHandler(
1918
Factory::create(),
2019
function () use (&$onCount) {
2120
$onCount++;
22-
},
23-
function () use (&$offCount) {
24-
$offCount++;
2521
}
2622
);
2723

2824
$this->assertSame(0, $callCount);
2925
$this->assertSame(0, $onCount);
30-
$this->assertSame(0, $offCount);
3126

3227
$signals->add(SIGUSR1, $func);
3328
$this->assertSame(0, $callCount);
3429
$this->assertSame(1, $onCount);
35-
$this->assertSame(0, $offCount);
3630

3731
$signals->add(SIGUSR1, $func);
3832
$this->assertSame(0, $callCount);
3933
$this->assertSame(1, $onCount);
40-
$this->assertSame(0, $offCount);
4134

4235
$signals->add(SIGUSR1, $func);
4336
$this->assertSame(0, $callCount);
4437
$this->assertSame(1, $onCount);
45-
$this->assertSame(0, $offCount);
4638

4739
$signals->call(SIGUSR1);
4840
$this->assertSame(1, $callCount);
4941
$this->assertSame(1, $onCount);
50-
$this->assertSame(0, $offCount);
5142

5243
$signals->add(SIGUSR2, $func);
5344
$this->assertSame(1, $callCount);
5445
$this->assertSame(2, $onCount);
55-
$this->assertSame(0, $offCount);
5646

5747
$signals->add(SIGUSR2, $func);
5848
$this->assertSame(1, $callCount);
5949
$this->assertSame(2, $onCount);
60-
$this->assertSame(0, $offCount);
6150

6251
$signals->call(SIGUSR2);
6352
$this->assertSame(2, $callCount);
6453
$this->assertSame(2, $onCount);
65-
$this->assertSame(0, $offCount);
6654

6755
$signals->remove(SIGUSR2, $func);
6856
$this->assertSame(2, $callCount);
6957
$this->assertSame(2, $onCount);
70-
$this->assertSame(1, $offCount);
7158

7259
$signals->remove(SIGUSR2, $func);
7360
$this->assertSame(2, $callCount);
7461
$this->assertSame(2, $onCount);
75-
$this->assertSame(1, $offCount);
7662

7763
$signals->call(SIGUSR2);
7864
$this->assertSame(2, $callCount);
7965
$this->assertSame(2, $onCount);
80-
$this->assertSame(1, $offCount);
8166

8267
$signals->remove(SIGUSR1, $func);
8368
$this->assertSame(2, $callCount);
8469
$this->assertSame(2, $onCount);
85-
$this->assertSame(2, $offCount);
8670

8771
$signals->call(SIGUSR1);
8872
$this->assertSame(2, $callCount);
8973
$this->assertSame(2, $onCount);
90-
$this->assertSame(2, $offCount);
9174
}
9275
}

0 commit comments

Comments
 (0)
0