feat: fix Done lock; bump: v0.1.9

This commit is contained in:
jar3b 2020-11-19 16:05:45 +03:00
parent b2c16b7750
commit 7096388647
2 changed files with 17 additions and 3 deletions

View File

@ -1,11 +1,17 @@
package grawt package grawt
import (
"sync"
)
type CloseHandler struct { type CloseHandler struct {
waiter *Waiter waiter *Waiter
Quit chan struct{} Quit chan struct{}
active bool active bool
autoDone bool autoDone bool
wgDone bool
handlerFunc *func() handlerFunc *func()
mu *sync.Mutex
} }
func (ch *CloseHandler) Halt(err error) { func (ch *CloseHandler) Halt(err error) {

View File

@ -22,7 +22,9 @@ func (w *Waiter) addHandler(f *func(), autoDone bool) *CloseHandler {
Quit: make(chan struct{}, 1), Quit: make(chan struct{}, 1),
active: true, active: true,
autoDone: autoDone, autoDone: autoDone,
wgDone: false,
handlerFunc: f, handlerFunc: f,
mu: &sync.Mutex{},
} }
w.Lock() w.Lock()
@ -34,18 +36,23 @@ func (w *Waiter) addHandler(f *func(), autoDone bool) *CloseHandler {
} }
func (w *Waiter) terminateHandler(h *CloseHandler, forceWaitGroupDone bool) { func (w *Waiter) terminateHandler(h *CloseHandler, forceWaitGroupDone bool) {
h.mu.Lock()
defer h.mu.Unlock()
if !h.active { if !h.active {
if !h.wgDone {
w.waitGroup.Done()
}
return return
} }
if h.handlerFunc != nil && *h.handlerFunc != nil { if h.handlerFunc != nil && *h.handlerFunc != nil {
(*h.handlerFunc)() (*h.handlerFunc)()
} }
if h.active { close(h.Quit)
close(h.Quit)
}
if h.autoDone || forceWaitGroupDone { if h.autoDone || forceWaitGroupDone {
w.waitGroup.Done() w.waitGroup.Done()
h.wgDone = true
} }
h.active = false h.active = false
@ -87,6 +94,7 @@ func (w *Waiter) Wait() {
w.blockingMode = true w.blockingMode = true
log.Info("Waiting...") log.Info("Waiting...")
w.waitGroup.Wait() w.waitGroup.Wait()
log.Info("Terminated")
} }
func (w *Waiter) onSignal(sig os.Signal) { func (w *Waiter) onSignal(sig os.Signal) {