Skip to content

Commit 4677638

Browse files
committed
[3.x] Add Io\Poll Event Loop
1 parent 0b45df3 commit 4677638

5 files changed

Lines changed: 277 additions & 15 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jobs:
1111
strategy:
1212
matrix:
1313
php:
14+
- 8.6
1415
- 8.5
1516
- 8.4
1617
- 8.3
@@ -29,7 +30,8 @@ jobs:
2930
coverage: ${{ matrix.php < 8.0 && 'xdebug' || 'pcov' }}
3031
ini-file: development
3132
ini-values: disable_functions='' # do not disable PCNTL functions on PHP < 8.1
32-
extensions: sockets, pcntl, event, ${{ matrix.php < 8.0 && 'ev-1.1.5' || 'ev' }}
33+
extensions: sockets, pcntl
34+
# extensions: sockets, pcntl, event, ${{ matrix.php < 8.0 && 'ev-1.1.5' || 'ev' }}
3335
env:
3436
fail-fast: true # fail step if any extension can not be installed
3537
- run: composer install
@@ -45,6 +47,7 @@ jobs:
4547
strategy:
4648
matrix:
4749
php:
50+
- 8.6
4851
- 8.5
4952
- 8.4
5053
- 8.3
@@ -63,11 +66,11 @@ jobs:
6366
coverage: ${{ matrix.php < 8.0 && 'xdebug' || 'pcov' }}
6467
ini-file: development
6568
extensions: sockets, pcntl
66-
- name: Install ext-uv
67-
run: |
68-
sudo apt-get update -q && sudo apt-get install libuv1-dev
69-
echo "yes" | sudo pecl install ${{ matrix.php >= 8.0 && 'uv-0.3.0' || 'uv-0.2.4' }}
70-
php -m | grep -q uv || echo "extension=uv.so" >> "$(php -r 'echo php_ini_loaded_file();')"
69+
# - name: Install ext-uv
70+
# run: |
71+
# sudo apt-get update -q && sudo apt-get install libuv1-dev
72+
# echo "yes" | sudo pecl install ${{ matrix.php >= 8.0 && 'uv-0.3.0' || 'uv-0.2.4' }}
73+
# php -m | grep -q uv || echo "extension=uv.so" >> "$(php -r 'echo php_ini_loaded_file();')"
7174
- run: composer install
7275
- run: vendor/bin/phpunit --coverage-text
7376
if: ${{ matrix.php >= 7.3 }}
@@ -81,6 +84,7 @@ jobs:
8184
strategy:
8285
matrix:
8386
php:
87+
- 8.6
8488
- 8.5
8589
- 8.4
8690
- 8.3

‎src/IoPollLoop.php‎

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
<?php
2+
3+
namespace React\EventLoop;
4+
5+
use React\EventLoop\Tick\FutureTickQueue;
6+
use React\EventLoop\Timer\Timer;
7+
use React\EventLoop\Timer\Timers;
8+
use React\EventLoop\Watcher\StreamWatcher;
9+
use SplObjectStorage;
10+
11+
final class IoPollLoop implements LoopInterface
12+
{
13+
private bool $running = false;
14+
private \Io\Poll\Context $context;
15+
private FutureTickQueue $futureTickQueue;
16+
private Timers $timers;
17+
private bool $pcntl;
18+
private SignalsHandler $signals;
19+
/** @var array<\Io\Poll\Watcher> */
20+
private array $watchers = [];
21+
22+
public function __construct()
23+
{
24+
$this->context = new \Io\Poll\Context();
25+
$this->futureTickQueue = new FutureTickQueue();
26+
$this->timers = new Timers();
27+
$this->pcntl = \function_exists('pcntl_signal') && \function_exists('pcntl_signal_dispatch');;
28+
$this->signals = new SignalsHandler();
29+
}
30+
31+
public function addReadStream($stream, $listener)
32+
{
33+
$this->manageStream($stream, \Io\Poll\Event::Read, $listener);
34+
}
35+
36+
public function addWriteStream($stream, $listener)
37+
{
38+
$this->manageStream($stream, \Io\Poll\Event::Write, $listener);
39+
}
40+
41+
public function removeReadStream($stream)
42+
{
43+
$this->manageStream($stream, \Io\Poll\Event::Read);
44+
}
45+
46+
public function removeWriteStream($stream)
47+
{
48+
$this->manageStream($stream, \Io\Poll\Event::Write);
49+
}
50+
51+
public function addTimer($interval, $callback)
52+
{
53+
$timer = new Timer($interval, $callback, false);
54+
55+
$this->timers->add($timer);
56+
57+
return $timer;
58+
}
59+
60+
public function addPeriodicTimer($interval, $callback)
61+
{
62+
$timer = new Timer($interval, $callback, true);
63+
64+
$this->timers->add($timer);
65+
66+
return $timer;
67+
}
68+
69+
public function cancelTimer(TimerInterface $timer)
70+
{
71+
$this->timers->cancel($timer);
72+
}
73+
74+
public function futureTick($listener)
75+
{
76+
$this->futureTickQueue->add($listener);
77+
}
78+
79+
public function addSignal($signal, $listener)
80+
{
81+
if ($this->pcntl === false) {
82+
throw new \BadMethodCallException('Event loop feature "signals" isn\'t supported by the "StreamSelectLoop"');
83+
}
84+
85+
$first = $this->signals->count($signal) === 0;
86+
$this->signals->add($signal, $listener);
87+
88+
if ($first) {
89+
\pcntl_signal($signal, [$this->signals, 'call']);
90+
}
91+
}
92+
93+
public function removeSignal($signal, $listener)
94+
{
95+
if (!$this->signals->count($signal)) {
96+
return;
97+
}
98+
99+
$this->signals->remove($signal, $listener);
100+
101+
if ($this->signals->count($signal) === 0) {
102+
\pcntl_signal($signal, \SIG_DFL);
103+
}
104+
}
105+
106+
public function run()
107+
{
108+
$this->running = true;
109+
110+
while ($this->running) {
111+
$this->futureTickQueue->tick();
112+
113+
$this->timers->tick();
114+
115+
// Future-tick queue has pending callbacks ...
116+
if (!$this->futureTickQueue->isEmpty()) {
117+
$duration = \Time\Duration::fromSeconds(0);
118+
119+
// There is a pending timer, only block until it is due ...
120+
} elseif ($scheduledAt = $this->timers->getFirst()) {
121+
$timeout = $scheduledAt - $this->timers->getTime();
122+
if ($timeout < 0) {
123+
$timeout = 0;
124+
}
125+
126+
$seconds = (int)$timeout;
127+
$nanoseconds = (int)(($timeout - $seconds) * 1_000_000_000);
128+
129+
$duration = \Time\Duration::fromSeconds($seconds, $nanoseconds);
130+
131+
// The only possible event is stream or signal activity, so wait forever ...
132+
} elseif ($this->watchers || !$this->signals->isEmpty()) {
133+
$duration = null;
134+
135+
// There's nothing left to do ...
136+
} else {
137+
break;
138+
}
139+
140+
try {
141+
foreach ($this->context->wait($duration) as $watcher) {
142+
$stream = $watcher->getHandle()->getStream();
143+
$streamWatcher = $watcher->getData();
144+
$triggeredEvents = $watcher->getTriggeredEvents();
145+
146+
if ($streamWatcher->readListener !== null && in_array(\Io\Poll\Event::Read, $triggeredEvents)) {
147+
\call_user_func($streamWatcher->readListener, $stream);
148+
}
149+
150+
if ($streamWatcher->writeListener !== null && in_array(\Io\Poll\Event::Write, $triggeredEvents)) {
151+
\call_user_func($streamWatcher->writeListener, $stream);
152+
}
153+
}
154+
} catch (\Io\Poll\FailedPollWaitException $failedPollWaitException) {
155+
if ($failedPollWaitException->getCode() === \Io\Poll\FailedPollWaitException::ERROR_INTERRUPTED) {
156+
\pcntl_signal_dispatch();
157+
} else {
158+
throw $failedPollWaitException;
159+
}
160+
}
161+
}
162+
}
163+
164+
public function stop()
165+
{
166+
$this->running = false;
167+
}
168+
169+
private function manageStream($stream, \Io\Poll\Event $event, ?callable $listener = null)
170+
{
171+
$key = (int) $stream;
172+
if (!array_key_exists($key, $this->watchers)) {
173+
if ($listener === null) {
174+
return;
175+
}
176+
177+
$streamWatcher = new StreamWatcher($key);
178+
$this->updateStreamWatcherListener($streamWatcher, $event, $listener);
179+
$handle = new \StreamPollHandle($stream);
180+
$watcher = $this->context->add($handle, [$event], $streamWatcher);
181+
$this->watchers[$key] = $watcher;
182+
183+
return;
184+
}
185+
186+
if (!isset($watcher)) {
187+
$watcher = $this->watchers[$key];
188+
}
189+
190+
if (!$watcher->isActive()) {
191+
$watcher->remove();
192+
unset($this->watchers[$key]);
193+
194+
return;
195+
}
196+
197+
$events = $watcher->getWatchedEvents();
198+
$events = array_filter($events, static fn (\Io\Poll\Event $watchedEvent)=> $watchedEvent === $event);
199+
if ($listener !== null) {
200+
$events[] = $event;
201+
}
202+
$this->updateStreamWatcherListener($watcher->getData(), $event, $listener);
203+
204+
if (count($events) > 0 && ($watcher->getData()->readListener !== null || $watcher->getData()->writeListener !== null)) {
205+
$watcher->modifyEvents($events);
206+
207+
return;
208+
}
209+
210+
$watcher->remove();
211+
unset($this->watchers[$key]);
212+
}
213+
214+
private function updateStreamWatcherListener(StreamWatcher $streamWatcher, \Io\Poll\Event $event, ?callable $listener = null): void
215+
{
216+
if ($event === \Io\Poll\Event::Read) {
217+
$streamWatcher->readListener = $listener;
218+
} elseif ($event === \Io\Poll\Event::Write) {
219+
$streamWatcher->writeListener = $listener;
220+
}
221+
}
222+
}

‎src/Loop.php‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -238,16 +238,20 @@ public static function stop()
238238
private static function create()
239239
{
240240
// @codeCoverageIgnoreStart
241-
if (\function_exists('uv_loop_new')) {
242-
return new ExtUvLoop();
243-
}
244-
245-
if (\class_exists('EvLoop', false)) {
246-
return new ExtEvLoop();
247-
}
241+
// if (\function_exists('uv_loop_new')) {
242+
// return new ExtUvLoop();
243+
// }
244+
//
245+
// if (\class_exists('EvLoop', false)) {
246+
// return new ExtEvLoop();
247+
// }
248+
//
249+
// if (\class_exists('EventBase', false)) {
250+
// return new ExtEventLoop();
251+
// }
248252

249-
if (\class_exists('EventBase', false)) {
250-
return new ExtEventLoop();
253+
if (\class_exists('Io\Poll\Context', false)) {
254+
return new IoPollLoop();
251255
}
252256

253257
return new StreamSelectLoop();

‎src/Watcher/StreamWatcher.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace React\EventLoop\Watcher;
4+
5+
final class StreamWatcher
6+
{
7+
public function __construct(
8+
public readonly int $key,
9+
/** @var ?callable */
10+
public $readListener = null,
11+
/** @var ?callable */
12+
public $writeListener = null,
13+
) {
14+
}
15+
}

‎tests/IoPollLoopTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace React\Tests\EventLoop;
4+
5+
use React\EventLoop\IoPollLoop;
6+
7+
class IoPollLoopTest extends \React\Tests\EventLoop\AbstractLoopTest
8+
{
9+
public function createLoop()
10+
{
11+
if (!\class_exists('Io\Poll\Context', false)) {
12+
$this->markTestSkipped('IOPollLoop tests skipped because IO Poll is not available.');
13+
}
14+
15+
return new IoPollLoop();
16+
}
17+
}

0 commit comments

Comments
 (0)