ui/progress: Restore atomics in Counter

We switched from atomics to a mutex in #3189 because of an alignment
bug, but the new-style atomic types don't need manual alignment.
This commit is contained in:
greatroar
2025-09-16 09:46:46 +02:00
parent 1ed93bd54d
commit 0a1ce4f207

View File

@@ -1,7 +1,7 @@
package progress
import (
"sync"
"sync/atomic"
"time"
)
@@ -17,17 +17,13 @@ type Func func(value uint64, total uint64, runtime time.Duration, final bool)
// The Func is also called when SIGUSR1 (or SIGINFO, on BSD) is received.
type Counter struct {
Updater
valueMutex sync.Mutex
value uint64
max uint64
value, max atomic.Uint64
}
// NewCounter starts a new Counter.
func NewCounter(interval time.Duration, total uint64, report Func) *Counter {
c := &Counter{
max: total,
}
c := new(Counter)
c.max.Store(total)
c.Updater = *NewUpdater(interval, func(runtime time.Duration, final bool) {
v, maxV := c.Get()
report(v, maxV, runtime, final)
@@ -37,33 +33,22 @@ func NewCounter(interval time.Duration, total uint64, report Func) *Counter {
// Add v to the Counter. This method is concurrency-safe.
func (c *Counter) Add(v uint64) {
if c == nil {
return
if c != nil {
c.value.Add(v)
}
c.valueMutex.Lock()
c.value += v
c.valueMutex.Unlock()
}
// SetMax sets the maximum expected counter value. This method is concurrency-safe.
func (c *Counter) SetMax(max uint64) {
if c == nil {
return
if c != nil {
c.max.Store(max)
}
c.valueMutex.Lock()
c.max = max
c.valueMutex.Unlock()
}
// Get returns the current value and the maximum of c.
// This method is concurrency-safe.
func (c *Counter) Get() (v, max uint64) {
c.valueMutex.Lock()
v, max = c.value, c.max
c.valueMutex.Unlock()
return v, max
return c.value.Load(), c.max.Load()
}
func (c *Counter) Done() {