{"id":21194154,"url":"https://github.com/petrknap/php-critical-section","last_synced_at":"2026-01-21T08:36:14.872Z","repository":{"id":206640958,"uuid":"717232959","full_name":"petrknap/php-critical-section","owner":"petrknap","description":"Critical section helper for PHP","archived":false,"fork":false,"pushed_at":"2024-11-06T19:55:56.000Z","size":54,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-22T03:41:42.516Z","etag":null,"topics":["critical-section","helper","lock","locking","php","php-library"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/petrknap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-11-10T21:46:52.000Z","updated_at":"2024-11-06T19:53:35.000Z","dependencies_parsed_at":"2023-11-11T22:27:08.371Z","dependency_job_id":"421e5b0f-6e28-4204-848c-08fcd933548c","html_url":"https://github.com/petrknap/php-critical-section","commit_stats":null,"previous_names":["petrknap/php-lock","petrknap/php-criticalsection","petrknap/php-critical-section"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-critical-section","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-critical-section/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-critical-section/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/petrknap%2Fphp-critical-section/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/petrknap","download_url":"https://codeload.github.com/petrknap/php-critical-section/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243647905,"owners_count":20324788,"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":["critical-section","helper","lock","locking","php","php-library"],"created_at":"2024-11-20T19:19:52.358Z","updated_at":"2026-01-21T08:36:14.831Z","avatar_url":"https://github.com/petrknap.png","language":"PHP","readme":"# Critical section based on `symfony/lock`\n\n[The `CriticalSection`](./src/CriticalSection.php) is a simple object that handles the critical section overhead for you\nand lets you focus on the actual code.\n\n```php\nuse PetrKnap\\CriticalSection\\CriticalSection;\nuse Symfony\\Component\\Lock\\NoLock;\n\n$lock = new NoLock();\n\n$criticalOutput = CriticalSection::withLock($lock)(fn () =\u003e 'This was critical.');\n\nvar_dump($criticalOutput);\n```\n\nYou can wrap critical sections one inside the other thanks to [the `WrappingCriticalSection`](./src/WrappingCriticalSection.php).\nThis makes it easy to combine multiple locks, for example.\n\n```php\nuse PetrKnap\\CriticalSection\\CriticalSection;\nuse Symfony\\Component\\Lock\\NoLock;\n\n$lockA = new NoLock();\n$lockB = new NoLock();\n\n$criticalOutput = CriticalSection::withLock($lockA)-\u003ewithLock($lockB)(fn () =\u003e 'This was critical.');\n\nvar_dump($criticalOutput);\n```\n\nYou can also pass locks as array and leave the composition to the critical section.\n\n```php\nuse PetrKnap\\CriticalSection\\CriticalSection;\nuse Symfony\\Component\\Lock\\NoLock;\n\n$lockA = new NoLock();\n$lockB = new NoLock();\n\n$criticalOutput = CriticalSection::withLocks([$lockA, $lockB])(fn () =\u003e 'This was critical.');\n\nvar_dump($criticalOutput);\n```\n\n## Do you need to accept only locked resources?\n\nUse [the `LockedResource`](./src/LockedResource.php) if you need to be sure that you are not processing resource outside it's critical section.\n\n```php\nnamespace PetrKnap\\CriticalSection;\n\nuse Symfony\\Component\\Lock\\NoLock;\n\n/** @param Locked\u003cSome\\Resource\u003e $resource */\nfunction f(LockedResource $resource) {\n    echo $resource-\u003evalue;\n}\n\n$lock = new NoLock();\n$resource = LockableResource::of(new Some\\Resource('data'), $lock);\nCriticalSection::withLock($lock)(fn () =\u003e f($resource));\n```\n\n## Does your critical section work with database?\n\nUse [the `doctrine/dbal`](https://packagist.org/packages/doctrine/dbal) and its `transactional` method.\n\n```php\n/** @var PetrKnap\\CriticalSection\\CriticalSectionInterface $criticalSection */\n/** @var Doctrine\\DBAL\\Connection $connection */\n$criticalSection(\n    fn () =\u003e $connection-\u003etransactional(\n        fn () =\u003e 'This was critical on DB server.'\n    )\n);\n```\n\nAlways use `transactional` inside critical section to prevent starvation.\n\n---\n\nRun `composer require petrknap/critical-section` to install it.\nYou can [support this project via donation](https://petrknap.github.io/donate.html).\nThe project is licensed under [the terms of the `LGPL-3.0-or-later`](./COPYING.LESSER).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetrknap%2Fphp-critical-section","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpetrknap%2Fphp-critical-section","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpetrknap%2Fphp-critical-section/lists"}