diff --git a/pool.go b/pool.go index 3c2e28d..cff7488 100644 --- a/pool.go +++ b/pool.go @@ -189,21 +189,27 @@ func (p *Pool) decRunning() { // retrieveWorker returns a available worker to run the tasks. func (p *Pool) retrieveWorker() *Worker { var w *Worker - var waiting bool p.lock.Lock() idleWorkers := p.workers n := len(idleWorkers) - 1 - if n < 0 { - waiting = p.Running() >= p.Cap() - } else { + if n >= 0 { w = idleWorkers[n] idleWorkers[n] = nil p.workers = idleWorkers[:n] p.lock.Unlock() - } - - if waiting { + } else if p.Running() < p.Cap() { + p.lock.Unlock() + if cacheWorker := p.workerCache.Get(); cacheWorker != nil { + w = cacheWorker.(*Worker) + } else { + w = &Worker{ + pool: p, + task: make(chan f, workerChanCap), + } + w.run() + } + } else { for { p.cond.Wait() l := len(p.workers) - 1 @@ -216,16 +222,6 @@ func (p *Pool) retrieveWorker() *Worker { break } p.lock.Unlock() - } else if w == nil { - p.lock.Unlock() - if cacheWorker := p.workerCache.Get(); cacheWorker != nil { - return cacheWorker.(*Worker) - } - w = &Worker{ - pool: p, - task: make(chan f, workerChanCap), - } - w.run() } return w } diff --git a/pool_func.go b/pool_func.go index 32b96d5..d0a5cd3 100644 --- a/pool_func.go +++ b/pool_func.go @@ -193,21 +193,27 @@ func (p *PoolWithFunc) decRunning() { // retrieveWorker returns a available worker to run the tasks. func (p *PoolWithFunc) retrieveWorker() *WorkerWithFunc { var w *WorkerWithFunc - var waiting bool p.lock.Lock() idleWorkers := p.workers n := len(idleWorkers) - 1 - if n < 0 { - waiting = p.Running() >= p.Cap() - } else { + if n >= 0 { w = idleWorkers[n] idleWorkers[n] = nil p.workers = idleWorkers[:n] p.lock.Unlock() - } - - if waiting { + } else if p.Running() < p.Cap() { + p.lock.Unlock() + if cacheWorker := p.workerCache.Get(); cacheWorker != nil { + w = cacheWorker.(*WorkerWithFunc) + } else { + w = &WorkerWithFunc{ + pool: p, + args: make(chan interface{}, workerChanCap), + } + w.run() + } + } else { for { p.cond.Wait() l := len(p.workers) - 1 @@ -220,16 +226,6 @@ func (p *PoolWithFunc) retrieveWorker() *WorkerWithFunc { break } p.lock.Unlock() - } else if w == nil { - p.lock.Unlock() - if cacheWorker := p.workerCache.Get(); cacheWorker != nil { - return cacheWorker.(*WorkerWithFunc) - } - w = &WorkerWithFunc{ - pool: p, - args: make(chan interface{}, workerChanCap), - } - w.run() } return w }