From 1b92267f99bbc2986b0c612307093f6ae8809c8a Mon Sep 17 00:00:00 2001 From: "L. Stubbs" Date: Sun, 24 Apr 2022 14:33:50 -0400 Subject: [PATCH] Update Thread.php --- Spawn/Thread.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Spawn/Thread.php b/Spawn/Thread.php index f59ebee..f86ceb3 100644 --- a/Spawn/Thread.php +++ b/Spawn/Thread.php @@ -78,7 +78,6 @@ public function close() $this->isYield = false; $this->isClosed = true; $this->releaseQueue = true; - \gc_collect_cycles(); } } @@ -92,6 +91,7 @@ public function __construct($loop = null, ?\UVLoop $uv = null, bool $yielding = if (!\IS_THREADED_UV) throw new \InvalidArgumentException('This `Thread` class requires PHP `ZTS` and the libuv `ext-uv` extension!'); + $lock = \mutex_lock(); $this->isYield = $yielding; $this->hasLoop = \is_object($loop) && \method_exists($loop, 'executeTask') && \method_exists($loop, 'run'); if (Parallel::isCoroutine($loop)) { @@ -106,6 +106,7 @@ public function __construct($loop = null, ?\UVLoop $uv = null, bool $yielding = self::$uv = $uvLoop instanceof \UVLoop ? $uvLoop : \uv_default_loop(); $this->success = $this->isYield ? [$this, 'yieldAsFinished'] : [$this, 'triggerSuccess']; $this->failed = $this->isYield ? [$this, 'yieldAsFailed'] : [$this, 'triggerError']; + \mutex_unlock($lock); } /** @@ -119,6 +120,7 @@ public function __construct($loop = null, ?\UVLoop $uv = null, bool $yielding = */ public function create($tid, callable $task, ...$args): self { + $lock = \mutex_lock(); $tid = \is_scalar($tid) ? $tid : (int) $tid; $this->tid = $tid; $this->status[$tid] = 'queued'; @@ -143,13 +145,12 @@ public function create($tid, callable $task, ...$args): self if (isset($async->threads[$tid]) && $async->threads[$tid] instanceof \UVAsync && \uv_is_active($async->threads[$tid])) { \uv_async_send($async->threads[$tid]); do { - \usleep($async->count() * 70000); + \usleep($async->count() * 35000); } while (!$async->releaseQueue); } - - \gc_collect_cycles(); }, function () { }); + \mutex_unlock($lock); return $this; } @@ -353,7 +354,9 @@ public function getFailed(): array */ public function then(callable $thenCallback, callable $failCallback = null): self { + $lock = \mutex_lock(); $this->successCallbacks[$this->tid][] = $thenCallback; + \mutex_unlock($lock); if ($failCallback !== null) { $this->catch($failCallback); } @@ -369,7 +372,9 @@ public function then(callable $thenCallback, callable $failCallback = null): sel */ public function catch(callable $callback): self { + $lock = \mutex_lock(); $this->errorCallbacks[$this->tid][] = $callback; + \mutex_unlock($lock); return $this; }