PHPackages                             cosmic-beacon/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. cosmic-beacon/critical-section

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

cosmic-beacon/critical-section
==============================

Handles critical sections while executing multiple php scripts in parallel. Useful for cron and any other recurrently run scripts.

1.0.0(10y ago)14.6kMITPHPPHP &gt;= 5.6.0

Since Oct 6Pushed 10y ago5 watchersCompare

[ Source](https://github.com/cosmic-beacon/critical-section)[ Packagist](https://packagist.org/packages/cosmic-beacon/critical-section)[ RSS](/packages/cosmic-beacon-critical-section/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

CriticalSection PHP library
===========================

[](#criticalsection-php-library)

[![Build Status](https://camo.githubusercontent.com/69b2d62e50ee26f14153516f56be23b8ba40b4cf6e1b4e7b15040bfb4d7e6a08/68747470733a2f2f7472617669732d63692e6f72672f636f736d69632d626561636f6e2f637269746963616c2d73656374696f6e2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/cosmic-beacon/critical-sectionn)

This library handles problem of code critical section which should be executed only once at a time.

Example use case
----------------

[](#example-use-case)

You have a cron task that is executing every minute, but it takes some time to execute. When server load's go critical it can take more than minute, which leads to bugs as a same cron is executed in multiple process at the same time.

You can encapsulate this code into critical section with use of this library.

```
$criticalSection = new FileCriticalSection();

$criticalSection->enter('cron_long_task');
doSomething();
$criticalSection->leave('cron_long_task');
```

This code example does take care of critical sections. You say which critical section should be entered. In this case it is `cron_long_task`. No other script can enter this critical section until the original one leaves the critical section. Keep in mind that critical section is left even if the script ends, or `CriticalSection` object is destructed.

If you want to enter critical section you can't script will wait until it can be entered. If you need to skip code which is already in critical section use timeout or `canEnter` method.

Timeout critical section
------------------------

[](#timeout-critical-section)

You can also do timeout enter. This way you can say explicitly say how long should the script wait to enter critical section. This is useful if you want to do something, but you know it only make sense in some time interval.

```
$criticalSection = new FileCriticalSection();

$code = __FILE__;
if ($criticalSection->enter($code, 30)) {
    doSomething();
    $criticalSection->leave($code);
}
```

Keep in mind that you have to check if enter returned true if you use timeout.

Check if code can enter critical section
----------------------------------------

[](#check-if-code-can-enter-critical-section)

If you are using every minute cron (or even faster), we can assume that you can skip one or two calls here and there. To do so you can check if critical section can be entered before entering it.

```
$criticalSection = new FileCriticalSection();

$code = __FILE__;
if ($criticalSection->canEnter($code)) {
    $criticalSection->enter($code);
    doSomething();
    $criticalSection->leave($code);
}
```

Another way to do so is to enter critical section with zero timeout.

```
$criticalSection = new FileCriticalSection();

$code = __FILE__;
if ($criticalSection->enter($code, 0)) {
    doSomething();
    $criticalSection->leave($code);
}
```

Reporting an issue or a feature request
---------------------------------------

[](#reporting-an-issue-or-a-feature-request)

Issues and feature requests are tracked in the [Github issue tracker](https://github.com/cosmic-beacon/critical-section/issues).

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity58

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

Unknown

Total

1

Last Release

3876d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/743421?v=4)[Martin Hlavac](/maintainers/mhlavac)[@mhlavac](https://github.com/mhlavac)

---

Top Contributors

[![mhlavac](https://avatars.githubusercontent.com/u/743421?v=4)](https://github.com/mhlavac "mhlavac (8 commits)")

---

Tags

semaphoremutexparallelsectionlockercritical

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[symfony/lock

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

514127.6M459](/packages/symfony-lock)[illuminated/console-mutex

Mutex for Laravel Console Commands.

146938.2k1](/packages/illuminated-console-mutex)[veewee/composer-run-parallel

Run composer tasks in parallel

91748.2k13](/packages/veewee-composer-run-parallel)[symfony/semaphore

Symfony Semaphore Component

373.9M6](/packages/symfony-semaphore)[recca0120/laravel-parallel

64116.7k](/packages/recca0120-laravel-parallel)[bluepsyduck/symfony-process-manager

A process manager for Symfony processes, able to run them in parallel.

10784.6k3](/packages/bluepsyduck-symfony-process-manager)

PHPackages © 2026

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