{"id":14966344,"url":"https://github.com/yiisoft/mutex","last_synced_at":"2025-10-14T22:17:03.915Z","repository":{"uuid":"156996600","full_name":"yiisoft/mutex","owner":"yiisoft","description":"Mutex lock implementation","archived":false,"fork":false,"pushed_at":"2024-04-10T02:43:32.000Z","size":139,"stargazers_count":33,"open_issues_count":1,"forks_count":6,"subscribers_count":21,"default_branch":"master","last_synced_at":"2024-04-15T03:05:30.274Z","etag":null,"topics":["hacktoberfest","lock","mutex","yii3"],"latest_commit_sha":null,"homepage":"https://www.yiiframework.com/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yiisoft.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"open_collective":"yiisoft","github":["yiisoft"]}},"created_at":"2018-11-10T15:59:00.000Z","updated_at":"2024-04-17T04:44:05.394Z","dependencies_parsed_at":"2024-01-11T14:13:59.499Z","dependency_job_id":"973f507f-b5b4-4337-9918-8ba7ed6868ee","html_url":"https://github.com/yiisoft/mutex","commit_stats":{"total_commits":121,"total_committers":16,"mean_commits":7.5625,"dds":0.6033057851239669,"last_synced_commit":"de8e76dc5b26e377f33b1b9ebf4f1d98f9bcfa51"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fmutex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fmutex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fmutex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yiisoft%2Fmutex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yiisoft","download_url":"https://codeload.github.com/yiisoft/mutex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":199582752,"owners_count":13621015,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","lock","mutex","yii3"],"created_at":"2024-09-24T13:36:14.987Z","updated_at":"2025-10-14T22:17:03.909Z","avatar_url":"https://github.com/yiisoft.png","language":"PHP","funding_links":["https://opencollective.com/yiisoft","https://github.com/sponsors/yiisoft"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/yiisoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://yiisoft.github.io/docs/images/yii_logo.svg\" height=\"100px\" alt=\"Yii\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003eYii Mutex\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\n[![Latest Stable Version](https://poser.pugx.org/yiisoft/mutex/v)](https://packagist.org/packages/yiisoft/mutex)\n[![Total Downloads](https://poser.pugx.org/yiisoft/mutex/downloads)](https://packagist.org/packages/yiisoft/mutex)\n[![Build status](https://github.com/yiisoft/mutex/actions/workflows/build.yml/badge.svg)](https://github.com/yiisoft/mutex/actions/workflows/build.yml)\n[![Code Coverage](https://codecov.io/gh/yiisoft/mutex/branch/master/graph/badge.svg)](https://codecov.io/gh/yiisoft/mutex)\n[![Mutation testing badge](https://img.shields.io/endpoint?style=flat\u0026url=https%3A%2F%2Fbadge-api.stryker-mutator.io%2Fgithub.com%2Fyiisoft%2Fmutex%2Fmaster)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/mutex/master)\n[![Static analysis](https://github.com/yiisoft/mutex/actions/workflows/static.yml/badge.svg?branch=master)](https://github.com/yiisoft/mutex/actions/workflows/static.yml?query=branch%3Amaster)\n[![type-coverage](https://shepherd.dev/github/yiisoft/mutex/coverage.svg)](https://shepherd.dev/github/yiisoft/mutex)\n\nThis package provides mutex implementation and allows mutual execution of concurrent processes in order to prevent\n\"race conditions\".\n\nThis is achieved by using a \"lock\" mechanism. Each possibly concurrent processes cooperates by acquiring\na lock before accessing the corresponding data.\n\n## Requirements\n\n- PHP 7.4 or higher.\n\n## Installation\n\nThe package could be installed with [Composer](https://getcomposer.org):\n\n```shell\ncomposer require yiisoft/mutex\n```\n\n## General usage\n\nThere are multiple ways you can use the package. You can execute a callback in a synchronized mode i.e. only a\nsingle instance of the callback is executed at the same time:\n\n```php\n/** @var \\Yiisoft\\Mutex\\Synchronizer $synchronizer */\n$newCount = $synchronizer-\u003eexecute('critical', function () {\n    return $counter-\u003eincrease();\n}, 10);\n```\n\nAnother way is to manually open and close mutex:\n\n```php\n/** @var \\Yiisoft\\Mutex\\SimpleMutex $simpleMutex */\nif (!$simpleMutex-\u003eacquire('critical', 10)) {\n    throw new \\Yiisoft\\Mutex\\Exception\\MutexLockedException('Unable to acquire the \"critical\" mutex.');\n}\n$newCount = $counter-\u003eincrease();\n$simpleMutex-\u003erelease('critical');\n```\n\nIt could be done on lower level:\n\n```php\n/** @var \\Yiisoft\\Mutex\\MutexFactoryInterface $mutexFactory */\n$mutex = $mutexFactory-\u003ecreateAndAcquire('critical', 10);\n$newCount = $counter-\u003eincrease();\n$mutex-\u003erelease();\n```\n\nAnd if you want even more control, you can acquire mutex manually:\n\n```php\n/** @var \\Yiisoft\\Mutex\\MutexFactoryInterface $mutexFactory */\n$mutex = $mutexFactory-\u003ecreate('critical');\nif (!$mutex-\u003eacquire(10)) {\n    throw new \\Yiisoft\\Mutex\\Exception\\MutexLockedException('Unable to acquire the \"critical\" mutex.');\n}\n$newCount = $counter-\u003eincrease();\n$mutex-\u003erelease();\n```\n\n## Mutex drivers\n\nThere are some mutex drivers available as separate packages:\n\n- [File](https://github.com/yiisoft/mutex-file)\n- [PDO MySQL](https://github.com/yiisoft/mutex-pdo-mysql)\n- [PDO Oracle](https://github.com/yiisoft/mutex-pdo-oracle)\n- [PDO Postgres](https://github.com/yiisoft/mutex-pdo-pgsql)\n- [Redis](https://github.com/yiisoft/mutex-redis)\n\nIf you want to provide your own driver, you need to implement `MutexFactoryInterface` and `MutexInterface`.\nThere is ready to extend `Mutex`, `MutexFactory` and a `RetryAcquireTrait` that contains `retryAcquire()`\nmethod implementing the \"wait for a lock for a certain time\" functionality.\n\nWhen implementing your own drivers, you need to take care of automatic unlocking. For example using a destructor:\n\n```php\npublic function __destruct()\n{\n    $this-\u003erelease();\n}\n```\n\nand shutdown function:\n\n```php\npublic function __construct()\n{\n    register_shutdown_function(function () {\n        $this-\u003erelease();\n    });\n}\n```\n\nNote that you should not call the `exit()` or `die()` functions in the destructor or shutdown function. Since calling\nthese functions in the destructor and shutdown function will prevent all subsequent completion functions from executing.\n\n## Documentation\n\n- [Internals](docs/internals.md)\n\nIf you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for\nthat. You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).\n\n## License\n\nThe Yii Mutex is free software. It is released under the terms of the BSD License.\nPlease see [`LICENSE`](./LICENSE.md) for more information.\n\nMaintained by [Yii Software](https://www.yiiframework.com/).\n\n## Support the project\n\n[![Open Collective](https://img.shields.io/badge/Open%20Collective-sponsor-7eadf1?logo=open%20collective\u0026logoColor=7eadf1\u0026labelColor=555555)](https://opencollective.com/yiisoft)\n\n## Follow updates\n\n[![Official website](https://img.shields.io/badge/Powered_by-Yii_Framework-green.svg?style=flat)](https://www.yiiframework.com/)\n[![Twitter](https://img.shields.io/badge/twitter-follow-1DA1F2?logo=twitter\u0026logoColor=1DA1F2\u0026labelColor=555555?style=flat)](https://twitter.com/yiiframework)\n[![Telegram](https://img.shields.io/badge/telegram-join-1DA1F2?style=flat\u0026logo=telegram)](https://t.me/yii3en)\n[![Facebook](https://img.shields.io/badge/facebook-join-1DA1F2?style=flat\u0026logo=facebook\u0026logoColor=ffffff)](https://www.facebook.com/groups/yiitalk)\n[![Slack](https://img.shields.io/badge/slack-join-1DA1F2?style=flat\u0026logo=slack)](https://yiiframework.com/go/slack)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiisoft%2Fmutex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyiisoft%2Fmutex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyiisoft%2Fmutex/lists"}