https://github.com/petrknap/php-critical-section
Critical section helper for PHP
https://github.com/petrknap/php-critical-section
critical-section helper lock locking php php-library
Last synced: 3 months ago
JSON representation
Critical section helper for PHP
- Host: GitHub
- URL: https://github.com/petrknap/php-critical-section
- Owner: petrknap
- License: lgpl-3.0
- Created: 2023-11-10T21:46:52.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-11-06T19:55:56.000Z (over 1 year ago)
- Last Synced: 2025-01-22T03:41:42.516Z (over 1 year ago)
- Topics: critical-section, helper, lock, locking, php, php-library
- Language: PHP
- Homepage:
- Size: 52.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: COPYING
- Authors: AUTHORS
Awesome Lists containing this project
README
# Critical section based on `symfony/lock`
[The `CriticalSection`](./src/CriticalSection.php) is a simple object that handles the critical section overhead for you
and lets you focus on the actual code.
```php
use PetrKnap\CriticalSection\CriticalSection;
use Symfony\Component\Lock\NoLock;
$lock = new NoLock();
$criticalOutput = CriticalSection::withLock($lock)(fn () => 'This was critical.');
var_dump($criticalOutput);
```
You can wrap critical sections one inside the other thanks to [the `WrappingCriticalSection`](./src/WrappingCriticalSection.php).
This makes it easy to combine multiple locks, for example.
```php
use PetrKnap\CriticalSection\CriticalSection;
use Symfony\Component\Lock\NoLock;
$lockA = new NoLock();
$lockB = new NoLock();
$criticalOutput = CriticalSection::withLock($lockA)->withLock($lockB)(fn () => 'This was critical.');
var_dump($criticalOutput);
```
You can also pass locks as array and leave the composition to the critical section.
```php
use PetrKnap\CriticalSection\CriticalSection;
use Symfony\Component\Lock\NoLock;
$lockA = new NoLock();
$lockB = new NoLock();
$criticalOutput = CriticalSection::withLocks([$lockA, $lockB])(fn () => 'This was critical.');
var_dump($criticalOutput);
```
## Do you need to accept only locked resources?
Use [the `LockedResource`](./src/LockedResource.php) if you need to be sure that you are not processing resource outside it's critical section.
```php
namespace PetrKnap\CriticalSection;
use Symfony\Component\Lock\NoLock;
/** @param Locked $resource */
function f(LockedResource $resource) {
echo $resource->value;
}
$lock = new NoLock();
$resource = LockableResource::of(new Some\Resource('data'), $lock);
CriticalSection::withLock($lock)(fn () => f($resource));
```
## Does your critical section work with database?
Use [the `doctrine/dbal`](https://packagist.org/packages/doctrine/dbal) and its `transactional` method.
```php
/** @var PetrKnap\CriticalSection\CriticalSectionInterface $criticalSection */
/** @var Doctrine\DBAL\Connection $connection */
$criticalSection(
fn () => $connection->transactional(
fn () => 'This was critical on DB server.'
)
);
```
Always use `transactional` inside critical section to prevent starvation.
---
Run `composer require petrknap/critical-section` to install it.
You can [support this project via donation](https://petrknap.github.io/donate.html).
The project is licensed under [the terms of the `LGPL-3.0-or-later`](./COPYING.LESSER).