Skip to content

Commit

Permalink
📚Refactor the blocking logic in retrieveWorker function
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Jul 27, 2019
1 parent 9cfc9fa commit 61660e2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
24 changes: 9 additions & 15 deletions pool.go
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
24 changes: 9 additions & 15 deletions pool_func.go
Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 61660e2

Please sign in to comment.