From 9a33c61229cae83ce00cd86ad619fa1825b95421 Mon Sep 17 00:00:00 2001 From: Wolfy-J Date: Tue, 12 Jun 2018 23:53:24 +0300 Subject: better multithread control --- static_pool.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) (limited to 'static_pool.go') diff --git a/static_pool.go b/static_pool.go index 24665c01..19fc1d13 100644 --- a/static_pool.go +++ b/static_pool.go @@ -22,9 +22,6 @@ type StaticPool struct { // worker command creator cmd func() *exec.Cmd - // lsn is optional callback to handle worker create/destruct/error events. - lsn func(event int, ctx interface{}) - // creates and connects to workers factory Factory @@ -42,6 +39,10 @@ type StaticPool struct { // pool is being destroying inDestroy int32 + + // lsn is optional callback to handle worker create/destruct/error events. + mul sync.Mutex + lsn func(event int, ctx interface{}) } // NewPool creates new worker pool and task multiplexer. StaticPool will initiate with one worker. @@ -75,6 +76,9 @@ func NewPool(cmd func() *exec.Cmd, factory Factory, cfg Config) (*StaticPool, er // AddListener attaches pool event watcher. func (p *StaticPool) Listen(l func(event int, ctx interface{})) { + p.mul.Lock() + defer p.mul.Unlock() + p.lsn = l } @@ -268,7 +272,11 @@ func (p *StaticPool) destroying() bool { // throw invokes event handler if any. func (p *StaticPool) throw(event int, ctx interface{}) { - if p.lsn != nil { - p.lsn(event, ctx) + p.mul.Lock() + lsn := p.lsn + p.mul.Unlock() + + if lsn != nil { + lsn(event, ctx) } } -- cgit v1.2.3