Skip to content
This repository has been archived by the owner on May 18, 2023. It is now read-only.

improve use of mutex #54

Merged
merged 1 commit into from
Apr 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ func (c *clock) WithTimeout(parent context.Context, t time.Duration) (context.Co
// Mock represents a mock clock that only moves forward programmically.
// It can be preferable to a real-time clock when testing time-based functionality.
type Mock struct {
mu sync.Mutex
// mu protects all other fields in this struct, and the data that they
// point to.
mu sync.Mutex

now time.Time // current time
timers clockTimers // tickers & timers
}
Expand All @@ -89,7 +92,9 @@ func NewMock() *Mock {
// This should only be called from a single goroutine at a time.
func (m *Mock) Add(d time.Duration) {
// Calculate the final current time.
m.mu.Lock()
t := m.now.Add(d)
m.mu.Unlock()

// Continue to execute timers until there are no more before the new time.
for {
Expand Down Expand Up @@ -240,6 +245,8 @@ func (m *Mock) Timer(d time.Duration) *Timer {
return t
}

// removeClockTimer removes a timer from m.timers. m.mu MUST be held
// when this method is called.
func (m *Mock) removeClockTimer(t clockTimer) {
for i, timer := range m.timers {
if timer == t {
Expand Down Expand Up @@ -373,7 +380,9 @@ func (t *internalTicker) Tick(now time.Time) {
case t.c <- now:
default:
}
t.mock.mu.Lock()
t.next = now.Add(t.d)
t.mock.mu.Unlock()
gosched()
}

Expand Down