Skip to content

Commit 8166af1

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 7601c6d commit 8166af1

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
@@ -4,6 +4,8 @@ import "internal/task"
44

55
const schedulerDebug = false
66

7+
var timerQueue *timerNode
8+
79
// Simple logging, for debugging.
810
func scheduleLog(msg string) {
911
if schedulerDebug {
@@ -25,6 +27,34 @@ func scheduleLogChan(msg string, ch *channel, t *task.Task) {
2527
}
2628
}
2729

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

src/runtime/scheduler_cooperative.go

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ var (
2828
runqueue task.Queue
2929
sleepQueue *task.Task
3030
sleepQueueBaseTime timeUnit
31-
timerQueue *timerNode
3231
)
3332

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

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

0 commit comments

Comments
 (0)