From 61660e2109c7181a8ab5110f3520ac78f0e9ff95 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 27 Jul 2019 13:08:09 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9ARefactor=20the=20blocking=20logic?= =?UTF-8?q?=20in=20retrieveWorker=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pool.go | 24 +++++++++--------------- pool_func.go | 24 +++++++++--------------- 2 files changed, 18 insertions(+), 30 deletions(-) diff --git a/pool.go b/pool.go index 1acda9d..bde1137 100644 --- a/pool.go +++ b/pool.go @@ -211,7 +211,6 @@ func (p *Pool) retrieveWorker() *Worker { p.lock.Lock() idleWorkers := p.workers n := len(idleWorkers) - 1 -RESUME: if n >= 0 { w = idleWorkers[n] idleWorkers[n] = nil @@ -229,21 +228,16 @@ RESUME: } w.run() } else { - for { - if p.Running() == 0 { - goto RESUME - } - p.cond.Wait() - l := len(p.workers) - 1 - if l < 0 { - continue - } - w = p.workers[l] - p.workers[l] = nil - p.workers = p.workers[:l] - break + Reentry: + p.cond.Wait() + l := len(p.workers) - 1 + if l < 0 { + goto Reentry } - p.lock.Unlock() + w = p.workers[l] + p.workers[l] = nil + p.workers = p.workers[:l] + p.lock.Unlock() } return w } diff --git a/pool_func.go b/pool_func.go index e93d838..9301397 100644 --- a/pool_func.go +++ b/pool_func.go @@ -216,7 +216,6 @@ func (p *PoolWithFunc) retrieveWorker() *WorkerWithFunc { p.lock.Lock() idleWorkers := p.workers n := len(idleWorkers) - 1 -RESUME: if n >= 0 { w = idleWorkers[n] idleWorkers[n] = nil @@ -234,21 +233,16 @@ RESUME: } w.run() } else { - for { - if p.Running() == 0 { - goto RESUME - } - p.cond.Wait() - l := len(p.workers) - 1 - if l < 0 { - continue - } - w = p.workers[l] - p.workers[l] = nil - p.workers = p.workers[:l] - break + Reentry: + p.cond.Wait() + l := len(p.workers) - 1 + if l < 0 { + goto Reentry } - p.lock.Unlock() + w = p.workers[l] + p.workers[l] = nil + p.workers = p.workers[:l] + p.lock.Unlock() } return w }