From 172c64c583fb3e9921ddbee5318d8f9f3511f615 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Tue, 1 Sep 2015 20:58:22 -0500 Subject: [PATCH] Fix threaded mutex and semaphore --- src/Threading/Internal/Mutex.php | 2 +- src/Threading/Internal/Semaphore.php | 12 +----------- 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/Threading/Internal/Mutex.php b/src/Threading/Internal/Mutex.php index 29650d4..b4c69f8 100644 --- a/src/Threading/Internal/Mutex.php +++ b/src/Threading/Internal/Mutex.php @@ -27,7 +27,7 @@ class Mutex extends \Threaded return ($this->lock ? $this->lock = false : true); }; - while ($this->lock && $this->synchronized($tsl)) { + while (!$this->lock || $this->synchronized($tsl)) { yield Coroutine\sleep(self::LATENCY_TIMEOUT); } diff --git a/src/Threading/Internal/Semaphore.php b/src/Threading/Internal/Semaphore.php index 52a6817..ce7b155 100644 --- a/src/Threading/Internal/Semaphore.php +++ b/src/Threading/Internal/Semaphore.php @@ -13,21 +13,11 @@ class Semaphore extends \Threaded { const LATENCY_TIMEOUT = 0.01; // 10 ms - /** - * @var int - */ - private $nextId = 0; - /** * @var int The number of available locks. */ private $locks; - /** - * @var array A queue of lock requests. - */ - private $waitQueue = []; - /** * Creates a new semaphore with a given number of locks. * @@ -72,7 +62,7 @@ class Semaphore extends \Threaded return true; }; - while ($this->locks > 0 && $this->synchronized($tsl)) { + while ($this->locks < 1 || $this->synchronized($tsl)) { yield Coroutine\sleep(self::LATENCY_TIMEOUT); }