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; $busyQueue = $this->busyQueue;
$this->push = static function (Worker $worker) use ($workers, $idleWorkers, $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->contains($worker) || ($workers[$worker] -= 1) > 0) {
return;
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);
} }
// 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} * {@inheritdoc}
*/ */
@ -232,7 +220,8 @@ final class DefaultPool implements Pool
$worker = $this->busyQueue->shift(); $worker = $this->busyQueue->shift();
} else { } else {
// Max worker count has not been reached, so create another worker. // Max worker count has not been reached, so create another worker.
$worker = $this->createWorker(); $worker = $this->factory->create();
$this->workers->attach($worker, 0);
break; break;
} }
} else { } else {