mirror of https://github.com/panjf2000/ants.git
📚Refactor the blocking logic in retrieveWorker function
This commit is contained in:
parent
9cfc9fa643
commit
61660e2109
24
pool.go
24
pool.go
|
@ -211,7 +211,6 @@ func (p *Pool) retrieveWorker() *Worker {
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
idleWorkers := p.workers
|
idleWorkers := p.workers
|
||||||
n := len(idleWorkers) - 1
|
n := len(idleWorkers) - 1
|
||||||
RESUME:
|
|
||||||
if n >= 0 {
|
if n >= 0 {
|
||||||
w = idleWorkers[n]
|
w = idleWorkers[n]
|
||||||
idleWorkers[n] = nil
|
idleWorkers[n] = nil
|
||||||
|
@ -229,21 +228,16 @@ RESUME:
|
||||||
}
|
}
|
||||||
w.run()
|
w.run()
|
||||||
} else {
|
} else {
|
||||||
for {
|
Reentry:
|
||||||
if p.Running() == 0 {
|
p.cond.Wait()
|
||||||
goto RESUME
|
l := len(p.workers) - 1
|
||||||
}
|
if l < 0 {
|
||||||
p.cond.Wait()
|
goto Reentry
|
||||||
l := len(p.workers) - 1
|
|
||||||
if l < 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
w = p.workers[l]
|
|
||||||
p.workers[l] = nil
|
|
||||||
p.workers = p.workers[:l]
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
p.lock.Unlock()
|
w = p.workers[l]
|
||||||
|
p.workers[l] = nil
|
||||||
|
p.workers = p.workers[:l]
|
||||||
|
p.lock.Unlock()
|
||||||
}
|
}
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
24
pool_func.go
24
pool_func.go
|
@ -216,7 +216,6 @@ func (p *PoolWithFunc) retrieveWorker() *WorkerWithFunc {
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
idleWorkers := p.workers
|
idleWorkers := p.workers
|
||||||
n := len(idleWorkers) - 1
|
n := len(idleWorkers) - 1
|
||||||
RESUME:
|
|
||||||
if n >= 0 {
|
if n >= 0 {
|
||||||
w = idleWorkers[n]
|
w = idleWorkers[n]
|
||||||
idleWorkers[n] = nil
|
idleWorkers[n] = nil
|
||||||
|
@ -234,21 +233,16 @@ RESUME:
|
||||||
}
|
}
|
||||||
w.run()
|
w.run()
|
||||||
} else {
|
} else {
|
||||||
for {
|
Reentry:
|
||||||
if p.Running() == 0 {
|
p.cond.Wait()
|
||||||
goto RESUME
|
l := len(p.workers) - 1
|
||||||
}
|
if l < 0 {
|
||||||
p.cond.Wait()
|
goto Reentry
|
||||||
l := len(p.workers) - 1
|
|
||||||
if l < 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
w = p.workers[l]
|
|
||||||
p.workers[l] = nil
|
|
||||||
p.workers = p.workers[:l]
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
p.lock.Unlock()
|
w = p.workers[l]
|
||||||
|
p.workers[l] = nil
|
||||||
|
p.workers = p.workers[:l]
|
||||||
|
p.lock.Unlock()
|
||||||
}
|
}
|
||||||
return w
|
return w
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue