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