1
0
mirror of https://github.com/danog/file.git synced 2024-12-02 09:17:57 +01:00
file/lib/Internal/EioPoll.php
2017-06-20 00:31:58 -05:00

48 lines
920 B
PHP

<?php
namespace Amp\File\Internal;
use Amp\Loop;
class EioPoll {
/** @var resource */
private static $stream;
/** @var string */
private $watcher;
/** @var int */
private $requests = 0;
public function __construct() {
if (!self::$stream) {
\eio_init();
self::$stream = \eio_get_event_stream();
}
$this->watcher = Loop::onReadable(self::$stream, function () {
while (\eio_npending()) {
\eio_poll();
}
});
Loop::disable($this->watcher);
}
public function listen() {
if ($this->requests++ === 0) {
Loop::enable($this->watcher);
}
}
public function done() {
if (--$this->requests === 0) {
Loop::disable($this->watcher);
}
}
public function __destruct() {
Loop::cancel($this->watcher);
}
}