1 | <?php |
||
39 | abstract class Mutex extends Component |
||
40 | { |
||
41 | /** |
||
42 | * @var bool whether all locks acquired in this process (i.e. local locks) must be released automatically |
||
43 | * before finishing script execution. Defaults to true. Setting this property to true means that all locks |
||
44 | * acquired in this process must be released (regardless of errors or exceptions). |
||
45 | */ |
||
46 | public $autoRelease = true; |
||
47 | |||
48 | /** |
||
49 | * @var string[] names of the locks acquired by the current PHP process. |
||
50 | */ |
||
51 | private $_locks = []; |
||
52 | |||
53 | |||
54 | /** |
||
55 | * Initializes the Mutex component. |
||
56 | */ |
||
57 | 16 | public function init() |
|
68 | |||
69 | /** |
||
70 | * Acquires a lock by name. |
||
71 | * @param string $name of the lock to be acquired. Must be unique. |
||
72 | * @param int $timeout time (in seconds) to wait for lock to be released. Defaults to zero meaning that method will return |
||
73 | * false immediately in case lock was already acquired. |
||
74 | * @return bool lock acquiring result. |
||
75 | */ |
||
76 | 16 | public function acquire($name, $timeout = 0) |
|
86 | |||
87 | /** |
||
88 | * Releases acquired lock. This method will return false in case the lock was not found. |
||
89 | * @param string $name of the lock to be released. This lock must already exist. |
||
90 | * @return bool lock release result: false in case named lock was not found.. |
||
91 | */ |
||
92 | 13 | public function release($name) |
|
105 | |||
106 | /** |
||
107 | * Executes callback with mutex synchronization. |
||
108 | * |
||
109 | * @param string $name of the lock to be acquired. Must be unique. |
||
110 | * @param int $timeout time (in seconds) to wait for lock to be released. |
||
111 | * @param callable $callback a valid PHP callback that performs the job. Accepts mutex instance as parameter. |
||
112 | * @param bool $throw whether to throw an exception when the lock is not acquired. |
||
113 | * @return mixed result of callback function, or null when the lock is not acquired. |
||
114 | * @throws SyncException when the lock is not acquired. |
||
115 | * @since 2.1 |
||
116 | */ |
||
117 | 9 | public function sync($name, $timeout, callable $callback, $throw = true) |
|
118 | { |
||
119 | 9 | if ($this->acquire($name, $timeout)) { |
|
120 | try { |
||
121 | 3 | $result = call_user_func($callback, $this); |
|
122 | 3 | } finally { |
|
123 | 3 | $this->release($name); |
|
124 | } |
||
125 | 3 | return $result; |
|
126 | } |
||
127 | 6 | if ($throw) { |
|
128 | 3 | throw new SyncException('Cannot acquire the lock.'); |
|
129 | } |
||
130 | 3 | return null; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * This method should be extended by a concrete Mutex implementations. Acquires lock by name. |
||
135 | * @param string $name of the lock to be acquired. |
||
136 | * @param int $timeout time (in seconds) to wait for the lock to be released. |
||
137 | * @return bool acquiring result. |
||
138 | */ |
||
139 | abstract protected function acquireLock($name, $timeout = 0); |
||
140 | |||
141 | /** |
||
142 | * This method should be extended by a concrete Mutex implementations. Releases lock by given name. |
||
143 | * @param string $name of the lock to be released. |
||
144 | * @return bool release result. |
||
145 | */ |
||
146 | abstract protected function releaseLock($name); |
||
147 | } |
||
148 |