Make mk2ser wait for goroutine to finish on Close.

This commit is contained in:
Hendrik van Wyk
2017-09-16 13:32:23 +02:00
parent 337c897afc
commit 0bd6b448b8

View File

@@ -25,6 +25,7 @@ type mk2Ser struct {
locked bool
sync.RWMutex
infochan chan *Mk2Info
wg sync.WaitGroup
}
func NewMk2Connection(dev io.ReadWriter) (Mk2If, error) {
@@ -38,6 +39,7 @@ func NewMk2Connection(dev io.ReadWriter) (Mk2If, error) {
mk2.setTarget()
mk2.run = make(chan struct{})
mk2.infochan = make(chan *Mk2Info)
mk2.wg.Add(1)
go mk2.frameLock()
return mk2, nil
}
@@ -50,8 +52,10 @@ func (mk2 *mk2Ser) frameLock() {
for {
select {
case <-mk2.run:
break
mk2.wg.Done()
return
default:
}
if mk2.locked {
size = mk2.readByte()
l, err := io.ReadFull(mk2.p, frame[0:int(size)+1])
@@ -83,12 +87,12 @@ func (mk2 *mk2Ser) frameLock() {
size = tmp
}
}
}
}
// Close Mk2
func (mk2 *mk2Ser) Close() {
close(mk2.run)
mk2.wg.Wait()
}
// Returns last known state with all reported errors since previous poll.