1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Mutex; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
use function in_array; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The Mutex component allows mutual execution of concurrent processes in order to prevent "race conditions". |
13
|
|
|
* |
14
|
|
|
* This is achieved by using a "lock" mechanism. Each possibly concurrent thread cooperates by acquiring |
15
|
|
|
* a lock before accessing the corresponding data. |
16
|
|
|
* |
17
|
|
|
* Usage example: |
18
|
|
|
* |
19
|
|
|
* ``` |
20
|
|
|
* if ($mutex->acquire($mutexName)) { |
21
|
|
|
* // business logic execution |
22
|
|
|
* } else { |
23
|
|
|
* // execution is blocked! |
24
|
|
|
* } |
25
|
|
|
* ``` |
26
|
|
|
* |
27
|
|
|
* This is a base class, which should be extended in order to implement the actual lock mechanism. |
28
|
|
|
*/ |
29
|
|
|
abstract class Mutex implements MutexInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string[] Names of the locks acquired by the current PHP process. |
33
|
|
|
*/ |
34
|
|
|
private array $locks = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Mutex constructor. |
38
|
|
|
* |
39
|
|
|
* @param bool $autoRelease Whether all locks acquired in this process (i.e. local locks) must be released |
40
|
|
|
* automatically before finishing script execution. Defaults to true. Setting this property |
41
|
|
|
* to true means that all locks acquired in this process must be released (regardless of |
42
|
|
|
* errors or exceptions). |
43
|
|
|
*/ |
44
|
10 |
|
public function __construct(bool $autoRelease = true) |
45
|
|
|
{ |
46
|
10 |
|
if ($autoRelease) { |
47
|
|
|
$locks = &$this->locks; |
48
|
|
|
register_shutdown_function(function () use (&$locks) { |
49
|
|
|
/** |
50
|
|
|
* @var string $lock |
51
|
|
|
*/ |
52
|
|
|
foreach ($locks as $lock) { |
53
|
|
|
$this->release($lock); |
54
|
|
|
} |
55
|
|
|
}); |
56
|
|
|
} |
57
|
10 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Acquires a lock by name. |
61
|
|
|
* |
62
|
|
|
* @param string $name Name of the lock to be acquired. Must be unique. |
63
|
|
|
* @param int $timeout Time (in seconds) to wait for lock to be released. Defaults to zero meaning that method |
64
|
|
|
* will return false immediately in case lock was already acquired. |
65
|
|
|
* |
66
|
|
|
* @return MutexLockInterface |
67
|
|
|
*/ |
68
|
7 |
|
public function acquire(string $name, int $timeout = 0): MutexLockInterface |
69
|
|
|
{ |
70
|
7 |
|
if (!in_array($name, $this->locks, true) && $this->acquireLock($name, $timeout)) { |
71
|
7 |
|
$this->locks[] = $name; |
72
|
|
|
|
73
|
7 |
|
return new MutexLock($this, $name); |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
throw new MutexLockedException(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Releases acquired lock. This method will return false in case the lock was not found. |
81
|
|
|
* |
82
|
|
|
* @param string $name Name of the lock to be released. This lock must already exist. |
83
|
|
|
*/ |
84
|
6 |
|
public function release(string $name): void |
85
|
|
|
{ |
86
|
6 |
|
if ($this->releaseLock($name)) { |
87
|
3 |
|
$index = array_search($name, $this->locks, true); |
88
|
3 |
|
if ($index !== false) { |
89
|
3 |
|
unset($this->locks[$index]); |
90
|
|
|
} |
91
|
|
|
|
92
|
3 |
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
3 |
|
throw new RuntimeException(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* This method should be extended by a concrete Mutex implementations. Acquires lock by name. |
100
|
|
|
* |
101
|
|
|
* @param string $name Name of the lock to be acquired. |
102
|
|
|
* @param int $timeout Time (in seconds) to wait for the lock to be released. |
103
|
|
|
* |
104
|
|
|
* @return bool Acquiring result. |
105
|
|
|
*/ |
106
|
|
|
abstract protected function acquireLock(string $name, int $timeout = 0): bool; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* This method should be extended by a concrete Mutex implementations. Releases lock by given name. |
110
|
|
|
* |
111
|
|
|
* @param string $name Name of the lock to be released. |
112
|
|
|
* |
113
|
|
|
* @return bool Release result. |
114
|
|
|
*/ |
115
|
|
|
abstract protected function releaseLock(string $name): bool; |
116
|
|
|
} |
117
|
|
|
|