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 | public function __construct($name, LockInterface $lockImplementor) |
||
51 | |||
52 | /** |
||
53 | * @param int|null $timeout |
||
54 | * @return bool |
||
55 | */ |
||
56 | public function acquireLock($timeout = null) |
||
67 | |||
68 | /** |
||
69 | * @return bool |
||
70 | */ |
||
71 | 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 | */ |
||
95 | public function __destruct() |
||
107 | |||
108 | /** |
||
109 | * Check if Mutex is acquired |
||
110 | * |
||
111 | * @return bool |
||
112 | */ |
||
113 | public function isAcquired() |
||
117 | |||
118 | /** |
||
119 | * @return bool |
||
120 | */ |
||
121 | public function isLocked() |
||
125 | } |
||
126 |