Icicle\Concurrent\Worker\DefaultPool::get PHP Method

get() public method

public get ( ) : Worker
return Worker
    public function get() : Worker
    {
        if (!$this->isRunning()) {
            throw new StatusError('The queue is not running.');
        }
        do {
            if ($this->idleWorkers->isEmpty()) {
                if ($this->getWorkerCount() >= $this->maxSize) {
                    // All possible workers busy, so shift from head (will be pushed back onto tail below).
                    $worker = $this->busyQueue->shift();
                } else {
                    // Max worker count has not been reached, so create another worker.
                    $worker = $this->createWorker();
                }
            } else {
                // Shift a worker off the idle queue.
                $worker = $this->idleWorkers->shift();
            }
            if ($worker->isRunning()) {
                break;
            }
            $this->workers->detach($worker);
        } while (true);
        $this->busyQueue->push($worker);
        $this->workers[$worker] += 1;
        return new Internal\PooledWorker($worker, $this->push);
    }