mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-04 19:17:47 +00:00
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:
@@ -6,6 +6,8 @@ const schedulerDebug = false
|
||||
|
||||
var mainExited bool
|
||||
|
||||
var timerQueue *timerNode
|
||||
|
||||
// Simple logging, for debugging.
|
||||
func scheduleLog(msg string) {
|
||||
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.
|
||||
func Goexit() {
|
||||
panicOrGoexit(nil, panicGoexit)
|
||||
|
||||
@@ -32,7 +32,6 @@ var (
|
||||
runqueue task.Queue
|
||||
sleepQueue *task.Task
|
||||
sleepQueueBaseTime timeUnit
|
||||
timerQueue *timerNode
|
||||
)
|
||||
|
||||
// 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.
|
||||
func addTimer(tim *timerNode) {
|
||||
mask := interrupt.Disable()
|
||||
|
||||
// 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
|
||||
timerQueueAdd(tim)
|
||||
interrupt.Restore(mask)
|
||||
}
|
||||
|
||||
// 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.
|
||||
func removeTimer(tim *timer) bool {
|
||||
removedTimer := false
|
||||
mask := interrupt.Disable()
|
||||
for t := &timerQueue; *t != nil; t = &(*t).next {
|
||||
if (*t).timer == tim {
|
||||
scheduleLog("removed timer")
|
||||
*t = (*t).next
|
||||
removedTimer = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !removedTimer {
|
||||
scheduleLog("did not remove timer")
|
||||
}
|
||||
removedTimer := timerQueueRemove(tim)
|
||||
interrupt.Restore(mask)
|
||||
return removedTimer
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user