-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPerformTrait.php
More file actions
36 lines (30 loc) · 890 Bytes
/
PerformTrait.php
File metadata and controls
36 lines (30 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
namespace Fervo\AdvisoryLocker;
trait PerformTrait
{
abstract public function acquire(string $name);
abstract public function release(string $name);
public function performLocked(string $name, callable $callable)
{
$this->acquire($name);
try {
$callable();
} finally {
$this->release($name);
}
}
public function performSpinlocked(string $name, callable $callable, int $waitMillis = 100, int $retries = 5)
{
$waitMicros = $waitMillis * 1000;
do {
try {
$this->performLocked($name, $callable);
return;
} catch (Exception\AcquireFailedException $e) {
$retries--;
usleep($waitMicros);
}
} while ($retries >= 0);
throw new Exception\AcquireFailedException();
}
}