Skip to content

Commit ad3f16c

Browse files
committed
#19: Added a new exception
- Added `ExecutionOutsideLockException` which can be thrown when the lock is released or times out before the synchronized code finished execution. - Changed `SpinlockMutex` to use the new `ExecutionOutsideLockException` when the synchronized code executes longer than the set timeout.
1 parent 5522f44 commit ad3f16c

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace malkusch\lock\exception;
4+
5+
/**
6+
* This exception should be thrown when for example the lock is released or
7+
* times out before the synchronized code finished execution.
8+
*
9+
* @see \malkusch\lock\mutex\SpinlockMutex::unlock()
10+
*
11+
* @author Petr Levtonov <[email protected]>
12+
* @license WTFPL
13+
*/
14+
class ExecutionOutsideLockException extends LockReleaseException
15+
{
16+
}

classes/mutex/SpinlockMutex.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace malkusch\lock\mutex;
44

5-
use malkusch\lock\util\Loop;
6-
use malkusch\lock\exception\LockReleaseException;
5+
use malkusch\lock\exception\ExecutionOutsideLockException;
76
use malkusch\lock\exception\LockAcquireException;
7+
use malkusch\lock\exception\LockReleaseException;
8+
use malkusch\lock\util\Loop;
89

910
/**
1011
* Spinlock implementation.
@@ -79,11 +80,13 @@ protected function unlock()
7980
$elapsed = microtime(true) - $this->acquired;
8081
if ($elapsed >= $this->timeout) {
8182
$message = sprintf(
82-
"The code executed for %d seconds. But the timeout is %d seconds.",
83+
"The code executed for %.2f seconds. But the timeout is %d " .
84+
"seconds. The last %.2f seconds were executed outside the lock.",
8385
$elapsed,
84-
$this->timeout
86+
$this->timeout,
87+
$elapsed - $this->timeout
8588
);
86-
throw new LockReleaseException($message);
89+
throw new ExecutionOutsideLockException($message);
8790
}
8891

8992
/*

0 commit comments

Comments
 (0)