mirror of
https://github.com/octoleo/restic.git
synced 2024-11-06 05:17:50 +00:00
Merge pull request #3189 from restic/fix-counter-segfault
ui/progress: Use mutex instead of atomic
This commit is contained in:
commit
69f9d269eb
@ -3,7 +3,6 @@ package progress
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/restic/restic/internal/debug"
|
"github.com/restic/restic/internal/debug"
|
||||||
@ -25,6 +24,8 @@ type Counter struct {
|
|||||||
stopped chan struct{} // Closed by run.
|
stopped chan struct{} // Closed by run.
|
||||||
stop chan struct{} // Close to stop run.
|
stop chan struct{} // Close to stop run.
|
||||||
tick *time.Ticker
|
tick *time.Ticker
|
||||||
|
|
||||||
|
valueMutex sync.Mutex
|
||||||
value uint64
|
value uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +53,10 @@ func (c *Counter) Add(v uint64) {
|
|||||||
if c == nil {
|
if c == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
atomic.AddUint64(&c.value, v)
|
|
||||||
|
c.valueMutex.Lock()
|
||||||
|
c.value += v
|
||||||
|
c.valueMutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Done tells a Counter to stop and waits for it to report its final value.
|
// Done tells a Counter to stop and waits for it to report its final value.
|
||||||
@ -66,7 +70,13 @@ func (c *Counter) Done() {
|
|||||||
*c = Counter{} // Prevent reuse.
|
*c = Counter{} // Prevent reuse.
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Counter) get() uint64 { return atomic.LoadUint64(&c.value) }
|
func (c *Counter) get() uint64 {
|
||||||
|
c.valueMutex.Lock()
|
||||||
|
v := c.value
|
||||||
|
c.valueMutex.Unlock()
|
||||||
|
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Counter) run() {
|
func (c *Counter) run() {
|
||||||
defer close(c.stopped)
|
defer close(c.stopped)
|
||||||
|
Loading…
Reference in New Issue
Block a user