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 70b764042..705d5edfa 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 452532b2d..117f5a6de 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 9babc686b..f59c82f2b 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 8365b7f86..4aedf811b 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 bc4fc2d1c..634144fa9 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 6f2ad6def..ca7d7c14e 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 2ad109639..28af3c554 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 437f03bf5..d2e0d91ed 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 457a1e177..932361818 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 7fbd0d9e1..a9cc063df 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 e5120d425..99cb3b0b6 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 b55ca18e9..2e7765b67 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 cff830d28..1cb523349 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 a7c8563df..50d2b968f 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 7596c8644..799e4eac9 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 5a30a81c2..a1af7bbf0 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 a80c7e921..034986b37 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 c7045087b..be61749e6 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 5a6eb1ad9..9aad880ce 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 1bf9c7ee9..1ecc662b2 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 27caa398a..0b4438387 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 584f03383..c185c4ab7 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 5e2eb1f51..b39aef0a5 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 b1a58886e..76d0a4520 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 42cc0fef8..c5286d33b 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