1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of ninja-mutex. |
4
|
|
|
* |
5
|
|
|
* (C) Kamil Dziedzic <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace NinjaMutex; |
11
|
|
|
|
12
|
|
|
use Exception; |
13
|
|
|
use NinjaMutex\Lock\LockInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Mutex |
17
|
|
|
* |
18
|
|
|
* @author Kamil Dziedzic <[email protected]> |
19
|
|
|
*/ |
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) |
48
|
|
|
{ |
49
|
54 |
|
$this->name = $name; |
50
|
54 |
|
$this->lockImplementor = $lockImplementor; |
51
|
54 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param int|null $timeout |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
26 |
|
public function acquireLock($timeout = null) |
58
|
|
|
{ |
59
|
26 |
|
if ($this->counter > 0 || |
60
|
26 |
|
$this->lockImplementor->acquireLock($this->name, $timeout)) { |
61
|
26 |
|
$this->counter++; |
62
|
|
|
|
63
|
26 |
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return bool |
71
|
|
|
*/ |
72
|
26 |
|
public function releaseLock() |
73
|
|
|
{ |
74
|
26 |
|
if ($this->counter > 0) { |
75
|
26 |
|
$this->counter--; |
76
|
26 |
|
if ($this->counter > 0 || |
77
|
26 |
|
$this->lockImplementor->releaseLock($this->name)) { |
78
|
26 |
|
return true; |
79
|
|
|
} |
80
|
|
|
$this->counter++; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return false; |
84
|
|
|
} |
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() |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
54 |
|
if ($this->isAcquired()) { |
100
|
12 |
|
$this->releaseLock(); |
101
|
12 |
|
} |
102
|
54 |
|
} catch (Exception $e) { |
|
|
|
|
103
|
|
|
} |
104
|
54 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check if Mutex is acquired |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
54 |
|
public function isAcquired() |
112
|
|
|
{ |
113
|
54 |
|
return $this->counter > 0; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return bool |
118
|
|
|
*/ |
119
|
22 |
|
public function isLocked() |
120
|
|
|
{ |
121
|
22 |
|
return $this->lockImplementor->isLocked($this->name); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|