10000 Refactor and simplify signal handling logic by clue · Pull Request #150 · reactphp/event-loop · GitHub
[go: up one dir, main page]

Skip to content

8000 Refactor and simplify signal handling logic #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Simplify adding signal handlers
  • Loading branch information
clue committed Feb 6, 2018
commit 8afd1f3c3de4b79238782e6556ac1a96fbd0b87f
28 changes: 13 additions & 15 deletions src/ExtEventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,7 @@ public function __construct(EventBaseConfig $config = null)
$this->eventBase = new EventBase($config);
$this->futureTickQueue = new FutureTickQueue();
$this->timerEvents = new SplObjectStorage();

$this->signals = new SignalsHandler(
$this,
function ($signal) {
$this->signalEvents[$signal] = Event::signal($this->eventBase, $signal, $f = function () use ($signal, &$f) {
$this->signals->call($signal);
// Ensure there are two copies of the callable around until it has been executed.
// For more information see: https://bugs.php.net/bug.php?id=62452
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
$g = $f;
$f = $g;
});
$this->signalEvents[$signal]->add();
}
);
$this->signals = new SignalsHandler($this);

$this->createTimerCallback();
$this->createStreamCallback();
Expand Down Expand Up @@ -161,6 +147,18 @@ public function futureTick($listener)
public function addSignal($signal, $listener)
{
$this->signals->add($signal, $listener);

if (!isset($this->signalEvents[$signal])) {
$this->signalEvents[$signal] = Event::signal($this->eventBase, $signal, $f = function () use ($signal, &$f) {
$this->signals->call($signal);
// Ensure there are two copies of the callable around until it has been executed.
// For more information see: https://bugs.php.net/bug.php?id=62452
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
$g = $f;
$f = $g;
});
$this->signalEvents[$signal]->add();
}
}

public function removeSignal($signal, $listener)
Expand Down
28 changes: 13 additions & 15 deletions src/ExtLibevLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,7 @@ public function __construct()
$this->loop = new EventLoop();
$this->futureTickQueue = new FutureTickQueue();
$this->timerEvents = new SplObjectStorage();

$this->signals = new SignalsHandler(
$this,
function ($signal) {
$this->signalEvents[$signal] = new SignalEvent($f = function () use ($signal, &$f) {
$this->signals->call($signal);
// Ensure there are two copies of the callable around until it has been executed.
// For more information see: https://bugs.php.net/bug.php?id=62452
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
$g = $f;
$f = $g;
}, $signal);
$this->loop->add($this->signalEvents[$signal]);
}
);
$this->signals = new SignalsHandler($this);
}

public function addReadStream($stream, $listener)
Expand Down Expand Up @@ -160,6 +146,18 @@ public function futureTick($listener)
public function addSignal($signal, $listener)
{
$this->signals->add($signal, $listener);

if (!isset($this->signalEvents[$signal])) {
$this->signalEvents[$signal] = new SignalEvent($f = function () use ($signal, &$f) {
$this->signals->call($signal);
// Ensure there are two copies of the callable around until it has been executed.
// For more information see: https://bugs.php.net/bug.php?id=62452
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
$g = $f;
$f = $g;
}, $signal);
$this->loop->add($this->signalEvents[$signal]);
}
}

public function removeSignal($signal, $listener)
Expand Down
32 changes: 15 additions & 17 deletions src/ExtLibeventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,7 @@ public function __construct()
$this->eventBase = event_base_new();
$this->futureTickQueue = new FutureTickQueue();
$this->timerEvents = new SplObjectStorage();

$this->signals = new SignalsHandler(
$this,
function ($signal) {
$this->signalEvents[$signal] = event_new();
event_set($this->signalEvents[$signal], $signal, EV_PERSIST | EV_SIGNAL, $f = function () use ($signal, &$f) {
$this->signals->call($signal);
// Ensure there are two copies of the callable around until it has been executed.
// For more information see: https://bugs.php.net/bug.php?id=62452
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
$g = $f;
$f = $g;
});
event_base_set($this->signalEvents[$signal], $this->eventBase);
event_add($this->signalEvents[$signal]);
}
);
$this->signals = new SignalsHandler($this);

$this->createTimerCallback();
$this->createStreamCallback();
Expand Down Expand Up @@ -178,6 +162,20 @@ public function futureTick($listener)
public function addSignal($signal, $listener)
{
$this->signals->add($signal, $listener);

if (!isset($this->signalEvents[$signal])) {
$this->signalEvents[$signal] = event_new();
event_set($this->signalEvents[$signal], $signal, EV_PERSIST | EV_SIGNAL, $f = function () use ($signal, &$f) {
$this->signals->call($signal);
// Ensure there are two copies of the callable around until it has been executed.
// For more information see: https://bugs.php.net/bug.php?id=62452
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
$g = $f;
$f = $g;
});
event_base_set($this->signalEvents[$signal], $this->eventBase);
event_add($this->signalEvents[$signal]);
}
}

public function removeSignal($signal, $listener)
Expand Down
7 changes: 1 addition & 6 deletions src/SignalsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ fi 8000 nal class SignalsHandler
private $loop;
private $timer;
private $signals = [];
private $on;

public function __construct(LoopInterface $loop, $on)
public function __construct(LoopInterface $loop)
{
$this->loop = $loop;
$this->on = $on;
}

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

if (!isset($this->signals[$signal])) {
$this->signals[$signal] = [];

$on = $this->on;
$on($signal);
}

if (in_array($listener, $this->signals[$signal])) {
Expand Down
26 changes: 13 additions & 13 deletions src/StreamSelectLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,7 @@ public function __construct()
$this->futureTickQueue = new FutureTickQueue();
$this->timers = new Timers();
$this->pcntl = extension_loaded('pcntl');
$this->signals = new SignalsHandler(
$this,
function ($signal) {
\pcntl_signal($signal, $f = function ($signal) use (&$f) {
$this->signals->call($signal);
// Ensure there are two copies of the callable around until it has been executed.
// For more information see: https://bugs.php.net/bug.php?id=62452
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
$g = $f;
$f = $g;
});
}
);
$this->signals = new SignalsHandler($this);
}

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

$first = $this->signals->count($signal) === 0;
$this->signals->add($signal, $listener);

if ($first) {
\pcntl_signal($signal, $f = function ($signal) use (&$f) {
$this->signals->call($signal);
// Ensure there are two copies of the callable around until it has been executed.
// For more information see: https://bugs.php.net/bug.php?id=62452
// Only an issue for PHP 5, this hack can be removed once PHP 5 support has been dropped.
$g = $f;
$f = $g;
});
}
}

public function removeSignal($signal, $listener)
Expand Down
19 changes: 1 addition & 18 deletions tests/SignalsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,49 @@ final class SignalsHandlerTest extends TestCase
public function testEmittedEventsAndCallHandling()
{
$callCount = 0;
$onCount = 0;
$func = function () use (&$callCount) {
$callCount++;
};
$signals = new SignalsHandler(
Factory::create(),
function () use (&$onCount) {
$onCount++;
}
Factory::create()
);

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

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

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

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

$signals->call(SIGUSR1);
$this->assertSame(1, $callCount);
$this->assertSame(1, $onCount);

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

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

$signals->call(SIGUSR2);
$this->assertSame(2, $callCount);
$this->assertSame(2, $onCount);

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

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

$signals->call(SIGUSR2);
$this->assertSame(2, $callCount);
$this->assertSame(2, $onCount);

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

$signals->call(SIGUSR1);
$this->assertSame(2, $callCount);
$this->assertSame(2, $onCount);
}
}
0