Skip to content

Commit 3cc9c44

Browse files
committed
runtime: refactor timerQueue
Move common functions to scheduler.go. They will be used both from the cooperative and from the threads scheduler.
1 parent c9bb33a commit 3cc9c44

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

src/runtime/scheduler.go

+30
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const schedulerDebug = false
66

77
var mainExited bool
88

9+
var timerQueue *timerNode
10+
911
// Simple logging, for debugging.
1012
func scheduleLog(msg string) {
1113
if schedulerDebug {
@@ -27,6 +29,34 @@ func scheduleLogChan(msg string, ch *channel, t *task.Task) {
2729
}
2830
}
2931

32+
func timerQueueAdd(tn *timerNode) {
33+
q := &timerQueue
34+
for ; *q != nil; q = &(*q).next {
35+
if tn.whenTicks() < (*q).whenTicks() {
36+
// this will finish earlier than the next - insert here
37+
break
38+
}
39+
}
40+
tn.next = *q
41+
*q = tn
42+
}
43+
44+
func timerQueueRemove(t *timer) bool {
45+
removedTimer := false
46+
for q := &timerQueue; *q != nil; q = &(*q).next {
47+
if (*q).timer == t {
48+
scheduleLog("removed timer")
49+
*q = (*q).next
50+
removedTimer = true
51+
break
52+
}
53+
}
54+
if !removedTimer {
55+
scheduleLog("did not remove timer")
56+
}
57+
return removedTimer
58+
}
59+
3060
// Goexit terminates the currently running goroutine. No other goroutines are affected.
3161
func Goexit() {
3262
panicOrGoexit(nil, panicGoexit)

src/runtime/scheduler_cooperative.go

+2-24
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ var (
3232
runqueue task.Queue
3333
sleepQueue *task.Task
3434
sleepQueueBaseTime timeUnit
35-
timerQueue *timerNode
3635
)
3736

3837
// deadlock is called when a goroutine cannot proceed any more, but is in theory
@@ -100,36 +99,15 @@ func addSleepTask(t *task.Task, duration timeUnit) {
10099
// sleepQueue.
101100
func addTimer(tim *timerNode) {
102101
mask := interrupt.Disable()
103-
104-
// Add to timer queue.
105-
q := &timerQueue
106-
for ; *q != nil; q = &(*q).next {
107-
if tim.whenTicks() < (*q).whenTicks() {
108-
// this will finish earlier than the next - insert here
109-
break
110-
}
111-
}
112-
tim.next = *q
113-
*q = tim
102+
timerQueueAdd(tim)
114103
interrupt.Restore(mask)
115104
}
116105

117106
// removeTimer is the implementation of time.stopTimer. It removes a timer from
118107
// the timer queue, returning true if the timer is present in the timer queue.
119108
func removeTimer(tim *timer) bool {
120-
removedTimer := false
121109
mask := interrupt.Disable()
122-
for t := &timerQueue; *t != nil; t = &(*t).next {
123-
if (*t).timer == tim {
124-
scheduleLog("removed timer")
125-
*t = (*t).next
126-
removedTimer = true
127-
break
128-
}
129-
}
130-
if !removedTimer {
131-
scheduleLog("did not remove timer")
132-
}
110+
removedTimer := timerQueueRemove(tim)
133111
interrupt.Restore(mask)
134112
return removedTimer
135113
}

0 commit comments

Comments
 (0)