runtime: move scheduler code around

This moves all scheduler code into a separate file that is only compiled
when there's a scheduler in use (the tasks or asyncify scheduler, which
are both cooperative). The main goal of this change is to make it easier
to add a new "scheduler" based on OS threads.

It also fixes a few subtle issues with `-gc=none`:

  - Gosched() panicked. This is now fixed to just return immediately
    (the only logical thing to do when there's only one goroutine).
  - Timers aren't supported without a scheduler, but the relevant code
    was still present and would happily add a timer to the queue. It
    just never ran. So now it exits with a runtime error, similar to any
    blocking operation.
This commit is contained in:
Ayke van Laethem
2024-10-25 11:14:57 +02:00
committed by Ron Evans
parent 6593cf22fa
commit d1fe02df23
12 changed files with 315 additions and 277 deletions
+48 -10
View File
@@ -2,6 +2,19 @@
package runtime
import "internal/task"
const hasScheduler = false
// run is called by the program entry point to execute the go program.
// With the "none" scheduler, init and the main function are invoked directly.
func run() {
initHeap()
initAll()
callMain()
mainExited = true
}
//go:linkname sleep time.Sleep
func sleep(duration int64) {
if duration <= 0 {
@@ -11,18 +24,43 @@ func sleep(duration int64) {
sleepTicks(nanosecondsToTicks(duration))
}
func deadlock() {
// The only goroutine available is deadlocked.
runtimePanic("all goroutines are asleep - deadlock!")
}
func scheduleTask(t *task.Task) {
// Pause() will panic, so this should not be reachable.
}
func Gosched() {
// There are no other goroutines, so there's nothing to schedule.
}
func addTimer(tim *timerNode) {
runtimePanic("timers not supported without a scheduler")
}
func removeTimer(tim *timer) bool {
runtimePanic("timers not supported without a scheduler")
return false
}
func schedulerRunQueue() *task.Queue {
// This function is not actually used, it is only called when hasScheduler
// is true.
runtimePanic("unreachable: no runqueue without a scheduler")
return nil
}
func scheduler(returnAtDeadlock bool) {
// The scheduler should never be run when using -scheduler=none. Meaning,
// this code should be unreachable.
runtimePanic("unreachable: scheduler must not be called with the 'none' scheduler")
}
// getSystemStackPointer returns the current stack pointer of the system stack.
// This is always the current stack pointer.
func getSystemStackPointer() uintptr {
return getCurrentStackPointer()
}
// run is called by the program entry point to execute the go program.
// With the "none" scheduler, init and the main function are invoked directly.
func run() {
initHeap()
initAll()
callMain()
}
const hasScheduler = false