1 | <?php |
||
19 | class Mutex |
||
20 | { |
||
21 | /** |
||
22 | * Lock implementor |
||
23 | * |
||
24 | * @var LockInterface |
||
25 | */ |
||
26 | protected $lockImplementor; |
||
27 | |||
28 | /** |
||
29 | * Name of lock |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $name; |
||
34 | |||
35 | /** |
||
36 | * Lock counter to protect against recursive deadlock |
||
37 | * |
||
38 | * @var integer |
||
39 | */ |
||
40 | protected $counter = 0; |
||
41 | |||
42 | /** |
||
43 | * @param string $name |
||
44 | * @param LockInterface $lockImplementor |
||
45 | */ |
||
46 | 155 | public function __construct($name, LockInterface $lockImplementor) |
|
51 | |||
52 | /** |
||
53 | * @param int|null $timeout |
||
54 | * @return bool |
||
55 | */ |
||
56 | 127 | public function acquireLock($timeout = null) |
|
67 | |||
68 | /** |
||
69 | * @return bool |
||
70 | */ |
||
71 | 127 | public function releaseLock() |
|
84 | |||
85 | /** |
||
86 | * Try to release any obtained locks when object is destroyed |
||
87 | * |
||
88 | * This is a safe guard for cases when your php script dies unexpectedly. |
||
89 | * It's not guaranteed it will work either. |
||
90 | * |
||
91 | * You should not depend on __destruct() to release your locks, |
||
92 | * instead release them with `$released = $this->releaseLock()`A |
||
93 | * and check `$released` if lock was properly released |
||
94 | * @throws UnrecoverableMutexException |
||
95 | 155 | */ |
|
96 | public function __destruct() |
||
108 | |||
109 | /** |
||
110 | * Check if Mutex is acquired |
||
111 | * |
||
112 | * @return bool |
||
113 | 155 | */ |
|
114 | public function isAcquired() |
||
118 | |||
119 | /** |
||
120 | * @return bool |
||
121 | 84 | */ |
|
122 | public function isLocked() |
||
126 | } |
||
127 |