File: timers.go

package info (click to toggle)
golang-github-desertbit-timer 1.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 88 kB
  • sloc: makefile: 2
file content (215 lines) | stat: -rw-r--r-- 3,911 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package timer

import (
	"sync"
	"time"
)

var (
	mutex       sync.Mutex
	timers      []*Timer
	rescheduleC = make(chan struct{}, 1)
)

func init() {
	go timerRoutine()
}

// Add the timer to the heap.
func addTimer(t *Timer, d time.Duration) {
	t.when = time.Now().Add(d)

	mutex.Lock()
	addTimerLocked(t)
	mutex.Unlock()
}

func addTimerLocked(t *Timer) {
	t.i = len(timers)
	timers = append(timers, t)
	siftupTimer(t.i)

	// Reschedule if this is the next timer in the heap.
	if t.i == 0 {
		reschedule()
	}
}

// Delete timer t from the heap.
// It returns true if t was removed, false if t wasn't even there.
// Do not need to update the timer routine: if it wakes up early, no big deal.
func delTimer(t *Timer) (b bool) {
	mutex.Lock()
	b = delTimerLocked(t)
	mutex.Unlock()
	return
}

// Delete timer t from the heap.
// It returns true if t was removed, false if t wasn't even there.
// Do not need to update the timer routine: if it wakes up early, no big deal.
func delTimerLocked(t *Timer) bool {
	// t may not be registered anymore and may have
	// a bogus i (typically 0, if generated by Go).
	// Verify it before proceeding.
	i := t.i
	last := len(timers) - 1
	if i < 0 || i > last || timers[i] != t {
		return false
	}
	if i != last {
		timers[i] = timers[last]
		timers[i].i = i
	}
	timers[last] = nil
	timers = timers[:last]
	if i != last {
		siftupTimer(i)
		siftdownTimer(i)
	}
	return true
}

// Reset the timer to the new timeout duration.
// This clears the channel.
func resetTimer(t *Timer, d time.Duration) (b bool) {
	mutex.Lock()
	b = delTimerLocked(t)
	t.reset()
	t.when = time.Now().Add(d)
	addTimerLocked(t)
	mutex.Unlock()
	return
}

func reschedule() {
	// Do not block if there is already a pending reschedule request.
	select {
	case rescheduleC <- struct{}{}:
	default:
	}
}

func timerRoutine() {
	var now time.Time
	var last int

	var sleepTimerActive bool
	sleepTimer := time.NewTimer(time.Second)
	sleepTimer.Stop()

Loop:
	for {
		select {
		case <-sleepTimer.C:

		case <-rescheduleC:
			// If not yet received a value from sleepTimer.C, the timer must be
			// stopped and—if Stop reports that the timer expired before being
			// stopped—the channel explicitly drained.
			if !sleepTimer.Stop() && sleepTimerActive {
				<-sleepTimer.C
			}
		}
		sleepTimerActive = false

	Reschedule:
		now = time.Now()

		mutex.Lock()
		if len(timers) == 0 {
			mutex.Unlock()
			continue Loop
		}

		t := timers[0]
		delta := t.when.Sub(now)

		// Sleep if not expired.
		if delta > 0 {
			mutex.Unlock()
			sleepTimer.Reset(delta)
			sleepTimerActive = true
			continue Loop
		}

		// Timer expired. Trigger the timer's function callback.
		t.f(&now)

		// Remove from heap.
		last = len(timers) - 1
		if last > 0 {
			timers[0] = timers[last]
			timers[0].i = 0
		}
		timers[last] = nil
		timers = timers[:last]
		if last > 0 {
			siftdownTimer(0)
		}
		t.i = -1 // mark as removed

		mutex.Unlock()

		// Reschedule immediately.
		goto Reschedule
	}
}

// Heap maintenance algorithms.
// Based on golang source /runtime/time.go

func siftupTimer(i int) {
	tmp := timers[i]
	when := tmp.when

	var p int
	for i > 0 {
		p = (i - 1) / 4 // parent
		if !when.Before(timers[p].when) {
			break
		}
		timers[i] = timers[p]
		timers[i].i = i
		timers[p] = tmp
		timers[p].i = p
		i = p
	}
}

func siftdownTimer(i int) {
	n := len(timers)
	when := timers[i].when
	tmp := timers[i]
	for {
		c := i*4 + 1 // left child
		c3 := c + 2  // mid child
		if c >= n {
			break
		}
		w := timers[c].when
		if c+1 < n && timers[c+1].when.Before(w) {
			w = timers[c+1].when
			c++
		}
		if c3 < n {
			w3 := timers[c3].when
			if c3+1 < n && timers[c3+1].when.Before(w3) {
				w3 = timers[c3+1].when
				c3++
			}
			if w3.Before(w) {
				w = w3
				c = c3
			}
		}
		if !w.Before(when) {
			break
		}
		timers[i] = timers[c]
		timers[i].i = i
		timers[c] = tmp
		timers[c].i = c
		i = c
	}
}