From 0d826d29d27cdc83766ceb60c33444603f67ade1 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Sun, 23 Sep 2018 15:47:55 +0300 Subject: task wait group protection added --- static_pool.go | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'static_pool.go') diff --git a/static_pool.go b/static_pool.go index b6e43ddc..432c9adc 100644 --- a/static_pool.go +++ b/static_pool.go @@ -26,6 +26,7 @@ type StaticPool struct { factory Factory // active task executions + tmu sync.Mutex tasks sync.WaitGroup // workers circular allocation buf @@ -112,7 +113,10 @@ func (p *StaticPool) Workers() (workers []*Worker) { // Exec one task with given payload and context, returns result or error. func (p *StaticPool) Exec(rqs *Payload) (rsp *Payload, err error) { + p.tmu.Lock() p.tasks.Add(1) + p.tmu.Unlock() + defer p.tasks.Done() w, err := p.allocateWorker() @@ -147,7 +151,10 @@ func (p *StaticPool) Exec(rqs *Payload) (rsp *Payload, err error) { func (p *StaticPool) Destroy() { atomic.AddInt32(&p.inDestroy, 1) close(p.destroy) + + p.tmu.Lock() p.tasks.Wait() + p.tmu.Unlock() var wg sync.WaitGroup for _, w := range p.Workers() { -- cgit v1.2.3