php-ipc/src/Sem/Semaphore.php

32 lines
594 B
PHP
Raw Normal View History

2018-04-15 14:41:46 +00:00
<?php
namespace NoccyLabs\Ipc\Sem;
use NoccyLabs\Ipc\Key\KeyInterface;
class Semaphore
{
2018-04-15 18:48:22 +00:00
protected $resource;
public function __construct(KeyInterface $key, int $max, $perm = 0660, $autorelease = 1)
2018-04-15 14:41:46 +00:00
{
2018-04-15 18:48:22 +00:00
$this->resource = sem_get($key->getKey(), $max, $perm, $autorelease);
}
2018-04-15 14:41:46 +00:00
2018-04-15 18:48:22 +00:00
public function destroy()
{
sem_remove($this->resource);
2018-04-15 14:41:46 +00:00
}
2018-04-15 18:48:22 +00:00
public function acquire(float $timeout = 0):bool
2018-04-15 14:41:46 +00:00
{
2018-04-15 18:48:22 +00:00
return sem_acquire($this->resource, true);
2018-04-15 14:41:46 +00:00
}
2018-04-15 18:48:22 +00:00
public function release():bool
2018-04-15 14:41:46 +00:00
{
2018-04-15 18:48:22 +00:00
return sem_release($this->resource);
2018-04-15 14:41:46 +00:00
}
}