mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-06 12:03:41 +00:00
runtime: use the main (startup) stack for the main goroutine
Instead of always starting a new goroutine for the main goroutine, run
the main goroutine on the system stack.
The system stack is not occupied with scheduling, instead each goroutine
that wants to pause itself calls into the scheduler which will switch to
the next task (goroutine) to run, or sleeps.
There are various advantages of this over the previous system:
* When the program doesn't start a goroutine, the code size and RAM
consumption is close to what you'd get with `-scheduler=none`.
* When the program does start a goroutine, there is still a reduction
in RAM consumption because only one extra stack is needed.
* Because tasks directly switch to the next task to run, only a single
task switch is needed instead of two (goroutine -> scheduler ->
goroutine). This should improve task switching performance.
I kept the current behavior for WebAssembly/Asyncify. I looked into how
the same benefits can be realized for WebAssembly but couldn't easily
find how to do that. Maybe this can be done separately, or maybe we'll
just wait for the stack switching proposal to finish.
The code for Cortex-M is currently more complicated than I'd like, and
therefore can sometimes result in a slight increase in code size. I'd
like to fix this eventually but am still looking into good ways to do
this. I still think this change is generally beneficial because many
programs see big reductions in code size when compiling for Cortex-M.
This commit is contained in:
@@ -14,7 +14,7 @@ func markStack() {
|
||||
// Scan the current stack, and all current registers.
|
||||
scanCurrentStack()
|
||||
|
||||
if !task.OnSystemStack() {
|
||||
if !task.MainTask() {
|
||||
// Mark system stack.
|
||||
markRoots(getSystemStackPointer(), stackTop)
|
||||
}
|
||||
@@ -28,7 +28,7 @@ func scanstack(sp uintptr) {
|
||||
// Mark current stack.
|
||||
// This function is called by scanCurrentStack, after pushing all registers onto the stack.
|
||||
// Callee-saved registers have been pushed onto stack by tinygo_localscan, so this will scan them too.
|
||||
if task.OnSystemStack() {
|
||||
if task.MainTask() {
|
||||
// This is the system stack.
|
||||
// Scan all words on the stack.
|
||||
markRoots(sp, stackTop)
|
||||
|
||||
@@ -112,9 +112,7 @@ func addSleepTask(t *task.Task, duration timeUnit) {
|
||||
*q = t
|
||||
}
|
||||
|
||||
// Run the scheduler until all tasks have finished.
|
||||
func scheduler() {
|
||||
// Main scheduler loop.
|
||||
var now timeUnit
|
||||
for !schedulerDone {
|
||||
scheduleLog("")
|
||||
@@ -164,7 +162,11 @@ func scheduler() {
|
||||
|
||||
// Run the given task.
|
||||
scheduleLogTask(" run:", t)
|
||||
t.Resume()
|
||||
t.Switch()
|
||||
if GOARCH != "wasm" {
|
||||
// Don't return when using Asyncify.
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,7 +183,7 @@ func minSched() {
|
||||
}
|
||||
|
||||
scheduleLogTask(" run:", t)
|
||||
t.Resume()
|
||||
t.Switch()
|
||||
}
|
||||
scheduleLog("stop nested scheduler")
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//go:build !scheduler.none
|
||||
// +build !scheduler.none
|
||||
//go:build scheduler.asyncify
|
||||
// +build scheduler.asyncify
|
||||
|
||||
package runtime
|
||||
|
||||
@@ -16,3 +16,27 @@ func getSystemStackPointer() uintptr {
|
||||
}
|
||||
return sp
|
||||
}
|
||||
|
||||
// Pause the current task for a given time.
|
||||
//
|
||||
//go:linkname sleep time.Sleep
|
||||
func sleep(duration int64) {
|
||||
if duration <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
addSleepTask(task.Current(), nanosecondsToTicks(duration))
|
||||
task.Pause()
|
||||
}
|
||||
|
||||
// run is called by the program entry point to execute the go program.
|
||||
// The main goroutine runs on the system stack, so initAll and callMain can be
|
||||
// called directly.
|
||||
func run() {
|
||||
initHeap()
|
||||
initAll()
|
||||
callMain()
|
||||
schedulerDone = true
|
||||
}
|
||||
|
||||
const hasScheduler = true
|
||||
|
||||
Reference in New Issue
Block a user