https://github.com/azjezz/mutex
Mutex Locking for hack projects
https://github.com/azjezz/mutex
hack hacklang hhvm mutex mutex-lock mutual-exclusion
Last synced: 6 months ago
JSON representation
Mutex Locking for hack projects
- Host: GitHub
- URL: https://github.com/azjezz/mutex
- Owner: azjezz
- License: mit
- Created: 2019-03-01T16:53:16.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-18T23:06:40.000Z (over 5 years ago)
- Last Synced: 2025-02-12T11:32:53.802Z (8 months ago)
- Topics: hack, hacklang, hhvm, mutex, mutex-lock, mutual-exclusion
- Language: Hack
- Homepage:
- Size: 6.84 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Mutex
Hack implementation of [`reactphp-muxted`](https://github.com/WyriHaximus/reactphp-mutex) by [WyriHaximus](https://github.com/WyriHaximus).
[](https://travis-ci.com/azjezz/mutex)
[](https://packagist.org/packages/azjezz/mutex)
[](https://packagist.org/packages/azjezz/mutex)
[](https://packagist.org/packages/azjezz/mutex)---
## Install
To install via Composer, use the command below :
```console
composer require azjezz/mutex
```## About
This package provides two things:
- An interface for [`mutex` locking](https://en.wikipedia.org/wiki/Mutual_exclusion)
- A in-memory implementation of that interface## Example
```hack
use namespace AzJezz\Mutex;
use namespace HH\Asio;require 'vendor/autoload.hack';
<<__EntryPoint>>
async function main(): Awaitable {
Facebook\AutoloadMap\initialize();$mutex = new Mutex\Memory();
$jobs = vec[
foo($mutex), // first to acquire the lock
foo($mutex), // won't be able to acquire the lock
foo($mutex), // same
];foreach($jobs as $job) {
await $job;
}
}async function foo(
Mutex\MutexInterface $mutex
): Awaitable {
$lock = await $mutex->acquire('foo');
if ($lock is nonnull) {
echo "doing things \n";
await Asio\usleep(10000000);
echo "finished my job, releasing the lock \n";
await $mutex->release($lock);
return;
}echo "someone else have requested the 'foo' lock\n";
}```
## License
The Mutex Project is open-sourced software licensed under the MIT-licensed.