An open API service indexing awesome lists of open source software.

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

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