0 follower

Abstract Class Yiisoft\Mutex\Mutex

InheritanceYiisoft\Mutex\Mutex
ImplementsYiisoft\Mutex\MutexInterface
Uses TraitsYiisoft\Mutex\RetryAcquireTrait

Provides basic functionality for creating drivers.

See also Yiisoft\Mutex\MutexFactoryInterface.

Protected Methods

Hide inherited methods

Method Description Defined By
acquireLock() Acquires lock. Yiisoft\Mutex\Mutex
releaseLock() Releases lock. Yiisoft\Mutex\Mutex

Method Details

Hide inherited methods

__construct() public method

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;
}

            
__destruct() public method

public mixed __destruct ( )

                final public function __destruct()
{
    $this->release();
}

            
acquire() public method

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;
    });
}

            
acquireLock() protected abstract method

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;

            
release() public method

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]);
}

            
releaseLock() protected abstract method

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;

            
withRetryDelay() public method

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;
}