Final Class Yiisoft\Mutex\SimpleMutex
| Inheritance | Yiisoft\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
| 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
| public mixed __construct ( Yiisoft\Mutex\MutexFactoryInterface $mutexFactory ) | ||
| $mutexFactory | Yiisoft\Mutex\MutexFactoryInterface | |
public function __construct(MutexFactoryInterface $mutexFactory)
{
$this->mutexFactory = $mutexFactory;
}
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;
}
Signup or Login in order to comment.