PHPackages                             sof3/rwlock - 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. sof3/rwlock

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

sof3/rwlock
===========

Exclusive and Read-write locks for PHP

0.1.1(4y ago)41.0k11MITPHP

Since Dec 19Pushed 1y ago1 watchersCompare

[ Source](https://github.com/SOF3/rwlock.php)[ Packagist](https://packagist.org/packages/sof3/rwlock)[ RSS](/packages/sof3-rwlock/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (3)Used By (1)

rwlock.php
==========

[](#rwlockphp)

Exclusive and Read-write locks for PHP.

This is a quick port of [my javascript library `rwlock-promise`](https://github.com/SOF3/rwlock-promise) to PHP. Some parts may happen to be less idiomatic because of this.

This library uses the [`await-generator`](https://github.com/SOF3/await-generator) framework to expose asynchronous function call API.

Usage
-----

[](#usage)

```
$mutex = new Mutex;

$epoch = time();

yield $mutex->run(function() use($epoch) {
	echo "Start 1: ", time() - $epoch, "\n";
	yield $this->waitSeconds(2);
	echo "End 1: ", time() - $epoch, "\n";
});

yield $mutex->run(function() use($epoch) {
	echo "Start 2: ", time() - $epoch, "\n";
	yield $this->waitSeconds(1);
	echo "End 2: ", time() - $epoch, "\n";
});
```

The above should output

```
Start 1: 0
End 1: 2
Start 2: 2
End 2: 3

```

The second generator is only run after the first generator returns.

```
$mutex = new RwLock;

$epoch = time();

Await::g2c($mutex->readClosure(function() use($epoch) {
	echo "Start 1: ", time() - $epoch, "\n";
	yield $this->waitSeconds(2);
	echo "End 1: ", time() - $epoch, "\n";
}));

Await::g2c($mutex->readClosure(function() use($epoch, $mutex) {
	echo "Start 2: ", time() - $epoch, "\n";
	yield $this->waitSeconds(1);
	echo "End 2: ", time() - $epoch, "\n";

	Await::g2c($mutex->readClosure(function() use($epoch) {
		echo "Start 3: ", time() - $epoch, "\n";
		yield $this->waitSeconds(2);
		echo "End 3: ", time() - $epoch, "\n";
	}));

	Await::f2c($mutex->writeClosure(function() use($epoch) {
		echo "Start 4: ", time() - $epoch, "\n";
		yield $this->waitSeconds(2);
		echo "End 4: ", time() - $epoch, "\n";
	}));

	Await::f2c($mutex->readClosure(function() use($epoch) {
		echo "Start 5: ", time() - $epoch, "\n";
		yield $this->waitSeconds(2);
		echo "End 5: ", time() - $epoch, "\n";
	}));
}));
```

The above should output

```
Start 1: 0
Start 2: 0
End 2: 1
Start 3: 1
End 1: 2
End 3: 3
Start 4: 3
End 4: 5
Start 5: 5
End 5: 7

```

It does not make sense to `yield` (await) a read/write inside a read/write block; in particular, yielding another write inside a read/write block would lead to a deadlock. Use `Await::g2c`, as shown above, to schedule a read/write without blocking on it. However, you must always either `yield` or `f2c`/`g2c` a read/write, because it only returns a generator, which does nothing if you don't call anything on it.

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 76.5% 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 ~76 days

Total

2

Last Release

1534d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2801890?v=4)[SOFe](/maintainers/SOFe)[@sofe](https://github.com/sofe)

---

Top Contributors

[![SOF3](https://avatars.githubusercontent.com/u/19623715?v=4)](https://github.com/SOF3 "SOF3 (13 commits)")[![Swift-Strider](https://avatars.githubusercontent.com/u/62265561?v=4)](https://github.com/Swift-Strider "Swift-Strider (2 commits)")[![Endermanbugzjfc](https://avatars.githubusercontent.com/u/53002741?v=4)](https://github.com/Endermanbugzjfc "Endermanbugzjfc (1 commits)")[![poggit-bot](https://avatars.githubusercontent.com/u/22427965?v=4)](https://github.com/poggit-bot "poggit-bot (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/sof3-rwlock/health.svg)

```
[![Health](https://phpackages.com/badges/sof3-rwlock/health.svg)](https://phpackages.com/packages/sof3-rwlock)
```

###  Alternatives

[weareferal/remote-sync

Sync your database and assets across Craft environments

1216.0k](/packages/weareferal-remote-sync)[dipcode/php-form

Full featured form engine library.

1110.5k](/packages/dipcode-php-form)

PHPackages © 2026

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