/
WrappingCriticalSection.php
76 lines (66 loc) · 2.16 KB
/
WrappingCriticalSection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?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 WrappingCriticalSection<T> */
public function withLock(LockInterface $lock, bool $isBlocking = true): WrappingCriticalSection
{
return new SymfonyLockCriticalSection($this->getWrappingReferenceOrNull(), $lock, $isBlocking);
}
/**
* @param array<LockInterface> $locks
*
* @return WrappingCriticalSection<T>
*/
public function withLocks(array $locks, bool $isBlocking = true): WrappingCriticalSection
{
$locks = array_reverse($locks); // reverse array to keep order of locks during wrapping
$instance = $this;
foreach ($locks as $lock) {
$instance = $instance->withLock($lock, $isBlocking);
}
return $instance;
}
/**
* @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;
}
}