1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-30 04:39:01 +01:00
This commit is contained in:
Aaron Piotrowski 2018-12-19 18:47:43 -06:00
parent c4eed9535b
commit 1c9822a564
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB

View File

@ -68,19 +68,19 @@ final class DefaultPool implements Pool
$busyQueue = $this->busyQueue;
$this->push = static function (Worker $worker) use ($workers, $idleWorkers, $busyQueue) {
\assert($workers->contains($worker), "The provided worker was not part of this queue");
if (($workers[$worker] -= 1) === 0) {
// Worker is completely idle, remove from busy queue and add to idle queue.
foreach ($busyQueue as $key => $busy) {
if ($busy === $worker) {
unset($busyQueue[$key]);
break;
}
}
$idleWorkers->push($worker);
if (!$workers->contains($worker) || ($workers[$worker] -= 1) > 0) {
return;
}
// Worker is completely idle, remove from busy queue and add to idle queue.
foreach ($busyQueue as $key => $busy) {
if ($busy === $worker) {
unset($busyQueue[$key]);
break;
}
}
$idleWorkers->push($worker);
};
}
@ -193,18 +193,6 @@ final class DefaultPool implements Pool
}
}
/**
* Creates a worker and adds them to the pool.
*
* @return Worker The worker created.
*/
private function createWorker(): Worker
{
$worker = $this->factory->create();
$this->workers->attach($worker, 0);
return $worker;
}
/**
* {@inheritdoc}
*/
@ -232,7 +220,8 @@ final class DefaultPool implements Pool
$worker = $this->busyQueue->shift();
} else {
// Max worker count has not been reached, so create another worker.
$worker = $this->createWorker();
$worker = $this->factory->create();
$this->workers->attach($worker, 0);
break;
}
} else {