KO
|
EN
gitlite — search
Search
#typescript
#ai-agents
#ai
#dsh-plugin
#deepseek-harness
#open-source
#cli
#claude-code
#codex
#developer-tools
#react
#windows
mutex
★ 33
Open GitHub ↗
Mutex lock implementation
Download README (.md)
Explore Similar Repositories
swift-mutex
:
Cross platform lock backporting the Swift 6 Mutex API to earlier platforms
dyndb-mutex
:
A mutex lock implementation leveraging DynamoDB. Useful for AWS Lambda
Simba
:
Distributed lock service | 分布式锁服务
nats-mutex
:
Nats-mutex is a distributed lock library using NATS JetStream.
milkthisbuffer
:
Mutex/Lock-Free Thread-Safe Buffer in Go. Shell automation in Go. Running and piping shell commands in Go.
// repository documentation
Was this content helpful?
★ 0
(0 ratings)
Select Rating:
★
★
★
★
★
Submit Feedback
Recent Feedback
×
Download README
Do you want to download the
README.md
file for
mutex
?
Download (.md)
<p align="center"> <a href="https://github.com/yiisoft" target="_blank"> <img src="https://yiisoft.github.io/docs/images/yii_logo.svg" height="100px" alt="Yii"> </a> <h1 align="center">Yii Mutex</h1> <br> </p> [](https://packagist.org/packages/yiisoft/mutex) [](https://packagist.org/packages/yiisoft/mutex) [](https://github.com/yiisoft/mutex/actions/workflows/build.yml) [](https://codecov.io/gh/yiisoft/mutex) [](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/mutex/master) [](https://github.com/yiisoft/mutex/actions/workflows/static.yml?query=branch%3Amaster) [](https://shepherd.dev/github/yiisoft/mutex) 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](https://getcomposer.org): ```shell composer require yiisoft/mutex ``` ## General 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: ```php /** @var \Yiisoft\Mutex\Synchronizer $synchronizer */ $newCount = $synchronizer->execute('critical', function () { return $counter->increase(); }, 10); ``` Another way is to manually open and close mutex: ```php /** @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: ```php /** @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: ```php /** @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: - [File](https://github.com/yiisoft/mutex-file) - [PDO MySQL](https://github.com/yiisoft/mutex-pdo-mysql) - [PDO Oracle](https://github.com/yiisoft/mutex-pdo-oracle) - [PDO Postgres](https://github.com/yiisoft/mutex-pdo-pgsql) - [Redis](https://github.com/yiisoft/mutex-redis) 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: ```php public function __destruct() { $this->release(); } ``` and shutdown function: ```php 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. ## Documentation - [Internals](docs/internals.md) If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that. You may also check out other [Yii Community Resources](https://www.yiiframework.com/community). ## License The Yii Mutex is free software. It is released under the terms of the BSD License. Please see [`LICENSE`](./LICENSE.md) for more information. Maintained by [Yii Software](https://www.yiiframework.com/). ## Support the project [](https://opencollective.com/yiisoft) ## Follow updates [](https://www.yiiframework.com/) [](https://twitter.com/yiiframework) [](https://t.me/yii3en) [](https://www.facebook.com/groups/yiitalk) [](https://yiiframework.com/go/slack)