8000 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 checking signal watchers and remove circular reference
  • Loading branch information
clue committed Feb 6, 2018
commit 3e4421bf6437e5aeda762d9d452e1799c8190fd4
4 changes: 2 additions & 2 deletions src/ExtEventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +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);
$this->signals = new SignalsHandler();

$this->createTimerCallback();
$this->createStreamCallback();
Expand Down Expand Up @@ -181,7 +181,7 @@ public function run()
$flags = EventBase::LOOP_ONCE;
if (!$this->running || !$this->futureTickQueue->isEmpty()) {
$flags |= EventBase::LOOP_NONBLOCK;
} elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count()) {
} elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count() && $this->signals->isEmpty()) {
break;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ExtLibevLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct()
$this->loop = new EventLoop();
$this->futureTickQueue = new FutureTickQueue();
$this->timerEvents = new SplObjectStorage();
$this->signals = new SignalsHandler($this);
$this->signals = new SignalsHandler();
}

public function addReadStream($stream, $listener)
Expand Down Expand Up @@ -181,7 +181,7 @@ public function run()
$flags = EventLoop::RUN_ONCE;
if (!$this->running || !$this->futureTickQueue->isEmpty()) {
$flags |= EventLoop::RUN_NOWAIT;
} elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count()) {
} elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count() && $this->signals->isEmpty()) {
break;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ExtLibeventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct()
$this->eventBase = event_base_new();
$this->futureTickQueue = new FutureTickQueue();
$this->timerEvents = new SplObjectStorage();
$this->signals = new SignalsHandler($this);
$this->signals = new SignalsHandler();

$this->createTimerCallback();
$this->createStreamCallback();
Expand Down Expand Up @@ -199,7 +199,7 @@ public function run()
$flags = EVLOOP_ONCE;
if (!$this->running || !$this->futureTickQueue->isEmpty()) {
$flags |= EVLOOP_NONBLOCK;
} elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count()) {
} elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count() && $this->signals->isEmpty()) {
break;
}

Expand Down
24 changes: 5 additions & 19 deletions src/SignalsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,10 @@
*/
final class SignalsHandler
{
private $loop;
private $timer;
private $signals = [];

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

public function add($signal, $listener)
{
if (empty($this->signals) && $this->timer === null) {
/**
* Timer to keep the loop alive as long as there are any signal handlers registered
*/
$this->timer = $this->loop->addPeriodicTimer(300, function () {});
}

if (!isset($this->signals[$signal])) {
$this->signals[$signal] = [];
}
Expand All @@ -48,11 +34,6 @@ public function remove($signal, $listener)
if (isset($this->signals[$signal]) && \count($this->signals[$signal]) === 0) {
unset($this->signals[$signal]);
}

if (empty($this->signals) && $this->timer instanceof TimerInterface) {
$this->loop->cancelTimer($this->timer);
$this->timer = null;
}
}

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

return \count($this->signals[$signal]);
}

public function isEmpty()
{
return !$this->signals;
}
}
6 changes: 3 additions & 3 deletions src/StreamSelectLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct()
$this->futureTickQueue = new FutureTickQueue();
$this->timers = new Timers();
$this->pcntl = extension_loaded('pcntl');
$this->signals = new SignalsHandler($this);
$this->signals = new SignalsHandler();
}

public function addReadStream($stream, $listener)
Expand Down Expand Up @@ -200,8 +200,8 @@ public function run()
$timeout = $timeout > PHP_INT_MAX ? PHP_INT_MAX : (int)$timeout;
}

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

// There's nothing left to do ...
Expand Down
5 changes: 1 addition & 4 deletions tests/SignalsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace React\Tests\EventLoop;

use React\EventLoop\Factory;
use React\EventLoop\SignalsHandler;

final class SignalsHandlerTest extends TestCase
Expand All @@ -13,9 +12,7 @@ public function testEmittedEventsAndCallHandling()
$func = function () use (&$callCount) {
$callCount++;
};
$signals = new SignalsHandler(
Factory::create()
);
$signals = new SignalsHandler();

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

Expand Down
0