Browse Source

Semaphores fully implemented

master 0.1.3
Noccy 4 years ago
parent
commit
4652e464b9
  1. 1
      .gitignore
  2. 17
      src/Sem/Semaphore.php
  3. 27
      tests/Sem/SemaphoreTest.php

1
.gitignore vendored

@ -1,2 +1,3 @@
/vendor/
/composer.lock
/doc

17
src/Sem/Semaphore.php

@ -8,18 +8,25 @@ use NoccyLabs\Ipc\Key\KeyInterface;
class Semaphore
{
public function __construct(KeyInterface $key, int $max)
protected $resource;
public function __construct(KeyInterface $key, int $max, $perm = 0660, $autorelease = 1)
{
$this->resource = sem_get($key->getKey(), $max, $perm, $autorelease);
}
public function destroy()
{
sem_remove($this->resource);
}
public function acquire(float $timeout)
public function acquire(float $timeout = 0):bool
{
return sem_acquire($this->resource, true);
}
public function release()
public function release():bool
{
return sem_release($this->resource);
}
}

27
tests/Sem/SemaphoreTest.php

@ -0,0 +1,27 @@
<?php
namespace NoccyLabs\Ipc\Sem;
use NoccyLabs\Ipc\Key\FileKey;
class SemaphoreTest extends \PhpUnit\Framework\TestCase
{
public function testCreatingAndAquiringSemaphore()
{
$key = new FileKey(__FILE__);
$sem = new Semaphore($key, 2);
$this->assertTrue($sem->acquire());
$this->assertTrue($sem->acquire());
$this->assertFalse($sem->acquire());
$this->assertTrue($sem->release());
$this->assertTrue($sem->acquire());
$this->assertFalse($sem->acquire());
$this->assertTrue($sem->release());
$this->assertTrue($sem->release());
}
}
Loading…
Cancel
Save