1 | <?php |
||
33 | abstract class Mutex extends Component |
||
34 | { |
||
35 | /** |
||
36 | * @var bool whether all locks acquired in this process (i.e. local locks) must be released automatically |
||
37 | * before finishing script execution. Defaults to true. Setting this property to true means that all locks |
||
38 | * acquired in this process must be released (regardless of errors or exceptions). |
||
39 | */ |
||
40 | public $autoRelease = true; |
||
41 | |||
42 | /** |
||
43 | * @var string[] names of the locks acquired by the current PHP process. |
||
44 | */ |
||
45 | private $_locks = []; |
||
46 | |||
47 | |||
48 | /** |
||
49 | * Initializes the Mutex component. |
||
50 | */ |
||
51 | 34 | public function init() |
|
52 | { |
||
53 | 34 | if ($this->autoRelease) { |
|
54 | 34 | $locks = &$this->_locks; |
|
55 | 34 | register_shutdown_function(function () use (&$locks) { |
|
56 | foreach ($locks as $lock) { |
||
57 | $this->release($lock); |
||
58 | } |
||
59 | 34 | }); |
|
60 | } |
||
61 | 34 | } |
|
62 | |||
63 | /** |
||
64 | * Acquires a lock by name. |
||
65 | * @param string $name of the lock to be acquired. Must be unique. |
||
66 | * @param int $timeout time (in seconds) to wait for lock to be released. Defaults to zero meaning that method will return |
||
67 | * false immediately in case lock was already acquired. |
||
68 | * @return bool lock acquiring result. |
||
69 | */ |
||
70 | 34 | public function acquire($name, $timeout = 0) |
|
71 | { |
||
72 | 34 | if (!in_array($name, $this->_locks, true) && $this->acquireLock($name, $timeout)) { |
|
73 | 34 | $this->_locks[] = $name; |
|
74 | |||
75 | 34 | return true; |
|
76 | } |
||
77 | |||
78 | 22 | return false; |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Releases acquired lock. This method will return false in case the lock was not found. |
||
83 | * @param string $name of the lock to be released. This lock must already exist. |
||
84 | * @return bool lock release result: false in case named lock was not found.. |
||
85 | */ |
||
86 | 33 | public function release($name) |
|
87 | { |
||
88 | 33 | if ($this->releaseLock($name)) { |
|
89 | 33 | $index = array_search($name, $this->_locks); |
|
90 | 33 | if ($index !== false) { |
|
91 | 33 | unset($this->_locks[$index]); |
|
92 | } |
||
93 | |||
94 | 33 | return true; |
|
95 | } |
||
96 | |||
97 | 21 | return false; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * This method should be extended by a concrete Mutex implementations. Acquires lock by name. |
||
102 | * @param string $name of the lock to be acquired. |
||
103 | * @param int $timeout time (in seconds) to wait for the lock to be released. |
||
104 | * @return bool acquiring result. |
||
105 | */ |
||
106 | abstract protected function acquireLock($name, $timeout = 0); |
||
107 | |||
108 | /** |
||
109 | * This method should be extended by a concrete Mutex implementations. Releases lock by given name. |
||
110 | * @param string $name of the lock to be released. |
||
111 | * @return bool release result. |
||
112 | */ |
||
113 | abstract protected function releaseLock($name); |
||
114 | } |
||
115 |