|
2 | 2 |
|
3 | 3 | namespace malkusch\lock\mutex; |
4 | 4 |
|
| 5 | +use malkusch\lock\exception\ExecutionOutsideLockException; |
5 | 6 | use malkusch\lock\exception\LockAcquireException; |
6 | 7 | use malkusch\lock\exception\LockReleaseException; |
| 8 | +use malkusch\lock\exception\TimeoutException; |
| 9 | +use malkusch\lock\util\Loop; |
| 10 | +use malkusch\lock\util\PcntlTimeout; |
7 | 11 |
|
8 | 12 | /** |
9 | 13 | * Flock() based mutex implementation. |
|
15 | 19 | */ |
16 | 20 | class FlockMutex extends LockMutex |
17 | 21 | { |
18 | | - |
| 22 | + const INFINITE_TIMEOUT = -1; |
| 23 | + |
| 24 | + /** |
| 25 | + * @internal |
| 26 | + */ |
| 27 | + const STRATEGY_BLOCK = 1; |
| 28 | + |
| 29 | + /** |
| 30 | + * @internal |
| 31 | + */ |
| 32 | + const STRATEGY_PCNTL = 2; |
| 33 | + |
| 34 | + /** |
| 35 | + * @internal |
| 36 | + */ |
| 37 | + const STRATEGY_BUSY = 3; |
| 38 | + |
19 | 39 | /** |
20 | 40 | * @var resource $fileHandle The file handle. |
21 | 41 | */ |
22 | 42 | private $fileHandle; |
23 | | - |
| 43 | + |
| 44 | + /** |
| 45 | + * @var int |
| 46 | + */ |
| 47 | + private $timeout; |
| 48 | + |
| 49 | + /** |
| 50 | + * @var int |
| 51 | + */ |
| 52 | + private $strategy; |
| 53 | + |
| 54 | + /** |
| 55 | + * @var float|null |
| 56 | + */ |
| 57 | + private $acquired; |
| 58 | + |
24 | 59 | /** |
25 | 60 | * Sets the file handle. |
26 | 61 | * |
27 | 62 | * @param resource $fileHandle The file handle. |
28 | 63 | * @throws \InvalidArgumentException The file handle is not a valid resource. |
29 | 64 | */ |
30 | | - public function __construct($fileHandle) |
| 65 | + public function __construct($fileHandle, $timeout = self::INFINITE_TIMEOUT) |
31 | 66 | { |
32 | 67 | if (!is_resource($fileHandle)) { |
33 | 68 | throw new \InvalidArgumentException("The file handle is not a valid resource."); |
34 | 69 | } |
| 70 | + |
35 | 71 | $this->fileHandle = $fileHandle; |
| 72 | + $this->timeout = $timeout; |
| 73 | + $this->strategy = $this->determineLockingStrategy(); |
36 | 74 | } |
37 | | - |
| 75 | + |
| 76 | + private function determineLockingStrategy() |
| 77 | + { |
| 78 | + if ($this->timeout == self::INFINITE_TIMEOUT) { |
| 79 | + return self::STRATEGY_BLOCK; |
| 80 | + } |
| 81 | + |
| 82 | + if (PcntlTimeout::isSupported()) { |
| 83 | + return self::STRATEGY_PCNTL; |
| 84 | + } |
| 85 | + |
| 86 | + return self::STRATEGY_BUSY; |
| 87 | + } |
| 88 | + |
38 | 89 | /** |
39 | | - * @internal |
| 90 | + * @throws LockAcquireException |
40 | 91 | */ |
41 | | - protected function lock() |
| 92 | + private function lockBlocking() |
42 | 93 | { |
43 | 94 | if (!flock($this->fileHandle, LOCK_EX)) { |
44 | 95 | throw new LockAcquireException("Failed to lock the file."); |
45 | 96 | } |
46 | 97 | } |
47 | | - |
| 98 | + |
48 | 99 | /** |
49 | | - * @internal |
| 100 | + * @throws LockAcquireException |
| 101 | + * @throws TimeoutException |
| 102 | + */ |
| 103 | + private function lockPcntl() |
| 104 | + { |
| 105 | + $timebox = new PcntlTimeout($this->timeout); |
| 106 | + |
| 107 | + $timebox->timeBoxed(function () { |
| 108 | + $this->lockBlocking(); |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * @throws TimeoutException |
| 114 | + * @throws LockAcquireException |
| 115 | + */ |
| 116 | + private function lockBusy() |
| 117 | + { |
| 118 | + $loop = new Loop($this->timeout); |
| 119 | + $loop->execute(function () use ($loop) { |
| 120 | + if ($this->acquireNonBlockingLock()) { |
| 121 | + $this->acquired = \microtime(true); |
| 122 | + $loop->end(); |
| 123 | + } |
| 124 | + }); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @return bool |
| 129 | + * @throws LockAcquireException |
| 130 | + */ |
| 131 | + private function acquireNonBlockingLock() |
| 132 | + { |
| 133 | + if (!flock($this->fileHandle, LOCK_EX | LOCK_NB, $wouldblock)) { |
| 134 | + if ($wouldblock) { |
| 135 | + /* |
| 136 | + * Another process holds the lock. |
| 137 | + */ |
| 138 | + return false; |
| 139 | + } |
| 140 | + throw new LockAcquireException("Failed to lock the file."); |
| 141 | + } |
| 142 | + return true; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @throws LockAcquireException |
| 147 | + * @throws TimeoutException |
| 148 | + */ |
| 149 | + protected function lock() |
| 150 | + { |
| 151 | + switch ($this->strategy) { |
| 152 | + case self::STRATEGY_BLOCK: |
| 153 | + $this->lockBlocking(); |
| 154 | + return; |
| 155 | + case self::STRATEGY_PCNTL: |
| 156 | + $this->lockPcntl(); |
| 157 | + return; |
| 158 | + case self::STRATEGY_BUSY: |
| 159 | + $this->lockBusy(); |
| 160 | + return; |
| 161 | + } |
| 162 | + |
| 163 | + throw new \RuntimeException("Unknown strategy '{$this->strategy}'.'"); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * @throws LockReleaseException |
| 168 | + * @throws ExecutionOutsideLockException |
50 | 169 | */ |
51 | 170 | protected function unlock() |
52 | 171 | { |
53 | 172 | if (!flock($this->fileHandle, LOCK_UN)) { |
54 | 173 | throw new LockReleaseException("Failed to unlock the file."); |
55 | 174 | } |
| 175 | + |
| 176 | + if ($this->acquired !== null) { |
| 177 | + $elapsed_time = \microtime(true) - $this->acquired; |
| 178 | + if ($elapsed_time > $this->timeout) { |
| 179 | + throw ExecutionOutsideLockException::create($elapsed_time, $this->timeout); |
| 180 | + } |
| 181 | + } |
56 | 182 | } |
57 | 183 | } |
0 commit comments