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