0 follower

Final Class Yiisoft\Mutex\SimpleMutex

InheritanceYiisoft\Mutex\SimpleMutex

Simplest way to use mutex:

$mutex = new \Yiisoft\Mutex\SimpleMutex(new MyMutexFactory());

if (!$mutex->acquire('critical_logic', 1000)) {
    throw new \Yiisoft\Mutex\Exception\MutexLockedException('Unable to acquire the "critical_logic" mutex.');
}

// ...
// business logic execution
// ...

$mutex->release();

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Mutex\SimpleMutex
acquire() Acquires a lock with a given name. Yiisoft\Mutex\SimpleMutex
release() Releases a lock with a given name. Yiisoft\Mutex\SimpleMutex

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( Yiisoft\Mutex\MutexFactoryInterface $mutexFactory )
$mutexFactory Yiisoft\Mutex\MutexFactoryInterface

                public function __construct(MutexFactoryInterface $mutexFactory)
{
    $this->mutexFactory = $mutexFactory;
}

            
acquire() public method

Acquires a lock with a given name.

public boolean acquire ( string $name, integer $timeout 0 )
$name string

Name of the mutex to acquire.

$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

Whether a lock is acquired.

                public function acquire(string $name, int $timeout = 0): bool
{
    $mutex = $this->mutexFactory->create($name);
    if ($mutex->acquire($timeout)) {
        $this->acquired[$name] = $mutex;
        return true;
    }
    return false;
}

            
release() public method

Releases a lock with a given name.

public void release ( string $name )
$name string

                public function release(string $name): void
{
    if (!isset($this->acquired[$name])) {
        return;
    }
    $this->acquired[$name]->release();
    unset($this->acquired[$name]);
}