PHPackages                             petrknap/critical-section - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. petrknap/critical-section

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

petrknap/critical-section
=========================

Critical section based on `symfony/lock`

v2.1.1(1y ago)069[1 PRs](https://github.com/petrknap/php-critical-section/pulls)LGPL-3.0-or-laterPHPPHP &gt;=8.1

Since Nov 11Pushed 1y ago1 watchersCompare

[ Source](https://github.com/petrknap/php-critical-section)[ Packagist](https://packagist.org/packages/petrknap/critical-section)[ Docs](https://github.com/petrknap/php-critical-section)[ Fund](https://petrknap.github.io/donate.html)[ RSS](/packages/petrknap-critical-section/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (6)Versions (11)Used By (0)

Critical section based on `symfony/lock`
========================================

[](#critical-section-based-on-symfonylock)

[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.

```
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.

```
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.

```
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?
--------------------------------------------

[](#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.

```
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?
----------------------------------------------

[](#does-your-critical-section-work-with-database)

Use [the `doctrine/dbal`](https://packagist.org/packages/doctrine/dbal) and its `transactional` method.

```
/** @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).

###  Health Score

30

—

LowBetter than 65% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~45 days

Recently: every ~34 days

Total

9

Last Release

549d ago

Major Versions

v1.4.0 → v2.0.02024-06-29

PHP version history (2 changes)v1.0.0PHP &gt;=8.0

v1.3.0PHP &gt;=8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/c0e19f2ea17567eb9f536d1e00595b83630dfb8b2df6e7e422e06a9a48e85585?d=identicon)[petrknap](/maintainers/petrknap)

---

Top Contributors

[![petrknap](https://avatars.githubusercontent.com/u/8299754?v=4)](https://github.com/petrknap "petrknap (33 commits)")

---

Tags

critical-sectionhelperlocklockingphpphp-librarylockinglockcritical-section

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/petrknap-critical-section/health.svg)

```
[![Health](https://phpackages.com/badges/petrknap-critical-section/health.svg)](https://phpackages.com/packages/petrknap-critical-section)
```

###  Alternatives

[symfony/lock

Creates and manages locks, a mechanism to provide exclusive access to a shared resource

514127.6M457](/packages/symfony-lock)[pudongping/hyperf-wise-locksmith

A mutex library provider for the Hyperf framework, designed to enable serialized execution of PHP code in high-concurrency scenarios.

106.3k2](/packages/pudongping-hyperf-wise-locksmith)[zerkalica/semaphore

This library provides an api for semaphore acquire and release

1119.0k1](/packages/zerkalica-semaphore)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
