diff --git a/src/runtime/runtime_arm7tdmi.go b/src/runtime/runtime_arm7tdmi.go index cf2ff681..d7629ad6 100644 --- a/src/runtime/runtime_arm7tdmi.go +++ b/src/runtime/runtime_arm7tdmi.go @@ -30,20 +30,16 @@ var _sidata [0]byte //go:extern _edata var _edata [0]byte +func postinit() {} + // Entry point for Go. Initialize all packages and call main.main(). //go:export main func main() { // Initialize .data and .bss sections. preinit() - // Run initializers of all packages. - initAll() - - // Compiler-generated call to main.main(). - go callMain() - - // Run the scheduler. - scheduler() + // Run program. + run() } func preinit() { diff --git a/src/runtime/runtime_atsamd21.go b/src/runtime/runtime_atsamd21.go index a3aa6c97..5c535cf1 100644 --- a/src/runtime/runtime_atsamd21.go +++ b/src/runtime/runtime_atsamd21.go @@ -13,12 +13,12 @@ import ( type timeUnit int64 +func postinit() {} + //go:export Reset_Handler func main() { preinit() - initAll() - go callMain() - scheduler() + run() abort() } diff --git a/src/runtime/runtime_atsamd51.go b/src/runtime/runtime_atsamd51.go index 23d91109..a605eee5 100644 --- a/src/runtime/runtime_atsamd51.go +++ b/src/runtime/runtime_atsamd51.go @@ -12,12 +12,12 @@ import ( type timeUnit int64 +func postinit() {} + //go:export Reset_Handler func main() { preinit() - initAll() - go callMain() - scheduler() + run() abort() } diff --git a/src/runtime/runtime_avr.go b/src/runtime/runtime_avr.go index 09f91f7a..09d4b599 100644 --- a/src/runtime/runtime_avr.go +++ b/src/runtime/runtime_avr.go @@ -39,9 +39,7 @@ var _ebss [0]byte //go:export main func main() { preinit() - initAll() - postinit() - callMain() + run() abort() } diff --git a/src/runtime/runtime_cortexm_qemu.go b/src/runtime/runtime_cortexm_qemu.go index 973c20ac..80365a7d 100644 --- a/src/runtime/runtime_cortexm_qemu.go +++ b/src/runtime/runtime_cortexm_qemu.go @@ -17,12 +17,12 @@ const tickMicros = 1 var timestamp timeUnit +func postinit() {} + //go:export Reset_Handler func main() { preinit() - initAll() - go callMain() - scheduler() + run() arm.SemihostingCall(arm.SemihostingReportException, arm.SemihostingApplicationExit) abort() } diff --git a/src/runtime/runtime_fe310.go b/src/runtime/runtime_fe310.go index 88295c9c..62673ad3 100644 --- a/src/runtime/runtime_fe310.go +++ b/src/runtime/runtime_fe310.go @@ -31,6 +31,8 @@ var _sidata [0]byte //go:extern _edata var _edata [0]byte +func postinit() {} + //go:export main func main() { // Zero the PLIC enable bits on startup: they are not zeroed at reset. @@ -51,9 +53,7 @@ func main() { preinit() initPeripherals() - initAll() - go callMain() - scheduler() + run() abort() } diff --git a/src/runtime/runtime_nrf.go b/src/runtime/runtime_nrf.go index 9b01ded7..9cfa457a 100644 --- a/src/runtime/runtime_nrf.go +++ b/src/runtime/runtime_nrf.go @@ -17,13 +17,13 @@ const tickMicros = 1024 * 32 //go:linkname systemInit SystemInit func systemInit() +func postinit() {} + //go:export Reset_Handler func main() { systemInit() preinit() - initAll() - go callMain() - scheduler() + run() abort() } diff --git a/src/runtime/runtime_stm32.go b/src/runtime/runtime_stm32.go index 11e686f0..bd529ceb 100644 --- a/src/runtime/runtime_stm32.go +++ b/src/runtime/runtime_stm32.go @@ -4,11 +4,11 @@ package runtime type timeUnit int64 +func postinit() {} + //go:export Reset_Handler func main() { preinit() - initAll() - go callMain() - scheduler() + run() abort() } diff --git a/src/runtime/runtime_unix.go b/src/runtime/runtime_unix.go index a88f243f..bb34a0d5 100644 --- a/src/runtime/runtime_unix.go +++ b/src/runtime/runtime_unix.go @@ -45,17 +45,12 @@ type timespec struct { const CLOCK_MONOTONIC_RAW = 4 +func postinit() {} + // Entry point for Go. Initialize all packages and call main.main(). //go:export main func main() int { - // Run initializers of all packages. - initAll() - - // Compiler-generated call to main.main(). - go callMain() - - // Run scheduler. - scheduler() + run() // For libc compatibility. return 0 diff --git a/src/runtime/runtime_wasm.go b/src/runtime/runtime_wasm.go index c01fa932..90a25a82 100644 --- a/src/runtime/runtime_wasm.go +++ b/src/runtime/runtime_wasm.go @@ -18,11 +18,11 @@ type wasiIOVec struct { //export fd_write func fd_write(id uint32, iovs *wasiIOVec, iovs_len uint, nwritten *uint) (errno uint) +func postinit() {} + //export _start func _start() { - initAll() - go callMain() - scheduler() + run() } // Using global variables to avoid heap allocation. diff --git a/src/runtime/scheduler_any.go b/src/runtime/scheduler_any.go index d541dc6a..b96a4eba 100644 --- a/src/runtime/scheduler_any.go +++ b/src/runtime/scheduler_any.go @@ -10,3 +10,14 @@ func sleep(duration int64) { addSleepTask(task.Current(), duration) task.Pause() } + +// run is called by the program entry point to execute the go program. +// With a scheduler, init and the main function are invoked in a goroutine before starting the scheduler. +func run() { + initAll() + postinit() + go func() { + callMain() + }() + scheduler() +} diff --git a/src/runtime/scheduler_none.go b/src/runtime/scheduler_none.go index 222867dd..a86110f2 100644 --- a/src/runtime/scheduler_none.go +++ b/src/runtime/scheduler_none.go @@ -12,3 +12,11 @@ func sleep(duration int64) { 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() { + initAll() + postinit() + callMain() +}