From 8cd2a462b9190c6615625ce60db8b6b38540c544 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sat, 8 May 2021 20:05:34 +0200 Subject: [PATCH] runtime: remove the asyncScheduler constant There is no reason to specialize this per chip as it is only ever used for JavaScript. Not only that, it is causing confusion and is yet another quirk to learn when porting the runtime to a new microcontroller. --- src/runtime/runtime_arm7tdmi.go | 2 -- src/runtime/runtime_atsamd21.go | 2 -- src/runtime/runtime_atsamd51.go | 2 -- src/runtime/runtime_avr.go | 2 -- src/runtime/runtime_cortexm_qemu.go | 2 -- src/runtime/runtime_esp32.go | 2 -- src/runtime/runtime_esp8266.go | 2 -- src/runtime/runtime_fe310.go | 2 -- src/runtime/runtime_k210.go | 2 -- src/runtime/runtime_mimxrt1062.go | 2 -- src/runtime/runtime_nintendoswitch.go | 2 -- src/runtime/runtime_nrf.go | 2 -- src/runtime/runtime_nxpmk66f18.go | 3 --- src/runtime/runtime_stm32f103.go | 2 -- src/runtime/runtime_stm32f405.go | 2 -- src/runtime/runtime_stm32f407.go | 2 -- src/runtime/runtime_stm32f7x2.go | 2 -- src/runtime/runtime_stm32l0.go | 2 -- src/runtime/runtime_stm32l4x2.go | 2 -- src/runtime/runtime_stm32l5x2.go | 2 -- src/runtime/runtime_tinygoriscv_qemu.go | 2 -- src/runtime/runtime_unix.go | 2 -- src/runtime/runtime_wasm_js.go | 2 -- src/runtime/runtime_wasm_wasi.go | 5 +---- src/runtime/scheduler.go | 8 +++++++- 25 files changed, 8 insertions(+), 52 deletions(-) diff --git a/src/runtime/runtime_arm7tdmi.go b/src/runtime/runtime_arm7tdmi.go index 70b76404..705d5edf 100644 --- a/src/runtime/runtime_arm7tdmi.go +++ b/src/runtime/runtime_arm7tdmi.go @@ -71,8 +71,6 @@ func ticks() timeUnit { return 0 } -const asyncScheduler = false - func sleepTicks(d timeUnit) { // TODO } diff --git a/src/runtime/runtime_atsamd21.go b/src/runtime/runtime_atsamd21.go index 452532b2..117f5a6d 100644 --- a/src/runtime/runtime_atsamd21.go +++ b/src/runtime/runtime_atsamd21.go @@ -243,8 +243,6 @@ var rtcOverflows volatile.Register32 // number of times the RTC wrapped around var timerWakeup volatile.Register8 -const asyncScheduler = false - // ticksToNanoseconds converts RTC ticks (at 32768Hz) to nanoseconds. func ticksToNanoseconds(ticks timeUnit) int64 { // The following calculation is actually the following, but with both sides diff --git a/src/runtime/runtime_atsamd51.go b/src/runtime/runtime_atsamd51.go index 9babc686..f59c82f2 100644 --- a/src/runtime/runtime_atsamd51.go +++ b/src/runtime/runtime_atsamd51.go @@ -232,8 +232,6 @@ var rtcOverflows volatile.Register32 // number of times the RTC wrapped around var timerWakeup volatile.Register8 -const asyncScheduler = false - // ticksToNanoseconds converts RTC ticks (at 32768Hz) to nanoseconds. func ticksToNanoseconds(ticks timeUnit) int64 { // The following calculation is actually the following, but with both sides diff --git a/src/runtime/runtime_avr.go b/src/runtime/runtime_avr.go index 8365b7f8..4aedf811 100644 --- a/src/runtime/runtime_avr.go +++ b/src/runtime/runtime_avr.go @@ -58,8 +58,6 @@ func init() { initUART() } -const asyncScheduler = false - const tickNanos = 1024 * 16384 // roughly 16ms in nanoseconds func ticksToNanoseconds(ticks timeUnit) int64 { diff --git a/src/runtime/runtime_cortexm_qemu.go b/src/runtime/runtime_cortexm_qemu.go index bc4fc2d1..634144fa 100644 --- a/src/runtime/runtime_cortexm_qemu.go +++ b/src/runtime/runtime_cortexm_qemu.go @@ -27,8 +27,6 @@ func main() { abort() } -const asyncScheduler = false - func ticksToNanoseconds(ticks timeUnit) int64 { return int64(ticks) } diff --git a/src/runtime/runtime_esp32.go b/src/runtime/runtime_esp32.go index 6f2ad6de..ca7d7c14 100644 --- a/src/runtime/runtime_esp32.go +++ b/src/runtime/runtime_esp32.go @@ -97,8 +97,6 @@ func ticks() timeUnit { return timeUnit(uint64(esp.TIMG0.T0LO.Get()) | uint64(esp.TIMG0.T0HI.Get())<<32) } -const asyncScheduler = false - func nanosecondsToTicks(ns int64) timeUnit { // Calculate the number of ticks from the number of nanoseconds. At a 80MHz // APB clock, that's 25 nanoseconds per tick with a timer prescaler of 2: diff --git a/src/runtime/runtime_esp8266.go b/src/runtime/runtime_esp8266.go index 2ad10963..28af3c55 100644 --- a/src/runtime/runtime_esp8266.go +++ b/src/runtime/runtime_esp8266.go @@ -89,8 +89,6 @@ func ticks() timeUnit { return currentTime } -const asyncScheduler = false - const tickNanos = 3200 // time.Second / (80MHz / 256) func ticksToNanoseconds(ticks timeUnit) int64 { diff --git a/src/runtime/runtime_fe310.go b/src/runtime/runtime_fe310.go index 437f03bf..d2e0d91e 100644 --- a/src/runtime/runtime_fe310.go +++ b/src/runtime/runtime_fe310.go @@ -100,8 +100,6 @@ func putchar(c byte) { machine.UART0.WriteByte(c) } -const asyncScheduler = false - var timerWakeup volatile.Register8 func ticks() timeUnit { diff --git a/src/runtime/runtime_k210.go b/src/runtime/runtime_k210.go index 457a1e17..93236181 100644 --- a/src/runtime/runtime_k210.go +++ b/src/runtime/runtime_k210.go @@ -112,8 +112,6 @@ func putchar(c byte) { machine.UART0.WriteByte(c) } -const asyncScheduler = false - var timerWakeup volatile.Register8 func ticks() timeUnit { diff --git a/src/runtime/runtime_mimxrt1062.go b/src/runtime/runtime_mimxrt1062.go index 7fbd0d9e..a9cc063d 100644 --- a/src/runtime/runtime_mimxrt1062.go +++ b/src/runtime/runtime_mimxrt1062.go @@ -10,8 +10,6 @@ import ( "unsafe" ) -const asyncScheduler = false - //go:extern _svectors var _svectors [0]byte diff --git a/src/runtime/runtime_nintendoswitch.go b/src/runtime/runtime_nintendoswitch.go index e5120d42..99cb3b0b 100644 --- a/src/runtime/runtime_nintendoswitch.go +++ b/src/runtime/runtime_nintendoswitch.go @@ -6,8 +6,6 @@ import "unsafe" type timeUnit int64 -const asyncScheduler = false - const ( // Handles infoTypeTotalMemorySize = 6 // Total amount of memory available for process. diff --git a/src/runtime/runtime_nrf.go b/src/runtime/runtime_nrf.go index b55ca18e..2e7765b6 100644 --- a/src/runtime/runtime_nrf.go +++ b/src/runtime/runtime_nrf.go @@ -67,8 +67,6 @@ func putchar(c byte) { machine.UART0.WriteByte(c) } -const asyncScheduler = false - func sleepTicks(d timeUnit) { for d != 0 { ticks := uint32(d) & 0x7fffff // 23 bits (to be on the safe side) diff --git a/src/runtime/runtime_nxpmk66f18.go b/src/runtime/runtime_nxpmk66f18.go index cff830d2..1cb52334 100644 --- a/src/runtime/runtime_nxpmk66f18.go +++ b/src/runtime/runtime_nxpmk66f18.go @@ -232,9 +232,6 @@ func putchar(c byte) { machine.PutcharUART(&machine.UART0, c) } -// ??? -const asyncScheduler = false - func abort() { println("!!! ABORT !!!") diff --git a/src/runtime/runtime_stm32f103.go b/src/runtime/runtime_stm32f103.go index a7c8563d..50d2b968 100644 --- a/src/runtime/runtime_stm32f103.go +++ b/src/runtime/runtime_stm32f103.go @@ -24,8 +24,6 @@ const ( type arrtype = uint32 -const asyncScheduler = false - func init() { initCLK() diff --git a/src/runtime/runtime_stm32f405.go b/src/runtime/runtime_stm32f405.go index 7596c864..799e4eac 100644 --- a/src/runtime/runtime_stm32f405.go +++ b/src/runtime/runtime_stm32f405.go @@ -80,8 +80,6 @@ const ( type arrtype = uint32 -const asyncScheduler = false - func init() { initOSC() // configure oscillators initCLK() diff --git a/src/runtime/runtime_stm32f407.go b/src/runtime/runtime_stm32f407.go index 5a30a81c..a1af7bbf 100644 --- a/src/runtime/runtime_stm32f407.go +++ b/src/runtime/runtime_stm32f407.go @@ -42,8 +42,6 @@ const ( type arrtype = uint32 -const asyncScheduler = false - func init() { initCLK() diff --git a/src/runtime/runtime_stm32f7x2.go b/src/runtime/runtime_stm32f7x2.go index a80c7e92..034986b3 100644 --- a/src/runtime/runtime_stm32f7x2.go +++ b/src/runtime/runtime_stm32f7x2.go @@ -41,8 +41,6 @@ const ( type arrtype = uint32 -const asyncScheduler = false - func init() { initCLK() diff --git a/src/runtime/runtime_stm32l0.go b/src/runtime/runtime_stm32l0.go index c7045087..be61749e 100644 --- a/src/runtime/runtime_stm32l0.go +++ b/src/runtime/runtime_stm32l0.go @@ -13,8 +13,6 @@ const ( type arrtype = uint16 -const asyncScheduler = false - func putchar(c byte) { machine.UART0.WriteByte(c) } diff --git a/src/runtime/runtime_stm32l4x2.go b/src/runtime/runtime_stm32l4x2.go index 5a6eb1ad..9aad880c 100644 --- a/src/runtime/runtime_stm32l4x2.go +++ b/src/runtime/runtime_stm32l4x2.go @@ -66,8 +66,6 @@ const ( type arrtype = uint32 -const asyncScheduler = false - func init() { initCLK() diff --git a/src/runtime/runtime_stm32l5x2.go b/src/runtime/runtime_stm32l5x2.go index 1bf9c7ee..1ecc662b 100644 --- a/src/runtime/runtime_stm32l5x2.go +++ b/src/runtime/runtime_stm32l5x2.go @@ -42,8 +42,6 @@ const ( type arrtype = uint32 -const asyncScheduler = false - func init() { initCLK() diff --git a/src/runtime/runtime_tinygoriscv_qemu.go b/src/runtime/runtime_tinygoriscv_qemu.go index 27caa398..0b443838 100644 --- a/src/runtime/runtime_tinygoriscv_qemu.go +++ b/src/runtime/runtime_tinygoriscv_qemu.go @@ -24,8 +24,6 @@ func main() { abort() } -const asyncScheduler = false - func ticksToNanoseconds(ticks timeUnit) int64 { return int64(ticks) } diff --git a/src/runtime/runtime_unix.go b/src/runtime/runtime_unix.go index 584f0338..c185c4ab 100644 --- a/src/runtime/runtime_unix.go +++ b/src/runtime/runtime_unix.go @@ -123,8 +123,6 @@ func putchar(c byte) { _putchar(int(c)) } -const asyncScheduler = false - func ticksToNanoseconds(ticks timeUnit) int64 { // The OS API works in nanoseconds so no conversion necessary. return int64(ticks) diff --git a/src/runtime/runtime_wasm_js.go b/src/runtime/runtime_wasm_js.go index 5e2eb1f5..b39aef0a 100644 --- a/src/runtime/runtime_wasm_js.go +++ b/src/runtime/runtime_wasm_js.go @@ -40,8 +40,6 @@ func go_scheduler() { scheduler() } -const asyncScheduler = true - func ticksToNanoseconds(ticks timeUnit) int64 { // The JavaScript API works in float64 milliseconds, so convert to // nanoseconds first before converting to a timeUnit (which is a float64), diff --git a/src/runtime/runtime_wasm_wasi.go b/src/runtime/runtime_wasm_wasi.go index b1a58886..76d0a452 100644 --- a/src/runtime/runtime_wasm_wasi.go +++ b/src/runtime/runtime_wasm_wasi.go @@ -59,10 +59,7 @@ func nanosecondsToTicks(ns int64) timeUnit { return timeUnit(ns) } -const ( - asyncScheduler = false - timePrecisionNanoseconds = 1000 // TODO: how can we determine the appropriate `precision`? -) +const timePrecisionNanoseconds = 1000 // TODO: how can we determine the appropriate `precision`? var ( sleepTicksSubscription = __wasi_subscription_t{ diff --git a/src/runtime/scheduler.go b/src/runtime/scheduler.go index 42cc0fef..c5286d33 100644 --- a/src/runtime/scheduler.go +++ b/src/runtime/scheduler.go @@ -20,6 +20,10 @@ import ( const schedulerDebug = false +// On JavaScript, we can't do a blocking sleep. Instead we have to return and +// queue a new scheduler invocation using setTimeout. +const asyncScheduler = GOOS == "js" + var schedulerDone bool // Queues used by the scheduler. @@ -138,6 +142,7 @@ func scheduler() { if t == nil { if sleepQueue == nil { if asyncScheduler { + // JavaScript is treated specially, see below. return } waitForEvents() @@ -154,7 +159,8 @@ func scheduler() { if asyncScheduler { // The sleepTicks function above only sets a timeout at which // point the scheduler will be called again. It does not really - // sleep. + // sleep. So instead of sleeping, we return and expect to be + // called again. break } continue