mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-17 03:03:27 +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 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)
|
||||||
|
|||||||
@@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user