All Projects → yiisoft → mutex

yiisoft / mutex

Licence: BSD-3-Clause License
Mutex lock implementation

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to mutex

async
Synchronization and asynchronous computation package for Go
Stars: ✭ 104 (+271.43%)
Mutual labels:  lock, mutex
AtomicKit
Concurrency made simple in Swift.
Stars: ✭ 88 (+214.29%)
Mutual labels:  lock, mutex
go-lock
go-lock is a lock library implementing read-write mutex and read-write trylock without starvation
Stars: ✭ 78 (+178.57%)
Mutual labels:  lock, mutex
run exclusive
⚡🔒 Wait queue for function execution 🔒 ⚡
Stars: ✭ 22 (-21.43%)
Mutual labels:  lock, mutex
mutexsafe
MutexSafe will help you use mutex more effectively. Different mutex for different components are presented. In addition, you can add your own lockers and use within the library.
Stars: ✭ 15 (-46.43%)
Mutual labels:  lock, mutex
distlock
The universal component of distributed locks in golang , support redis and postgresql
Stars: ✭ 60 (+114.29%)
Mutual labels:  lock, mutex
futex
File-based Ruby Mutex
Stars: ✭ 14 (-50%)
Mutual labels:  lock, mutex
yii-gii
Yii code generator extension
Stars: ✭ 27 (-3.57%)
Mutual labels:  yii3
scroll-padlock
🔒 CSS variables-based scrollbars locker, compatible with all reactive frameworks
Stars: ✭ 12 (-57.14%)
Mutual labels:  lock
files
Useful methods to manage files and directories
Stars: ✭ 27 (-3.57%)
Mutual labels:  yii3
cache
PSR-16 compatible cache library
Stars: ✭ 30 (+7.14%)
Mutual labels:  yii3
yii-console
Yii console components
Stars: ✭ 48 (+71.43%)
Mutual labels:  yii3
mongo
Light-weight utilities and declarative schema (mutable mapping) to augment, not replace the Python MongoDB driver.
Stars: ✭ 18 (-35.71%)
Mutual labels:  mutex
slow-down
A centralized Redis-based lock to help you wait on throttled resources
Stars: ✭ 21 (-25%)
Mutual labels:  lock
strings
String helper methods and an inflector
Stars: ✭ 31 (+10.71%)
Mutual labels:  yii3
Bluetooth-Unlock
Simple script to unlock your Linux based Computer using a Bluetooth device when nearby
Stars: ✭ 37 (+32.14%)
Mutual labels:  lock
db-sqlite
SQLite support for Yii
Stars: ✭ 15 (-46.43%)
Mutual labels:  yii3
db-oracle
Oracle Database support for Yii
Stars: ✭ 21 (-25%)
Mutual labels:  yii3
toxic-decorators
Library of Javascript decorators
Stars: ✭ 26 (-7.14%)
Mutual labels:  lock
var-dumper
Helper for dumping variable for debug purposes
Stars: ✭ 13 (-53.57%)
Mutual labels:  yii3

Yii Mutex


Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage

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.

Requirements

  • PHP 7.4 or higher.

Installation

The package could be installed with composer:

composer require yiisoft/mutex --prefer-dist

Usage

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 \Yiisoft\Mutex\Exception\MutexLockedException('Unable to acquire the "critical" mutex.');
}
$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 \Yiisoft\Mutex\Exception\MutexLockedException('Unable to acquire the "critical" mutex.');
}
$newCount = $counter->increase();
$mutex->release();

Mutex drivers

There are some mutex drivers available as separate packages:

If you want to provide your own driver, you need to implement MutexFactoryInterface and MutexInterface. There is ready to extend Mutex, MutexFactory and a RetryAcquireTrait that contains retryAcquire() method implementing the "wait for a lock for a certain time" functionality.

When implementing your own drivers, you need to take care of automatic unlocking. For example using a destructor:

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

and shutdown function:

public function __construct()
{
    register_shutdown_function(function () {
        $this->release();
    });
}

Note that you should not call the exit() or die() functions in the destructor or shutdown function. Since calling these functions in the destructor and shutdown function will prevent all subsequent completion functions from executing.

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:

./vendor/bin/roave-infection-static-analysis-plugin

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

License

The Yii Mutex is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].