-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCriticalSection.php
55 lines (47 loc) · 1.4 KB
/
CriticalSection.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
<?php
declare(strict_types=1);
namespace PetrKnap\CriticalSection;
use Throwable;
abstract class CriticalSection
{
use CriticalSectionStaticFactory;
protected function __construct(
protected readonly bool $isBlocking,
) {
}
/**
* @phpstan-ignore-next-line Template type T ... is not referenced in a parameter.
*
* @template T of mixed
*
* @param (callable(mixed ...$args): T)|callable $criticalSection
* @param mixed ...$args will be forwarded to {@link $criticalSection}
*
* @return T|null returned by {@link $criticalSection} or null when it is occupied (non-blocking mode only)
*
* @throws Exception\CouldNotEnterCriticalSection
* @throws Exception\CouldNotLeaveCriticalSection
* @throws Throwable from {@link $criticalSection}
*/
public function __invoke(callable $criticalSection, mixed ...$args): mixed
{
if ($this->enter() === false) {
return null;
}
try {
return $criticalSection(...$args);
} finally {
$this->leave();
}
}
/**
* @return bool false if it is occupied (non-blocking mode only)
*
* @throws Exception\CouldNotEnterCriticalSection
*/
abstract protected function enter(): bool;
/**
* @throws Exception\CouldNotLeaveCriticalSection
*/
abstract protected function leave(): void;
}