-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactored critical sections
- Loading branch information
Showing
15 changed files
with
293 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection; | ||
|
||
use PetrKnap\Shorts\Exception\NotImplemented; | ||
use Symfony\Component\Lock\LockInterface; | ||
|
||
/** | ||
* @internal please use {@see CriticalSection} | ||
* | ||
* @method static WrappingCriticalSection withLock(LockInterface $lock, bool $isBlocking = true) | ||
* @method static WrappingCriticalSection withLocks(array $locks, bool $isBlocking = true) | ||
*/ | ||
trait CriticalSectionStaticFactory | ||
{ | ||
/** | ||
* @todo refactor it when {@see https://bugs.php.net/bug.php?id=40837} will be fixed | ||
* | ||
* @param array<mixed> $arguments | ||
*/ | ||
public static function __callStatic(string $name, array $arguments): mixed | ||
{ | ||
return match ($name) { | ||
'withLock', 'withLocks' => self::nonCritical(canEnter: true)->$name(...$arguments), | ||
default => NotImplemented::throw("Static method `{$name}`"), | ||
}; | ||
} | ||
|
||
private static function nonCritical(CriticalSection $wrappedCriticalSection = null, bool $canEnter = null): WrappingCriticalSection | ||
{ | ||
return new class ($wrappedCriticalSection, (bool) $canEnter) extends WrappingCriticalSection { | ||
public function __construct( | ||
private readonly CriticalSection|null $wrappedCriticalSection, | ||
private readonly bool $canEnter, | ||
) { | ||
parent::__construct($wrappedCriticalSection, false); | ||
} | ||
|
||
public function enter(): bool | ||
{ | ||
return $this->canEnter; | ||
} | ||
|
||
public function leave(): void | ||
{ | ||
} | ||
|
||
protected function getWrappingReferenceOrNull(): CriticalSection|null | ||
{ | ||
return $this->wrappedCriticalSection; | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.