Fisrt version of mutex package was released. This package provides mutex implementation and allows mutual execution of concurrent processes in order to prevent "race conditions". This is achieved by using a "lock" mechanism. Each possibly concurrent processes cooperates by acquiring a lock before accessing the corresponding data.
There are multiple ways you can use the package. You can execute a callback in a synchronized mode i.e. only a single instance of the callback is executed at the same time:
/** @var \Yiisoft\Mutex\Synchronizer $synchronizer */
$newCount = $synchronizer->execute('critical', function () {
return $counter->increase();
}, 10);
Another way is to manually open and close mutex:
/** @var \Yiisoft\Mutex\SimpleMutex $simpleMutex */
if (!$simpleMutex->acquire('critical', 10)) {
throw new \RuntimeException('Unable to acquire mutex "critical".');
}
$newCount = $counter->increase();
$simpleMutex->release('critical');
It could be done on lower level:
/** @var \Yiisoft\Mutex\MutexFactoryInterface $mutexFactory */
$mutex = $mutexFactory->createAndAcquire('critical', 10);
$newCount = $counter->increase();
$mutex->release();
And if you want even more control, you can acquire mutex manually:
/** @var \Yiisoft\Mutex\MutexFactoryInterface $mutexFactory */
$mutex = $mutexFactory->create('critical');
if (!$mutex->acquire(10)) {
throw new \RuntimeException('Unable to acquire mutex "critical".');
}
$newCount = $counter->increase();
$mutex->release();
Additionally, the following adapter packages were released and are available:
As usual for Yii3 packages, this set has a high test coverage, high MSI score, statically checked and well documented.