summaryrefslogtreecommitdiff
path: root/lib/textui
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-05 19:20:25 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-25 17:28:49 -0700
commitdc586c25206a4b41a7d8c505e3700f5704134228 (patch)
tree43b1299be580366e8ae819e2f6cfefce3440d915 /lib/textui
parentdcd67db108bec3a4133542f02fe91faaa0681aa3 (diff)
containers: Add SyncValue and SyncPool types
Diffstat (limited to 'lib/textui')
-rw-r--r--lib/textui/progress.go13
1 files changed, 8 insertions, 5 deletions
diff --git a/lib/textui/progress.go b/lib/textui/progress.go
index 68d986f..1a5e7d8 100644
--- a/lib/textui/progress.go
+++ b/lib/textui/progress.go
@@ -7,10 +7,11 @@ package textui
import (
"context"
"fmt"
- "sync/atomic"
"time"
"github.com/datawire/dlib/dlog"
+
+ "git.lukeshu.com/btrfs-progs-ng/lib/containers"
)
type Stats interface {
@@ -26,7 +27,7 @@ type Progress[T Stats] struct {
cancel context.CancelFunc
done chan struct{}
- cur atomic.Value // Value[T]
+ cur containers.SyncValue[T]
oldStat T
oldLine string
}
@@ -45,7 +46,7 @@ func NewProgress[T Stats](ctx context.Context, lvl dlog.LogLevel, interval time.
}
func (p *Progress[T]) Set(val T) {
- if p.cur.Swap(val) == nil {
+ if _, hadOld := p.cur.Swap(val); !hadOld {
go p.run()
}
}
@@ -56,8 +57,10 @@ func (p *Progress[T]) Done() {
}
func (p *Progress[T]) flush(force bool) {
- //nolint:forcetypeassert // It wasn't worth it to me (yet?) to make a typed wrapper around atomic.Value.
- cur := p.cur.Load().(T)
+ cur, ok := p.cur.Load()
+ if !ok {
+ panic("should not happen")
+ }
if !force && cur == p.oldStat {
return
}