mirror of
https://github.com/danog/file.git
synced 2024-12-02 09:17:57 +01:00
Pass current loop driver to EioPoll
This commit is contained in:
parent
c0bdd0f55b
commit
1a6558c113
@ -7,6 +7,7 @@ use Amp\File\Driver;
|
|||||||
use Amp\File\FilesystemException;
|
use Amp\File\FilesystemException;
|
||||||
use Amp\File\Internal;
|
use Amp\File\Internal;
|
||||||
use Amp\File\StatCache;
|
use Amp\File\StatCache;
|
||||||
|
use Amp\Loop;
|
||||||
use Amp\Promise;
|
use Amp\Promise;
|
||||||
use Amp\Success;
|
use Amp\Success;
|
||||||
|
|
||||||
@ -23,9 +24,9 @@ final class EioDriver implements Driver
|
|||||||
/** @var Internal\EioPoll */
|
/** @var Internal\EioPoll */
|
||||||
private $poll;
|
private $poll;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct(Loop\Driver $driver)
|
||||||
{
|
{
|
||||||
$this->poll = new Internal\EioPoll;
|
$this->poll = new Internal\EioPoll($driver);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function openFile(string $path, string $mode): Promise
|
public function openFile(string $path, string $mode): Promise
|
||||||
|
@ -17,32 +17,39 @@ final class EioPoll
|
|||||||
/** @var int */
|
/** @var int */
|
||||||
private $requests = 0;
|
private $requests = 0;
|
||||||
|
|
||||||
public function __construct()
|
/** @var Loop\Driver */
|
||||||
|
private $driver;
|
||||||
|
|
||||||
|
public function __construct(Loop\Driver $driver)
|
||||||
{
|
{
|
||||||
|
$this->driver = $driver;
|
||||||
|
|
||||||
if (!self::$stream) {
|
if (!self::$stream) {
|
||||||
\eio_init();
|
\eio_init();
|
||||||
self::$stream = \eio_get_event_stream();
|
self::$stream = \eio_get_event_stream();
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->watcher = Loop::onReadable(self::$stream, static function (): void {
|
$this->watcher = $this->driver->onReadable(self::$stream, static function (): void {
|
||||||
while (\eio_npending()) {
|
while (\eio_npending()) {
|
||||||
\eio_poll();
|
\eio_poll();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Loop::disable($this->watcher);
|
$this->driver->disable($this->watcher);
|
||||||
|
|
||||||
Loop::setState(self::class, new class($this->watcher) {
|
$this->driver->setState(self::class, new class($this->watcher, $driver) {
|
||||||
private $watcher;
|
private $watcher;
|
||||||
|
private $driver;
|
||||||
|
|
||||||
public function __construct(string $watcher)
|
public function __construct(string $watcher, Loop\Driver $driver)
|
||||||
{
|
{
|
||||||
$this->watcher = $watcher;
|
$this->watcher = $watcher;
|
||||||
|
$this->driver = $driver;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destruct()
|
public function __destruct()
|
||||||
{
|
{
|
||||||
Loop::cancel($this->watcher);
|
$this->driver->cancel($this->watcher);
|
||||||
|
|
||||||
// Ensure there are no active operations anymore. This is a safe-guard as some operations might not be
|
// Ensure there are no active operations anymore. This is a safe-guard as some operations might not be
|
||||||
// finished on loop exit due to not being yielded. This also ensures a clean shutdown for these if PHP
|
// finished on loop exit due to not being yielded. This also ensures a clean shutdown for these if PHP
|
||||||
@ -55,7 +62,7 @@ final class EioPoll
|
|||||||
public function listen(Promise $promise): void
|
public function listen(Promise $promise): void
|
||||||
{
|
{
|
||||||
if ($this->requests++ === 0) {
|
if ($this->requests++ === 0) {
|
||||||
Loop::enable($this->watcher);
|
$this->driver->enable($this->watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
$promise->onResolve(\Closure::fromCallable([$this, 'done']));
|
$promise->onResolve(\Closure::fromCallable([$this, 'done']));
|
||||||
@ -64,7 +71,7 @@ final class EioPoll
|
|||||||
private function done(): void
|
private function done(): void
|
||||||
{
|
{
|
||||||
if (--$this->requests === 0) {
|
if (--$this->requests === 0) {
|
||||||
Loop::disable($this->watcher);
|
$this->driver->disable($this->watcher);
|
||||||
}
|
}
|
||||||
|
|
||||||
\assert($this->requests >= 0);
|
\assert($this->requests >= 0);
|
||||||
|
@ -55,7 +55,7 @@ function createDefaultDriver(): Driver
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (EioDriver::isSupported()) {
|
if (EioDriver::isSupported()) {
|
||||||
return new EioDriver;
|
return new EioDriver($driver);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (\defined("AMP_WORKER")) { // Prevent spawning infinite workers.
|
if (\defined("AMP_WORKER")) { // Prevent spawning infinite workers.
|
||||||
|
Loading…
Reference in New Issue
Block a user