Skip to content

PeclEvLoop: Add new PeclEvLoop (PECL ext-ev) #97

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

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ In addition to the interface there are the following implementations provided:
([github](https://github.com/m4rw3r/php-libev)). It supports the same
backends as libevent.

* `PeclEvLoop`: This uses the `libev` pecl extension that is documented on
([php.net](http://php.net/manual/en/book.ev.php)). See
([bitbucket](https://bitbucket.org/osmanov/pecl-ev/overview)) for source.

* `ExtEventLoop`: This uses the `event` pecl extension. It supports the same
backends as libevent.

Expand Down
2 changes: 2 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public static function create()
return new LibEventLoop();
} elseif (class_exists('libev\EventLoop', false)) {
return new LibEvLoop;
} elseif (class_exists('EvLoop', false)) {
return new PeclEvLoop;
} elseif (class_exists('EventBase', false)) {
return new ExtEventLoop;
}
Expand Down
195 changes: 195 additions & 0 deletions src/PeclEvLoop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<?php

namespace React\EventLoop;

use Ev;
use EvLoop;
use React\EventLoop\Tick\FutureTickQueue;
use React\EventLoop\Timer\Timer;
use React\EventLoop\Timer\TimerInterface;
use SplObjectStorage;

/**
* @see https://bitbucket.org/osmanov/pecl-ev/overview
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A short description would help?

*/
class PeclEvLoop implements LoopInterface
{
private $loop;
private $futureTickQueue;
private $timers;
private $readStreams = [];
private $writeStreams = [];
private $running;

public function __construct()
{
$this->loop = new EvLoop();
$this->futureTickQueue = new FutureTickQueue($this);
$this->timers = new SplObjectStorage();
}

public function addReadStream($stream, callable $listener)
{
$key = (int) $stream;

if (isset($this->readStreams[$key])) {
return;
}

$callback = $this->getStreamListenerClosure($stream, $listener);
$event = $this->loop->io($stream, Ev::READ, $callback);
$this->readStreams[$key] = $event;
}

/**
* @param resource $stream
* @param callable $listener
*
* @return \Closure
*/
private function getStreamListenerClosure($stream, callable $listener) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick: Line break before { (PSR-2)

return function () use ($stream, $listener) {
call_user_func($listener, $stream, $this);
};
}

public function addWriteStream($stream, callable $listener)
{
$key = (int) $stream;

if (isset($this->writeStreams[$key])) {
return;
}

$callback = $this->getStreamListenerClosure($stream, $listener);
$event = $this->loop->io($stream, Ev::WRITE, $callback);
$this->writeStreams[$key] = $event;
}

public function removeReadStream($stream)
{
$key = (int) $stream;

if (!isset($this->readStreams[$key])) {
return;
}

$this->readStreams[$key]->stop();
unset($this->readStreams[$key]);
}

public function removeWriteStream($stream)
{
$key = (int) $stream;

if (!isset($this->writeStreams[$key])) {
return;
}

$this->writeStreams[$key]->stop();
unset($this->writeStreams[$key]);
}

public function removeStream($stream)
{
$this->removeReadStream($stream);
$this->removeWriteStream($stream);
}

public function addTimer($interval, callable $callback)
{
$timer = new Timer($this, $interval, $callback, false);

$callback = function () use ($timer) {
call_user_func($timer->getCallback(), $timer);

if ($this->isTimerActive($timer)) {
$this->cancelTimer($timer);
}
};

$event = $this->loop->timer($timer->getInterval(), 0.0, $callback);
$this->timers->attach($timer, $event);

return $timer;
}

public function addPeriodicTimer($interval, callable $callback)
{
$timer = new Timer($this, $interval, $callback, true);

$callback = function () use ($timer) {
call_user_func($timer->getCallback(), $timer);
};

//reschedule callback should be NULL to utilize $offset and $interval params
$event = $this->loop->periodic($interval, $interval, NULL, $callback);
$this->timers->attach($timer, $event);

return $timer;
}

public function cancelTimer(TimerInterface $timer)
{
if (!isset($this->timers[$timer])) {
return;
}

$event = $this->timers[$timer];
$event->stop();
$this->timers->detach($timer);
}

public function isTimerActive(TimerInterface $timer)
{
return $this->timers->contains($timer);
}

public function futureTick(callable $listener)
{
$this->futureTickQueue->add($listener);
}

public function run()
{
$this->running = true;

while ($this->running) {
$this->futureTickQueue->tick();

$hasPendingCallbacks = !$this->futureTickQueue->isEmpty();
$wasJustStopped = !$this->running;
$nothingLeftToDo = !$this->readStreams && !$this->writeStreams && !$this->timers->count();

$flags = Ev::RUN_ONCE;
if ($wasJustStopped || $hasPendingCallbacks) {
$flags |= Ev::RUN_NOWAIT;
} elseif ($nothingLeftToDo) {
break;
}

$this->loop->run($flags);
}
}

public function stop()
{
$this->running = false;
}

public function __destruct()
{
/** @var TimerInterface $timer */
foreach($this->timers as $timer) {
$this->cancelTimer($timer);
}

foreach($this->readStreams as $key => $stream) {
$this->removeReadStream($key);
}

foreach($this->writeStreams as $key => $stream) {
$this->removeWriteStream($key);
}
}
}
17 changes: 17 additions & 0 deletions tests/PeclEvLoopTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace React\Tests\EventLoop;

use React\EventLoop\PeclEvLoop;

class PeclEvLoopTest extends AbstractLoopTest
{
public function createLoop()
{
if (!class_exists('EvLoop')) {
$this->markTestSkipped('pecl-ev tests skipped because ext-ev is not installed.');
}

return new PeclEvLoop();
}
}
14 changes: 14 additions & 0 deletions tests/Timer/AbstractTimerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

namespace React\Tests\EventLoop\Timer;

use React\EventLoop\LoopInterface;
use React\EventLoop\Timer\Timer;
use React\Tests\EventLoop\TestCase;

abstract class AbstractTimerTest extends TestCase
{
/**
* @return LoopInterface
*/
abstract public function createLoop();

public function testAddTimer()
Expand Down Expand Up @@ -94,4 +99,13 @@ public function testMinimumIntervalOneMicrosecond()

$this->assertEquals(0.000001, $timer->getInterval());
}

public function testCancelNonexistentTimer()
{
$loop = $this->createLoop();

$timer = new Timer($loop, 1, function(){});

$loop->cancelTimer($timer);
}
}
17 changes: 17 additions & 0 deletions tests/Timer/PeclEvLoopTimerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace React\Tests\EventLoop\Timer;

use React\EventLoop\PeclEvLoop;

class PeclEvLoopTimerTest extends AbstractTimerTest
{
public function createLoop()
{
if (!class_exists('EvLoop')) {
$this->markTestSkipped('pecl-ev tests skipped because ext-ev is not installed.');
}

return new PeclEvLoop();
}
}
3 changes: 3 additions & 0 deletions travis-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ if [[ "$TRAVIS_PHP_VERSION" != "hhvm" &&
# install 'event' PHP extension
echo "yes" | pecl install event

# install 'ev' PHP extension
echo "yes" | pecl install ev

# install 'libevent' PHP extension (does not support php 7)
if [[ "$TRAVIS_PHP_VERSION" != "7.0" &&
"$TRAVIS_PHP_VERSION" != "7.1" ]]; then
Expand Down