Skip to content

Commit

Permalink
Update Thread.php
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTechsTech committed Apr 24, 2022
1 parent 1eafcff commit 1b92267
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions Spawn/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public function close()
$this->isYield = false;
$this->isClosed = true;
$this->releaseQueue = true;
\gc_collect_cycles();
}
}

Expand All @@ -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)) {
Expand All @@ -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);
}

/**
Expand All @@ -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';
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 1b92267

Please sign in to comment.