Skip to content

Commit

Permalink
Update Psalm and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Mar 12, 2024
1 parent a404df9 commit c88e224
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
"revolt/event-loop": "^1 || ^0.2"
},
"require-dev": {
"ext-shmop": "*",
"ext-sysvmsg": "*",
"phpunit/phpunit": "^9",
"amphp/phpunit-util": "^3",
"amphp/php-cs-fixer-config": "^2",
"psalm/phar": "^5.4"
"psalm/phar": "5.23"
},
"autoload": {
"psr-4": {
Expand Down
35 changes: 16 additions & 19 deletions src/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use SplObjectStorage;
use function Amp\async;
use function Amp\Future\awaitAll;
use function register_shutdown_function;

/**
* A handle on an acquired lock from a synchronization object.
Expand All @@ -15,7 +14,7 @@
*/
final class Lock
{
private static \Fiber $testFiber;
private static ?\Fiber $testFiber = null;

private static ?\SplObjectStorage $pendingOperations = null;

Expand All @@ -34,17 +33,15 @@ public function __construct(\Closure $release)

private static function setupPendingOperations(): SplObjectStorage
{
if (self::$pendingOperations === null) {
self::$pendingOperations = new SplObjectStorage();

register_shutdown_function(static function () {
while (self::$pendingOperations->count() > 0) {
awaitAll(self::$pendingOperations);
}
});
}
$pending = new SplObjectStorage();

\register_shutdown_function(static function () use ($pending): void {
while ($pending->count() > 0) {
awaitAll($pending);
}
});

return self::$pendingOperations;
return $pending;
}

/**
Expand Down Expand Up @@ -73,9 +70,9 @@ public function release(): void
if ($this->isForceClosed()) {
$future = async($release);

self::$pendingOperations = self::setupPendingOperations();
self::$pendingOperations->attach($future);
$future->finally(fn () => self::$pendingOperations->detach($future));
$pending = self::$pendingOperations ??= self::setupPendingOperations();
$pending->attach($future);
$future->finally(fn () => $pending->detach($future));
} else {
$release();
}
Expand All @@ -94,17 +91,17 @@ public function __destruct()

private function isForceClosed(): bool
{
self::$testFiber ??= new \Fiber(function () {
$fiber = self::$testFiber ??= new \Fiber(function () {
while (true) {
\Fiber::suspend();
}
});

try {
if (self::$testFiber->isStarted()) {
self::$testFiber->resume();
if ($fiber->isStarted()) {
$fiber->resume();
} else {
self::$testFiber->start();
$fiber->start();
}

return false;
Expand Down

0 comments on commit c88e224

Please sign in to comment.