-
-
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.
Merge pull request #2 from petrknap/multilock-support
Implemented wrapping of critical sections
- Loading branch information
Showing
20 changed files
with
292 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.bat text eol=crlf |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env bash | ||
DIR="$(realpath "${BASH_SOURCE%/*}")" | ||
|
||
docker build "${DIR}/.." -t petrknap/php-critical-section:latest |
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 @@ | ||
docker build %~dp0/.. -t petrknap/php-critical-section:latest |
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,7 @@ | ||
#!/usr/bin/env bash | ||
DIR="$(realpath "${BASH_SOURCE%/*}")" | ||
|
||
docker run --rm -ti \ | ||
-v "${DIR}/..:/app" \ | ||
petrknap/php-critical-section:latest \ | ||
$@ |
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,4 @@ | ||
docker run --rm -ti ^ | ||
-v %~dp0/..:/app ^ | ||
petrknap/php-critical-section:latest ^ | ||
%* |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,23 @@ | ||
<?php declare(strict_types=1); | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection; | ||
|
||
use Symfony\Component\Lock\LockInterface; | ||
|
||
/** @template T */ | ||
final class CriticalSection | ||
{ | ||
public static function withLock(LockInterface $lock, bool $isBlocking = true): CriticalSectionInterface | ||
/** @return NonCriticalSection<T> */ | ||
public static function create(): NonCriticalSection | ||
{ | ||
return new NonCriticalSection(); | ||
} | ||
|
||
/** @return SymfonyLockCriticalSection<T> */ | ||
public static function withLock(LockInterface $lock, bool $isBlocking = true): SymfonyLockCriticalSection | ||
{ | ||
return new SymfonyLockCriticalSection($lock, $isBlocking); | ||
return self::create()->withLock($lock, $isBlocking); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
<?php declare(strict_types=1); | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Exception; | ||
|
||
|
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
<?php declare(strict_types=1); | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Exception; | ||
|
||
|
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
<?php declare(strict_types=1); | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection\Exception; | ||
|
||
|
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,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @extends WrappingCriticalSection<T> | ||
*/ | ||
final class NonCriticalSection extends WrappingCriticalSection | ||
{ | ||
/** | ||
* @internal Use {@link CriticalSection::create()} | ||
* | ||
* @param CriticalSectionInterface<T>|null $wrappedCriticalSection | ||
*/ | ||
public function __construct( | ||
private ?CriticalSectionInterface $wrappedCriticalSection = null, | ||
private bool $canEnter = true, | ||
) { | ||
parent::__construct($wrappedCriticalSection); | ||
} | ||
|
||
protected function enter(): bool | ||
{ | ||
return $this->canEnter; | ||
} | ||
|
||
protected function leave(): void | ||
{ | ||
} | ||
|
||
/** @return CriticalSectionInterface<T>|null */ | ||
protected function getWrappingReferenceOrNull(): ?CriticalSectionInterface | ||
{ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection; | ||
|
||
use PetrKnap\CriticalSection\Exception\CouldNotEnterCriticalSection; | ||
use PetrKnap\CriticalSection\Exception\CouldNotLeaveCriticalSection; | ||
use Symfony\Component\Lock\LockInterface; | ||
|
||
/** | ||
* @template T | ||
* | ||
* @implements CriticalSectionInterface<T> | ||
*/ | ||
abstract class WrappingCriticalSection implements CriticalSectionInterface | ||
{ | ||
/** @param CriticalSectionInterface<T>|null $wrappedCriticalSection */ | ||
protected function __construct( | ||
private ?CriticalSectionInterface $wrappedCriticalSection, | ||
) { | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function __invoke(callable $criticalSection) | ||
{ | ||
if ($this->enter() === false) { | ||
return null; | ||
} | ||
try { | ||
if ($this->wrappedCriticalSection) { | ||
return ($this->wrappedCriticalSection)(static fn () => $criticalSection()); | ||
} | ||
return $criticalSection(); | ||
} finally { | ||
$this->leave(); | ||
} | ||
} | ||
|
||
/** @return SymfonyLockCriticalSection<T> */ | ||
public function withLock(LockInterface $lock, bool $isBlocking = true): SymfonyLockCriticalSection | ||
{ | ||
return new SymfonyLockCriticalSection($this->getWrappingReferenceOrNull(), $lock, $isBlocking); | ||
} | ||
|
||
/** | ||
* @return bool false if it is occupied (non-blocking mode only) | ||
* | ||
* @throws CouldNotEnterCriticalSection | ||
*/ | ||
abstract protected function enter(): bool; | ||
|
||
/** @throws CouldNotLeaveCriticalSection */ | ||
abstract protected function leave(): void; | ||
|
||
/** @return CriticalSectionInterface<T>|null */ | ||
protected function getWrappingReferenceOrNull(): ?CriticalSectionInterface | ||
{ | ||
return $this; | ||
} | ||
} |
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,38 @@ | ||
<?php /** @noinspection PhpUnhandledExceptionInspection */ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PetrKnap\CriticalSection; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class NonCriticalSectionTest extends TestCase | ||
{ | ||
/** @dataProvider dataEntersCriticalSectionWhenCanEnter */ | ||
public function testEntersCriticalSectionWhenCanEnter(bool $canEnter): void | ||
{ | ||
$section = new NonCriticalSection(canEnter: $canEnter); | ||
|
||
if ( | ||
$section(function () use ($canEnter) { | ||
self::assertTrue($canEnter); | ||
return true; | ||
}) === null | ||
) { | ||
self::assertFalse($canEnter); | ||
} | ||
} | ||
|
||
public static function dataEntersCriticalSectionWhenCanEnter(): array | ||
{ | ||
return [ | ||
'yes' => [true], | ||
'no' => [false], | ||
]; | ||
} | ||
|
||
public function testDoesNotUseSelfAsWrappingReference(): void | ||
{ | ||
self::markTestIncomplete(); | ||
} | ||
} |
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.