widgets/spinner: fix Spinner.frame race
It's accessed by the goroutine which increments it and the goroutine that draws the widget at the same time. Use atomic instead. Write at 0x00c00000ebc0 by goroutine 7: git.sr.ht/~sircmpwn/aerc2/widgets.(*Spinner).Start.func1() /home/simon/src/aerc2/widgets/spinner.go:50 +0x169 Previous read at 0x00c00000ebc0 by main goroutine: [failed to restore the stack] Goroutine 7 (running) created at: git.sr.ht/~sircmpwn/aerc2/widgets.(*Spinner).Start() /home/simon/src/aerc2/widgets/spinner.go:44 +0x8b git.sr.ht/~sircmpwn/aerc2/widgets.NewDirectoryList() /home/simon/src/aerc2/widgets/dirlist.go:37 +0x286 git.sr.ht/~sircmpwn/aerc2/widgets.NewAccountView() /home/simon/src/aerc2/widgets/account.go:50 +0x5ca git.sr.ht/~sircmpwn/aerc2/widgets.NewAerc() /home/simon/src/aerc2/widgets/aerc.go:60 +0x800 main.main() /home/simon/src/aerc2/aerc.go:65 +0x33e
This commit is contained in:
parent
e72574c308
commit
2159eb876e
1 changed files with 15 additions and 12 deletions
|
@ -1,6 +1,7 @@
|
||||||
package widgets
|
package widgets
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gdamore/tcell"
|
"github.com/gdamore/tcell"
|
||||||
|
@ -22,14 +23,14 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Spinner struct {
|
type Spinner struct {
|
||||||
frame int
|
frame int64 // access via atomic
|
||||||
onInvalidate func(d ui.Drawable)
|
onInvalidate func(d ui.Drawable)
|
||||||
stop chan interface{}
|
stop chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSpinner() *Spinner {
|
func NewSpinner() *Spinner {
|
||||||
spinner := Spinner{
|
spinner := Spinner{
|
||||||
stop: make(chan interface{}),
|
stop: make(chan struct{}),
|
||||||
frame: -1,
|
frame: -1,
|
||||||
}
|
}
|
||||||
return &spinner
|
return &spinner
|
||||||
|
@ -40,17 +41,17 @@ func (s *Spinner) Start() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.frame = 0
|
atomic.StoreInt64(&s.frame, 0)
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-s.stop:
|
case <-s.stop:
|
||||||
|
atomic.StoreInt64(&s.frame, -1)
|
||||||
|
s.stop <- struct{}{}
|
||||||
return
|
return
|
||||||
case <-time.After(200 * time.Millisecond):
|
case <-time.After(200 * time.Millisecond):
|
||||||
s.frame++
|
atomic.AddInt64(&s.frame, 1)
|
||||||
if s.frame >= len(frames) {
|
|
||||||
s.frame = 0
|
|
||||||
}
|
|
||||||
s.Invalidate()
|
s.Invalidate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,13 +63,13 @@ func (s *Spinner) Stop() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
s.stop <- nil
|
s.stop <- struct{}{}
|
||||||
s.frame = -1
|
<-s.stop
|
||||||
s.Invalidate()
|
s.Invalidate()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Spinner) IsRunning() bool {
|
func (s *Spinner) IsRunning() bool {
|
||||||
return s.frame != -1
|
return atomic.LoadInt64(&s.frame) != -1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Spinner) Draw(ctx *ui.Context) {
|
func (s *Spinner) Draw(ctx *ui.Context) {
|
||||||
|
@ -76,9 +77,11 @@ func (s *Spinner) Draw(ctx *ui.Context) {
|
||||||
s.Start()
|
s.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cur := int(atomic.LoadInt64(&s.frame) % int64(len(frames)))
|
||||||
|
|
||||||
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
|
||||||
col := ctx.Width()/2 - len(frames[0])/2 + 1
|
col := ctx.Width()/2 - len(frames[0])/2 + 1
|
||||||
ctx.Printf(col, 0, tcell.StyleDefault, "%s", frames[s.frame])
|
ctx.Printf(col, 0, tcell.StyleDefault, "%s", frames[cur])
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Spinner) OnInvalidate(onInvalidate func(d ui.Drawable)) {
|
func (s *Spinner) OnInvalidate(onInvalidate func(d ui.Drawable)) {
|
||||||
|
|
Loading…
Reference in a new issue