Skip to content

Commit 8b39f07

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 fb199f5 commit 8b39f07

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

src/runtime/scheduler.go

Lines changed: 30 additions & 0 deletions
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(tim *timerNode) {
33+
q := &timerQueue
34+
for ; *q != nil; q = &(*q).next {
35+
if tim.whenTicks() < (*q).whenTicks() {
36+
// this will finish earlier than the next - insert here
37+
break
38+
}
39+
}
40+
tim.next = *q
41+
*q = tim
42+
}
43+
44+
func timerQueueRemove(tim *timer) bool {
45+
removedTimer := false
46+
for t := &timerQueue; *t != nil; t = &(*t).next {
47+
if (*t).timer == tim {
48+
scheduleLog("removed timer")
49+
*t = (*t).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

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ var (
2626
runqueue task.Queue
2727
sleepQueue *task.Task
2828
sleepQueueBaseTime timeUnit
29-
timerQueue *timerNode
3029
)
3130

3231
// deadlock is called when a goroutine cannot proceed any more, but is in theory
@@ -94,36 +93,15 @@ func addSleepTask(t *task.Task, duration timeUnit) {
9493
// sleepQueue.
9594
func addTimer(tim *timerNode) {
9695
mask := interrupt.Disable()
97-
98-
// Add to timer queue.
99-
q := &timerQueue
100-
for ; *q != nil; q = &(*q).next {
101-
if tim.whenTicks() < (*q).whenTicks() {
102-
// this will finish earlier than the next - insert here
103-
break
104-
}
105-
}
106-
tim.next = *q
107-
*q = tim
96+
timerQueueAdd(tim)
10897
interrupt.Restore(mask)
10998
}
11099

111100
// removeTimer is the implementation of time.stopTimer. It removes a timer from
112101
// the timer queue, returning true if the timer is present in the timer queue.
113102
func removeTimer(tim *timer) bool {
114-
removedTimer := false
115103
mask := interrupt.Disable()
116-
for t := &timerQueue; *t != nil; t = &(*t).next {
117-
if (*t).timer == tim {
118-
scheduleLog("removed timer")
119-
*t = (*t).next
120-
removedTimer = true
121-
break
122-
}
123-
}
124-
if !removedTimer {
125-
scheduleLog("did not remove timer")
126-
}
104+
removedTimer := timerQueueRemove(tim)
127105
interrupt.Restore(mask)
128106
return removedTimer
129107
}

0 commit comments

Comments
 (0)