Skip to content

Commit

Permalink
Change approach
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Dec 6, 2024
1 parent 38b195d commit d4b6aed
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
24 changes: 15 additions & 9 deletions src/EventLoop/Internal/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ abstract class AbstractDriver implements Driver

private readonly \Closure $interruptCallback;
private readonly \Closure $queueCallback;
/** @var \Closure(): ((\Closure(): mixed)|bool|null) */
/** @var \Closure(): ((\Closure(): mixed)|bool) */
private readonly \Closure $runCallback;

private readonly \stdClass $internalSuspensionMarker;
Expand Down Expand Up @@ -89,13 +89,13 @@ public function __construct()
$this->interruptCallback = $this->setInterrupt(...);
$this->queueCallback = $this->queue(...);
$this->runCallback =
/** @return (\Closure(): mixed)|bool|null */
function (): \Closure|bool|null {
/** @return (\Closure(): mixed)|bool */
function (): \Closure|bool {
if ($this->fiber->isTerminated()) {
$this->createLoopFiber();
}

return $this->fiber->isStarted() ? $this->fiber->resume() : $this->fiber->start();
return ($this->fiber->isStarted() ? $this->fiber->resume() : $this->fiber->start()) ?? $this->fiber->getReturn();
};
}

Expand Down Expand Up @@ -493,9 +493,11 @@ private function tick(bool $previousIdle): void
$this->dispatch($blocking);
}

private function invokeCallbacks(): void
private function invokeCallbacks(): bool
{
$didWork = false;
while (!$this->microtaskQueue->isEmpty() || !$this->callbackQueue->isEmpty()) {
$didWork = true;
/** @noinspection PhpUnhandledExceptionInspection */
$yielded = $this->callbackFiber->isStarted()
? $this->callbackFiber->resume()
Expand All @@ -509,6 +511,7 @@ private function invokeCallbacks(): void
$this->invokeInterrupt();
}
}
return $didWork;
}

/**
Expand All @@ -534,28 +537,31 @@ private function invokeInterrupt(): void

private function createLoopFiber(): void
{
$this->fiber = new \Fiber(function (): void {
$this->fiber = new \Fiber(function (): bool {
$this->stopped = false;

// Invoke microtasks if we have some
$this->invokeCallbacks();
$didWork = $this->invokeCallbacks();

/** @psalm-suppress RedundantCondition $this->stopped may be changed by $this->invokeCallbacks(). */
while (!$this->stopped) {
if ($this->interrupt) {
$didWork = true;
$this->invokeInterrupt();
}

if ($this->isEmpty()) {
return;
return $didWork;
}

$previousIdle = $this->idle;
$this->idle = true;

$this->tick($previousIdle);
$this->invokeCallbacks();
$didWork = $this->invokeCallbacks();
}

return $didWork;
});
}

Expand Down
9 changes: 6 additions & 3 deletions src/EventLoop/Internal/DriverSuspension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class DriverSuspension implements Suspension
private bool $deadMain = false;

/**
* @param \Closure(): ((\Closure(): mixed)|bool|null) $run
* @param \Closure(): ((\Closure(): mixed)|bool) $run
* @param \WeakMap<object, \WeakReference<DriverSuspension>> $suspensions
*/
public function __construct(
Expand Down Expand Up @@ -117,8 +117,11 @@ public function suspend(): mixed
// Awaiting from {main}.
$result = ($this->run)();

while ($this->pending && $result === false) {

if ($this->pending && \is_bool($result)) {

Check failure on line 120 in src/EventLoop/Internal/DriverSuspension.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

RedundantCondition

src/EventLoop/Internal/DriverSuspension.php:120:13: RedundantCondition: Operand of type true is always truthy (see https://psalm.dev/122)

Check failure on line 120 in src/EventLoop/Internal/DriverSuspension.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

RedundantCondition

src/EventLoop/Internal/DriverSuspension.php:120:13: RedundantCondition: Operand of type true is always truthy (see https://psalm.dev/122)

Check failure on line 120 in src/EventLoop/Internal/DriverSuspension.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

RedundantCondition

src/EventLoop/Internal/DriverSuspension.php:120:13: RedundantCondition: Operand of type true is always truthy (see https://psalm.dev/122)
do {
while (\gc_collect_cycles());
$result = ($this->run)();
} while ($this->pending && $result === true);

Check failure on line 124 in src/EventLoop/Internal/DriverSuspension.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

RedundantCondition

src/EventLoop/Internal/DriverSuspension.php:124:22: RedundantCondition: Type true for $this->pending is never falsy (see https://psalm.dev/122)

Check failure on line 124 in src/EventLoop/Internal/DriverSuspension.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

RedundantCondition

src/EventLoop/Internal/DriverSuspension.php:124:22: RedundantCondition: Type true for $this->pending is never falsy (see https://psalm.dev/122)

Check failure on line 124 in src/EventLoop/Internal/DriverSuspension.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

RedundantCondition

src/EventLoop/Internal/DriverSuspension.php:124:22: RedundantCondition: Type true for $this->pending is never falsy (see https://psalm.dev/122)
}

/** @psalm-suppress RedundantCondition $this->pending should be changed when resumed. */
Expand Down

0 comments on commit d4b6aed

Please sign in to comment.