mirror of
https://github.com/octoleo/syncthing.git
synced 2025-01-02 22:50:18 +00:00
lib/sync: Make the clock a function pointer
This commit is contained in:
parent
6a9716e8a1
commit
c366933416
@ -19,11 +19,7 @@ import (
|
|||||||
"github.com/sasha-s/go-deadlock"
|
"github.com/sasha-s/go-deadlock"
|
||||||
)
|
)
|
||||||
|
|
||||||
type clock interface {
|
var timeNow = time.Now
|
||||||
Now() time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
var defaultClock clock = (*standardClock)(nil)
|
|
||||||
|
|
||||||
type Mutex interface {
|
type Mutex interface {
|
||||||
Lock()
|
Lock()
|
||||||
@ -86,7 +82,7 @@ func (h holder) String() string {
|
|||||||
if h.at == "" {
|
if h.at == "" {
|
||||||
return "not held"
|
return "not held"
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("at %s goid: %d for %s", h.at, h.goid, defaultClock.Now().Sub(h.time))
|
return fmt.Sprintf("at %s goid: %d for %s", h.at, h.goid, timeNow().Sub(h.time))
|
||||||
}
|
}
|
||||||
|
|
||||||
type loggedMutex struct {
|
type loggedMutex struct {
|
||||||
@ -101,7 +97,7 @@ func (m *loggedMutex) Lock() {
|
|||||||
|
|
||||||
func (m *loggedMutex) Unlock() {
|
func (m *loggedMutex) Unlock() {
|
||||||
currentHolder := m.holder.Load().(holder)
|
currentHolder := m.holder.Load().(holder)
|
||||||
duration := defaultClock.Now().Sub(currentHolder.time)
|
duration := timeNow().Sub(currentHolder.time)
|
||||||
if duration >= threshold {
|
if duration >= threshold {
|
||||||
l.Debugf("Mutex held for %v. Locked at %s unlocked at %s", duration, currentHolder.at, getHolder().at)
|
l.Debugf("Mutex held for %v. Locked at %s unlocked at %s", duration, currentHolder.at, getHolder().at)
|
||||||
}
|
}
|
||||||
@ -125,7 +121,7 @@ type loggedRWMutex struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *loggedRWMutex) Lock() {
|
func (m *loggedRWMutex) Lock() {
|
||||||
start := defaultClock.Now()
|
start := timeNow()
|
||||||
|
|
||||||
atomic.StoreInt32(&m.logUnlockers, 1)
|
atomic.StoreInt32(&m.logUnlockers, 1)
|
||||||
m.RWMutex.Lock()
|
m.RWMutex.Lock()
|
||||||
@ -153,7 +149,7 @@ func (m *loggedRWMutex) Lock() {
|
|||||||
|
|
||||||
func (m *loggedRWMutex) Unlock() {
|
func (m *loggedRWMutex) Unlock() {
|
||||||
currentHolder := m.holder.Load().(holder)
|
currentHolder := m.holder.Load().(holder)
|
||||||
duration := defaultClock.Now().Sub(currentHolder.time)
|
duration := timeNow().Sub(currentHolder.time)
|
||||||
if duration >= threshold {
|
if duration >= threshold {
|
||||||
l.Debugf("RWMutex held for %v. Locked at %s unlocked at %s", duration, currentHolder.at, getHolder().at)
|
l.Debugf("RWMutex held for %v. Locked at %s unlocked at %s", duration, currentHolder.at, getHolder().at)
|
||||||
}
|
}
|
||||||
@ -205,9 +201,9 @@ type loggedWaitGroup struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (wg *loggedWaitGroup) Wait() {
|
func (wg *loggedWaitGroup) Wait() {
|
||||||
start := defaultClock.Now()
|
start := timeNow()
|
||||||
wg.WaitGroup.Wait()
|
wg.WaitGroup.Wait()
|
||||||
duration := defaultClock.Now().Sub(start)
|
duration := timeNow().Sub(start)
|
||||||
if duration >= threshold {
|
if duration >= threshold {
|
||||||
l.Debugf("WaitGroup took %v at %s", duration, getHolder())
|
l.Debugf("WaitGroup took %v at %s", duration, getHolder())
|
||||||
}
|
}
|
||||||
@ -219,7 +215,7 @@ func getHolder() holder {
|
|||||||
return holder{
|
return holder{
|
||||||
at: fmt.Sprintf("%s:%d", file, line),
|
at: fmt.Sprintf("%s:%d", file, line),
|
||||||
goid: goid(),
|
goid: goid(),
|
||||||
time: defaultClock.Now(),
|
time: timeNow(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -300,9 +296,3 @@ func (w *TimeoutCondWaiter) Wait() bool {
|
|||||||
func (w *TimeoutCondWaiter) Stop() {
|
func (w *TimeoutCondWaiter) Stop() {
|
||||||
w.timer.Stop()
|
w.timer.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
type standardClock struct{}
|
|
||||||
|
|
||||||
func (*standardClock) Now() time.Time {
|
|
||||||
return time.Now()
|
|
||||||
}
|
|
||||||
|
@ -57,10 +57,10 @@ func TestTypes(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestMutex(t *testing.T) {
|
func TestMutex(t *testing.T) {
|
||||||
oldClock := defaultClock
|
oldClock := timeNow
|
||||||
clock := newTestClock()
|
clock := newTestClock()
|
||||||
defaultClock = clock
|
timeNow = clock.Now
|
||||||
defer func() { defaultClock = oldClock }()
|
defer func() { timeNow = oldClock }()
|
||||||
|
|
||||||
debug = true
|
debug = true
|
||||||
l.SetDebug("sync", true)
|
l.SetDebug("sync", true)
|
||||||
@ -97,10 +97,10 @@ func TestMutex(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRWMutex(t *testing.T) {
|
func TestRWMutex(t *testing.T) {
|
||||||
oldClock := defaultClock
|
oldClock := timeNow
|
||||||
clock := newTestClock()
|
clock := newTestClock()
|
||||||
defaultClock = clock
|
timeNow = clock.Now
|
||||||
defer func() { defaultClock = oldClock }()
|
defer func() { timeNow = oldClock }()
|
||||||
|
|
||||||
debug = true
|
debug = true
|
||||||
l.SetDebug("sync", true)
|
l.SetDebug("sync", true)
|
||||||
@ -170,10 +170,10 @@ func TestRWMutex(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestWaitGroup(t *testing.T) {
|
func TestWaitGroup(t *testing.T) {
|
||||||
oldClock := defaultClock
|
oldClock := timeNow
|
||||||
clock := newTestClock()
|
clock := newTestClock()
|
||||||
defaultClock = clock
|
timeNow = clock.Now
|
||||||
defer func() { defaultClock = oldClock }()
|
defer func() { timeNow = oldClock }()
|
||||||
|
|
||||||
debug = true
|
debug = true
|
||||||
l.SetDebug("sync", true)
|
l.SetDebug("sync", true)
|
||||||
|
Loading…
Reference in New Issue
Block a user