Abstract Class Yiisoft\Mutex\Mutex
| Inheritance | Yiisoft\Mutex\Mutex |
|---|---|
| Implements | Yiisoft\Mutex\MutexInterface |
| Uses Traits | Yiisoft\Mutex\RetryAcquireTrait |
Provides basic functionality for creating drivers.
See also Yiisoft\Mutex\MutexFactoryInterface.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Mutex\Mutex | |
| __destruct() | Yiisoft\Mutex\Mutex | |
| acquire() | Yiisoft\Mutex\Mutex | |
| release() | Yiisoft\Mutex\Mutex | |
| withRetryDelay() | Returns a new instance with the specified retry delay. | Yiisoft\Mutex\RetryAcquireTrait |
Protected Methods
| Method | Description | Defined By |
|---|---|---|
| acquireLock() | Acquires lock. | Yiisoft\Mutex\Mutex |
| releaseLock() | Releases lock. | Yiisoft\Mutex\Mutex |
Method Details
| public mixed __construct ( string $driverName, string $mutexName ) | ||
| $driverName | string | |
| $mutexName | string | |
public function __construct(string $driverName, string $mutexName)
{
$this->lockName = md5($driverName . $mutexName);
$this->mutexName = $mutexName;
}
| public boolean acquire ( integer $timeout = 0 ) | ||
| $timeout | integer | |
final public function acquire(int $timeout = 0): bool
{
return $this->retryAcquire($timeout, function () use ($timeout): bool {
if (!$this->isCurrentProcessLocked() && $this->acquireLock($timeout)) {
return self::$currentProcessLocks[$this->lockName] = true;
}
return false;
});
}
Acquires lock.
This method should be extended by a concrete Mutex implementations.
| protected abstract boolean acquireLock ( integer $timeout = 0 ) | ||
| $timeout | integer |
Time (in seconds) to wait for lock to be released. Defaults to zero meaning that method will return false immediately in case lock was already acquired. |
| return | boolean |
The acquiring result. |
|---|---|---|
abstract protected function acquireLock(int $timeout = 0): bool;
| public void release ( ) |
final public function release(): void
{
if (!$this->isCurrentProcessLocked()) {
return;
}
if (!$this->releaseLock()) {
throw new MutexReleaseException("Unable to release the \"$this->mutexName\" mutex.");
}
unset(self::$currentProcessLocks[$this->lockName]);
}
Releases lock.
This method should be extended by a concrete Mutex implementations.
| protected abstract boolean releaseLock ( ) | ||
| return | boolean |
The release result. |
|---|---|---|
abstract protected function releaseLock(): bool;
Defined in: Yiisoft\Mutex\RetryAcquireTrait::withRetryDelay()
Returns a new instance with the specified retry delay.
| public self withRetryDelay ( integer $retryDelay ) | ||
| $retryDelay | integer |
Number of milliseconds between each try until specified timeout times out. By default, it is 50 milliseconds - it means that we may try to acquire lock up to 20 times per second. |
public function withRetryDelay(int $retryDelay): self
{
if ($retryDelay < 1) {
throw new InvalidArgumentException(
"Retry delay value must be a positive number greater than zero, \"$retryDelay\" is received.",
);
}
$new = clone $this;
$new->retryDelay = $retryDelay;
return $new;
}
Signup or Login in order to comment.