https://github.com/takuya/php-sysv-ipc-semaphore
php sysv semaphore wrapper
https://github.com/takuya/php-sysv-ipc-semaphore
Last synced: about 1 year ago
JSON representation
php sysv semaphore wrapper
- Host: GitHub
- URL: https://github.com/takuya/php-sysv-ipc-semaphore
- Owner: takuya
- License: gpl-3.0
- Created: 2025-03-05T17:16:44.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-04-19T01:57:11.000Z (about 1 year ago)
- Last Synced: 2025-04-19T10:56:34.471Z (about 1 year ago)
- Language: PHP
- Size: 37.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# php-sysv-ipc-semaphore
This package is wrapper for php sysv sem_xxx. And with `string $name`
## Installing
from Packagist
```shell
composer require takuya/php-sysv-ipc-semaphore
```
from GitHub
```shell
name='php-sysv-ipc-semaphore'
composer config repositories.$name \
vcs https://github.com/takuya/$name
composer require takuya/$name:master
composer install
```
## Examples
```php
acquire();// first acquire must be success.
$semaphore->acquire(true);// multiple acquired result true
$semaphore->release();
//
// remove from IPC
//
$semaphore->destroy();
```
## More easy way to use semaphore
Using semaphore with callback
```php
withLock(function(){
// do something in lock
echo "run in lock";
return 1234;
});
$ret === 1234; //=> true
```
Using easy locking ( release() by destructor ).
```php
lock();
return 1234;
}
///
RunWithLock();
```
Using semaphore with try-finally
```php
acquire();
return $msg; // finally called before return.
} finally {
$sem->release();
}
}
```
### semaphore and thread-mutex
Compare to Thread and SyncMutex , SysV semaphore has one big advantage in PHP.
SysV function (ex `sem_get`) is bundled with PHP, no required PECL.
But, `sem_get()` does not accept string named. This package utilize pseudo string $name.
### remove ipc by manually
If unused ipc remains. use SHELL command to remove.
```shell
ipcs -s | grep $USER | grep -oE '0x[a-f0-9]+' | xargs -I@ ipcrm --semaphore-key @
```