From 4652e464b999498ef337d4878d0e4e6ba5b6f7e9 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Sun, 15 Apr 2018 20:48:22 +0200 Subject: [PATCH] Semaphores fully implemented --- .gitignore | 1 + src/Sem/Semaphore.php | 19 +++++++++++++------ tests/Sem/SemaphoreTest.php | 27 +++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 tests/Sem/SemaphoreTest.php diff --git a/.gitignore b/.gitignore index 4fbb073..c8a31df 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor/ /composer.lock +/doc diff --git a/src/Sem/Semaphore.php b/src/Sem/Semaphore.php index 18a94cd..c1bc4aa 100644 --- a/src/Sem/Semaphore.php +++ b/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 acquire(float $timeout) + public function destroy() { - + sem_remove($this->resource); } - public function release() + public function acquire(float $timeout = 0):bool { - + return sem_acquire($this->resource, true); + } + + public function release():bool + { + return sem_release($this->resource); } } \ No newline at end of file diff --git a/tests/Sem/SemaphoreTest.php b/tests/Sem/SemaphoreTest.php new file mode 100644 index 0000000..6262f60 --- /dev/null +++ b/tests/Sem/SemaphoreTest.php @@ -0,0 +1,27 @@ +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()); + } + +} \ No newline at end of file