runtime: refactor timerQueue

Move common functions to scheduler.go. They will be used both from the
cooperative and from the threads scheduler.
This commit is contained in:
Ayke van Laethem
2024-11-02 13:45:17 +01:00
parent c9bb33a73c
commit 3cc9c44c26
2 changed files with 32 additions and 24 deletions
+30
View File
@@ -6,6 +6,8 @@ const schedulerDebug = false
var mainExited bool var mainExited bool
var timerQueue *timerNode
// Simple logging, for debugging. // Simple logging, for debugging.
func scheduleLog(msg string) { func scheduleLog(msg string) {
if schedulerDebug { if schedulerDebug {
@@ -27,6 +29,34 @@ func scheduleLogChan(msg string, ch *channel, t *task.Task) {
} }
} }
func timerQueueAdd(tn *timerNode) {
q := &timerQueue
for ; *q != nil; q = &(*q).next {
if tn.whenTicks() < (*q).whenTicks() {
// this will finish earlier than the next - insert here
break
}
}
tn.next = *q
*q = tn
}
func timerQueueRemove(t *timer) bool {
removedTimer := false
for q := &timerQueue; *q != nil; q = &(*q).next {
if (*q).timer == t {
scheduleLog("removed timer")
*q = (*q).next
removedTimer = true
break
}
}
if !removedTimer {
scheduleLog("did not remove timer")
}
return removedTimer
}
// Goexit terminates the currently running goroutine. No other goroutines are affected. // Goexit terminates the currently running goroutine. No other goroutines are affected.
func Goexit() { func Goexit() {
panicOrGoexit(nil, panicGoexit) panicOrGoexit(nil, panicGoexit)
+2 -24
View File
@@ -32,7 +32,6 @@ var (
runqueue task.Queue runqueue task.Queue
sleepQueue *task.Task sleepQueue *task.Task
sleepQueueBaseTime timeUnit sleepQueueBaseTime timeUnit
timerQueue *timerNode
) )
// deadlock is called when a goroutine cannot proceed any more, but is in theory // 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) {
// sleepQueue. // sleepQueue.
func addTimer(tim *timerNode) { func addTimer(tim *timerNode) {
mask := interrupt.Disable() mask := interrupt.Disable()
timerQueueAdd(tim)
// Add to timer queue.
q := &timerQueue
for ; *q != nil; q = &(*q).next {
if tim.whenTicks() < (*q).whenTicks() {
// this will finish earlier than the next - insert here
break
}
}
tim.next = *q
*q = tim
interrupt.Restore(mask) interrupt.Restore(mask)
} }
// removeTimer is the implementation of time.stopTimer. It removes a timer from // removeTimer is the implementation of time.stopTimer. It removes a timer from
// the timer queue, returning true if the timer is present in the timer queue. // the timer queue, returning true if the timer is present in the timer queue.
func removeTimer(tim *timer) bool { func removeTimer(tim *timer) bool {
removedTimer := false
mask := interrupt.Disable() mask := interrupt.Disable()
for t := &timerQueue; *t != nil; t = &(*t).next { removedTimer := timerQueueRemove(tim)
if (*t).timer == tim {
scheduleLog("removed timer")
*t = (*t).next
removedTimer = true
break
}
}
if !removedTimer {
scheduleLog("did not remove timer")
}
interrupt.Restore(mask) interrupt.Restore(mask)
return removedTimer return removedTimer
} }