88use Malkusch \Lock \Exception \LockAcquireException ;
99use Malkusch \Lock \Exception \LockAcquireTimeoutException ;
1010use Malkusch \Lock \Exception \LockReleaseException ;
11+ use Malkusch \Lock \Util \LockUtil ;
1112use Malkusch \Lock \Util \Loop ;
1213use Malkusch \Lock \Util \PcntlTimeout ;
1314
1617 */
1718class FlockMutex extends AbstractLockMutex
1819{
19- public const INFINITE_TIMEOUT = -1.0 ;
20-
2120 private const STRATEGY_BLOCK = 'block ' ;
22-
2321 private const STRATEGY_PCNTL = 'pcntl ' ;
24-
2522 private const STRATEGY_LOOP = 'loop ' ;
2623
2724 /** @var resource */
2825 private $ fileHandle ;
2926
30- private float $ timeout ;
27+ private float $ acquireTimeout ;
3128
3229 /** @var self::STRATEGY_* */
3330 private $ strategy ;
3431
3532 /**
3633 * @param resource $fileHandle
34+ * @param float $acquireTimeout In seconds
3735 */
38- public function __construct ($ fileHandle , float $ timeout = self :: INFINITE_TIMEOUT )
36+ public function __construct ($ fileHandle , float $ acquireTimeout = \ INF )
3937 {
4038 if (!is_resource ($ fileHandle )) {
4139 throw new \InvalidArgumentException ('The file handle is not a valid resource ' );
4240 }
4341
4442 $ this ->fileHandle = $ fileHandle ;
45- $ this ->timeout = $ timeout ;
43+ $ this ->acquireTimeout = $ acquireTimeout ;
4644 $ this ->strategy = $ this ->determineLockingStrategy ();
4745 }
4846
@@ -51,7 +49,7 @@ public function __construct($fileHandle, float $timeout = self::INFINITE_TIMEOUT
5149 */
5250 private function determineLockingStrategy (): string
5351 {
54- if ($ this ->timeout === self :: INFINITE_TIMEOUT ) {
52+ if ($ this ->acquireTimeout > 100 * 365.25 * 24 * 60 * 60 ) { // 100 years
5553 return self ::STRATEGY_BLOCK ;
5654 }
5755
@@ -62,25 +60,18 @@ private function determineLockingStrategy(): string
6260 return self ::STRATEGY_LOOP ;
6361 }
6462
65- /**
66- * @throws LockAcquireException
67- */
6863 private function lockBlocking (): void
6964 {
7065 if (!flock ($ this ->fileHandle , \LOCK_EX )) {
7166 throw new LockAcquireException ('Failed to lock the file ' );
7267 }
7368 }
7469
75- /**
76- * @throws LockAcquireException
77- * @throws LockAcquireTimeoutException
78- */
7970 private function lockPcntl (): void
8071 {
81- $ timeoutInt = ( int ) ceil ($ this ->timeout );
72+ $ acquireTimeoutInt = LockUtil:: getInstance ()-> castFloatToInt ( ceil ($ this ->acquireTimeout ) );
8273
83- $ timebox = new PcntlTimeout ($ timeoutInt );
74+ $ timebox = new PcntlTimeout ($ acquireTimeoutInt );
8475
8576 try {
8677 $ timebox ->timeBoxed (
@@ -89,14 +80,10 @@ function (): void {
8980 }
9081 );
9182 } catch (DeadlineException $ e ) {
92- throw LockAcquireTimeoutException::create ($ timeoutInt );
83+ throw LockAcquireTimeoutException::create ($ acquireTimeoutInt );
9384 }
9485 }
9586
96- /**
97- * @throws LockAcquireTimeoutException
98- * @throws LockAcquireException
99- */
10087 private function lockBusy (): void
10188 {
10289 $ loop = new Loop ();
@@ -105,12 +92,9 @@ private function lockBusy(): void
10592 if ($ this ->acquireNonBlockingLock ()) {
10693 $ loop ->end ();
10794 }
108- }, $ this ->timeout );
95+ }, $ this ->acquireTimeout );
10996 }
11097
111- /**
112- * @throws LockAcquireException
113- */
11498 private function acquireNonBlockingLock (): bool
11599 {
116100 if (!flock ($ this ->fileHandle , \LOCK_EX | \LOCK_NB , $ wouldBlock )) {
@@ -125,10 +109,6 @@ private function acquireNonBlockingLock(): bool
125109 return true ;
126110 }
127111
128- /**
129- * @throws LockAcquireException
130- * @throws LockAcquireTimeoutException
131- */
132112 #[\Override]
133113 protected function lock (): void
134114 {
0 commit comments