From 0064d3f30cb971cde1ec8214224497a158bc2649 Mon Sep 17 00:00:00 2001 From: hhvn Date: Sat, 27 Jan 2024 15:17:35 +0000 Subject: Each bar has drawing goroutine. Mutexes eliminated. --- bar/bar.go | 54 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 19 deletions(-) (limited to 'bar/bar.go') diff --git a/bar/bar.go b/bar/bar.go index 0fd930a..c77e2e9 100644 --- a/bar/bar.go +++ b/bar/bar.go @@ -2,7 +2,6 @@ package bar // import "hhvn.uk/hbspbar/bar" import ( "fmt" - "sync" "image" "errors" "image/color" @@ -17,11 +16,16 @@ import ( var bars map[int]*bar +type drawinfo struct { + State *bspc.State + Blocks *status.Blocks +} + type bar struct { id int w *drw.Window i *image.RGBA - drawing sync.Mutex + redraw chan drawinfo } func (b *bar) init(state *bspc.State) error { @@ -38,10 +42,26 @@ func (b *bar) init(state *bspc.State) error { rect := image.Rect(0, 0, int(mon.Rectangle.Width), int(config.H)) b.i = image.NewRGBA(rect) + b.redraw = make(chan drawinfo, 20) + + go func(){ + for d := range b.redraw { + // Gobble up excess redraws + gobble: for { + select { + case d = <- b.redraw: + default: break gobble + } + } + + b.draw(d) + } + }() + return nil } -func (b *bar) getmon(state *bspc.State) (*bspc.Monitor, error) { +func (b bar) getmon(state *bspc.State) (*bspc.Monitor, error) { return state.GetMon(b.id) } @@ -53,12 +73,12 @@ func (b *bar) drawRect(x, y, w, h int, c color.Color, fill bool) { drw.DrawRect(b.i, x, y, w, h, c, fill) } -func (b *bar) draw(state *bspc.State) { +func (b bar) draw(d drawinfo) { var bg color.Color var filled bool - b.drawing.Lock() - defer b.drawing.Unlock() + state := d.State + blocks := d.Blocks mon, err := b.getmon(state) if err != nil { @@ -117,10 +137,9 @@ func (b *bar) draw(state *bspc.State) { cx += ax + 2 } - status.Mutex.Lock() cx = int(mon.Rectangle.Width) - 6 - for i := len(status.Status) - 1; i >= 0; i -= 1 { - s, ok := status.Status[i] + for i := blocks.Len() - 1; i >= 0; i -= 1 { + s, ok := blocks.Get(i) if ok { cx -= s.W cx -= config.StatusPad @@ -133,8 +152,8 @@ func (b *bar) draw(state *bspc.State) { config.Status, true) cx = int(mon.Rectangle.Width) - 6 - for i := len(status.Status) - 1; i >= 0; i -= 1 { - s, ok := status.Status[i] + for i := blocks.Len() - 1; i >= 0; i -= 1 { + s, ok := blocks.Get(i) if ok { cx -= s.W @@ -143,7 +162,6 @@ func (b *bar) draw(state *bspc.State) { cx -= config.StatusPad } } - status.Mutex.Unlock() b.w.Paint(b.i) } @@ -170,8 +188,8 @@ var Handle handle var finalstate *bspc.State = nil func init() { - var state *bspc.State - state = nil + var state *bspc.State = nil + var blocks *status.Blocks = nil Handle.Create = make(chan int) Handle.Destroy = make(chan int) @@ -218,14 +236,14 @@ func init() { Handle.Err <- fmt.Errorf("Couldn't create window: %s\n", err) return } - case <- status.Updated: + case blocks = <- status.NewBlocks: for _, b := range bars { - go b.draw(state) + b.redraw <- drawinfo{state, blocks} } case state = <- Handle.NewState: finalstate = state for _, b := range bars { - go b.draw(state) + b.redraw <- drawinfo{state, blocks} } } } @@ -242,8 +260,6 @@ func create(state *bspc.State, id int) (error) { return err } - go b.draw(state) - if bars == nil { bars = make(map[int]*bar) } -- cgit v1.2.3