From 41e501aaf4fc218a121aeaff9e8a01c9a18da874 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sat, 12 Apr 2025 12:54:06 +0200 Subject: [PATCH] runtime: move timeUnit to a single place The timeUnit is now the same type everywhere. Move it to a single place and add some documentation to it. --- src/runtime/runtime.go | 11 +++++++++++ src/runtime/runtime_arm7tdmi.go | 2 -- src/runtime/runtime_atsamd21.go | 2 -- src/runtime/runtime_atsamd51.go | 2 -- src/runtime/runtime_avr.go | 3 --- src/runtime/runtime_avrtiny.go | 2 -- src/runtime/runtime_cortexm_qemu.go | 2 -- src/runtime/runtime_esp32xx.go | 2 -- src/runtime/runtime_esp8266.go | 2 -- src/runtime/runtime_fe310.go | 2 -- src/runtime/runtime_k210.go | 2 -- src/runtime/runtime_mimxrt1062_time.go | 2 -- src/runtime/runtime_nintendoswitch.go | 2 -- src/runtime/runtime_nrf.go | 2 -- src/runtime/runtime_nrf52840.go | 2 -- src/runtime/runtime_rp2040.go | 2 -- src/runtime/runtime_rp2350.go | 2 -- src/runtime/runtime_stm32.go | 2 -- src/runtime/runtime_tinygoriscv_qemu.go | 6 ++---- src/runtime/runtime_tkey.go | 2 -- src/runtime/runtime_unix.go | 2 -- src/runtime/runtime_wasip1.go | 2 -- src/runtime/runtime_wasip2.go | 2 -- src/runtime/runtime_wasm_js.go | 2 -- src/runtime/runtime_wasm_unknown.go | 5 ----- src/runtime/runtime_windows.go | 2 -- src/runtime/time_nxpmk66f18.go | 2 -- 27 files changed, 13 insertions(+), 58 deletions(-) diff --git a/src/runtime/runtime.go b/src/runtime/runtime.go index 99ca34f2c..2dcf313ff 100644 --- a/src/runtime/runtime.go +++ b/src/runtime/runtime.go @@ -8,6 +8,17 @@ import ( const Compiler = "tinygo" +// Unit for the 'ticks' and 'sleepTicks' functions. +// +// This is the native time unit for the given system. One timeUnit tick might be +// 1ns or 100ns on a desktop system, or 1/32768s on baremetal systems with a +// low-power RTC. Many other tick durations are possible. +// +// Conversion from time units to nanoseconds and back is done using +// ticksToNanoseconds and nanosecondsToTicks, which need to be implemented for +// each system as needed. +type timeUnit int64 + // The compiler will fill this with calls to the initialization function of each // package. func initAll() diff --git a/src/runtime/runtime_arm7tdmi.go b/src/runtime/runtime_arm7tdmi.go index d63b229c2..fe0b648b5 100644 --- a/src/runtime/runtime_arm7tdmi.go +++ b/src/runtime/runtime_arm7tdmi.go @@ -7,8 +7,6 @@ import ( "unsafe" ) -type timeUnit int64 - func putchar(c byte) { // dummy, TODO } diff --git a/src/runtime/runtime_atsamd21.go b/src/runtime/runtime_atsamd21.go index d30fc7f6f..e0aeb0869 100644 --- a/src/runtime/runtime_atsamd21.go +++ b/src/runtime/runtime_atsamd21.go @@ -12,8 +12,6 @@ import ( "unsafe" ) -type timeUnit int64 - //export Reset_Handler func main() { preinit() diff --git a/src/runtime/runtime_atsamd51.go b/src/runtime/runtime_atsamd51.go index 151f81581..5c8a3d8b2 100644 --- a/src/runtime/runtime_atsamd51.go +++ b/src/runtime/runtime_atsamd51.go @@ -11,8 +11,6 @@ import ( "runtime/volatile" ) -type timeUnit int64 - //export Reset_Handler func main() { arm.SCB.CPACR.Set(0) // disable FPU if it is enabled diff --git a/src/runtime/runtime_avr.go b/src/runtime/runtime_avr.go index 43d35d7b9..7274748af 100644 --- a/src/runtime/runtime_avr.go +++ b/src/runtime/runtime_avr.go @@ -12,9 +12,6 @@ import ( const BOARD = "arduino" -// timeUnit in nanoseconds -type timeUnit int64 - // Watchdog timer periods. These can be off by a large margin (hence the jump // between 64ms and 125ms which is not an exact double), so don't rely on this // for accurate time keeping. diff --git a/src/runtime/runtime_avrtiny.go b/src/runtime/runtime_avrtiny.go index 8ce324938..e8ffd17cd 100644 --- a/src/runtime/runtime_avrtiny.go +++ b/src/runtime/runtime_avrtiny.go @@ -22,8 +22,6 @@ import ( "runtime/volatile" ) -type timeUnit int64 - //export main func main() { // Initialize RTC. diff --git a/src/runtime/runtime_cortexm_qemu.go b/src/runtime/runtime_cortexm_qemu.go index 22bfee167..f15e92be8 100644 --- a/src/runtime/runtime_cortexm_qemu.go +++ b/src/runtime/runtime_cortexm_qemu.go @@ -11,8 +11,6 @@ import ( "unsafe" ) -type timeUnit int64 - var timestamp timeUnit //export Reset_Handler diff --git a/src/runtime/runtime_esp32xx.go b/src/runtime/runtime_esp32xx.go index e4fe8835a..f1c62243f 100644 --- a/src/runtime/runtime_esp32xx.go +++ b/src/runtime/runtime_esp32xx.go @@ -8,8 +8,6 @@ import ( "unsafe" ) -type timeUnit int64 - // Initialize .bss: zero-initialized global variables. // The .data section has already been loaded by the ROM bootloader. func clearbss() { diff --git a/src/runtime/runtime_esp8266.go b/src/runtime/runtime_esp8266.go index b12a8b68f..6dd0f83e0 100644 --- a/src/runtime/runtime_esp8266.go +++ b/src/runtime/runtime_esp8266.go @@ -9,8 +9,6 @@ import ( "unsafe" ) -type timeUnit int64 - var currentTime timeUnit = 0 func putchar(c byte) { diff --git a/src/runtime/runtime_fe310.go b/src/runtime/runtime_fe310.go index 218962edb..99d253af0 100644 --- a/src/runtime/runtime_fe310.go +++ b/src/runtime/runtime_fe310.go @@ -14,8 +14,6 @@ import ( "runtime/volatile" ) -type timeUnit int64 - //export main func main() { // Zero the PLIC enable bits on startup: they are not zeroed at reset. diff --git a/src/runtime/runtime_k210.go b/src/runtime/runtime_k210.go index 8ee79b938..61b6bfa3e 100644 --- a/src/runtime/runtime_k210.go +++ b/src/runtime/runtime_k210.go @@ -13,8 +13,6 @@ import ( "unsafe" ) -type timeUnit int64 - //export main func main() { diff --git a/src/runtime/runtime_mimxrt1062_time.go b/src/runtime/runtime_mimxrt1062_time.go index feaf68dbd..382ba1c6f 100644 --- a/src/runtime/runtime_mimxrt1062_time.go +++ b/src/runtime/runtime_mimxrt1062_time.go @@ -10,8 +10,6 @@ import ( "unsafe" ) -type timeUnit int64 - const ( lastCycle = SYSTICK_FREQ/1000 - 1 cyclesPerMicro = CORE_FREQ / 1000000 diff --git a/src/runtime/runtime_nintendoswitch.go b/src/runtime/runtime_nintendoswitch.go index 2d3677bf0..074e18287 100644 --- a/src/runtime/runtime_nintendoswitch.go +++ b/src/runtime/runtime_nintendoswitch.go @@ -4,8 +4,6 @@ package runtime import "unsafe" -type timeUnit int64 - 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 729c6bb20..a295b9966 100644 --- a/src/runtime/runtime_nrf.go +++ b/src/runtime/runtime_nrf.go @@ -10,8 +10,6 @@ import ( "runtime/volatile" ) -type timeUnit int64 - //go:linkname systemInit SystemInit func systemInit() diff --git a/src/runtime/runtime_nrf52840.go b/src/runtime/runtime_nrf52840.go index 41c36fe5f..4ac7314a2 100644 --- a/src/runtime/runtime_nrf52840.go +++ b/src/runtime/runtime_nrf52840.go @@ -11,8 +11,6 @@ import ( "runtime/volatile" ) -type timeUnit int64 - //go:linkname systemInit SystemInit func systemInit() diff --git a/src/runtime/runtime_rp2040.go b/src/runtime/runtime_rp2040.go index 1d36a771e..fb1d1ddbd 100644 --- a/src/runtime/runtime_rp2040.go +++ b/src/runtime/runtime_rp2040.go @@ -14,8 +14,6 @@ func machineTicks() uint64 // machineLightSleep is provided by package machine. func machineLightSleep(uint64) -type timeUnit int64 - // ticks returns the number of ticks (microseconds) elapsed since power up. func ticks() timeUnit { t := machineTicks() diff --git a/src/runtime/runtime_rp2350.go b/src/runtime/runtime_rp2350.go index f70ec413c..500e4c9f2 100644 --- a/src/runtime/runtime_rp2350.go +++ b/src/runtime/runtime_rp2350.go @@ -14,8 +14,6 @@ func machineTicks() uint64 // machineLightSleep is provided by package machine. func machineLightSleep(uint64) -type timeUnit int64 - // ticks returns the number of ticks (microseconds) elapsed since power up. func ticks() timeUnit { t := machineTicks() diff --git a/src/runtime/runtime_stm32.go b/src/runtime/runtime_stm32.go index c212ead72..c4f2ef660 100644 --- a/src/runtime/runtime_stm32.go +++ b/src/runtime/runtime_stm32.go @@ -4,8 +4,6 @@ package runtime import "device/arm" -type timeUnit int64 - //export Reset_Handler func main() { preinit() diff --git a/src/runtime/runtime_tinygoriscv_qemu.go b/src/runtime/runtime_tinygoriscv_qemu.go index e58e75351..a77ad71f5 100644 --- a/src/runtime/runtime_tinygoriscv_qemu.go +++ b/src/runtime/runtime_tinygoriscv_qemu.go @@ -11,10 +11,6 @@ import ( // This file implements the VirtIO RISC-V interface implemented in QEMU, which // is an interface designed for emulation. -// One tick is 100ns by default in QEMU. -// (This is not a standard, just the default used by QEMU). -type timeUnit int64 - //export main func main() { preinit() @@ -61,6 +57,8 @@ func handleInterrupt() { riscv.MCAUSE.Set(0) } +// One tick is 100ns by default in QEMU. +// (This is not a standard, just the default used by QEMU). func ticksToNanoseconds(ticks timeUnit) int64 { return int64(ticks) * 100 // one tick is 100ns } diff --git a/src/runtime/runtime_tkey.go b/src/runtime/runtime_tkey.go index ba8c5e944..87628c62e 100644 --- a/src/runtime/runtime_tkey.go +++ b/src/runtime/runtime_tkey.go @@ -10,8 +10,6 @@ import ( "runtime/volatile" ) -type timeUnit int64 - //export main func main() { preinit() diff --git a/src/runtime/runtime_unix.go b/src/runtime/runtime_unix.go index 3724c457c..99f28411f 100644 --- a/src/runtime/runtime_unix.go +++ b/src/runtime/runtime_unix.go @@ -61,8 +61,6 @@ func clock_gettime(clk_id int32, ts *timespec) { } } -type timeUnit int64 - // Note: tv_sec and tv_nsec normally vary in size by platform. However, we're // using the time64 variant (see clock_gettime above), so the formats are the // same between 32-bit and 64-bit architectures. diff --git a/src/runtime/runtime_wasip1.go b/src/runtime/runtime_wasip1.go index 92adb9bef..d680fad17 100644 --- a/src/runtime/runtime_wasip1.go +++ b/src/runtime/runtime_wasip1.go @@ -6,8 +6,6 @@ import ( "unsafe" ) -type timeUnit int64 - // libc constructors // //export __wasm_call_ctors diff --git a/src/runtime/runtime_wasip2.go b/src/runtime/runtime_wasip2.go index 296f4a45b..46ce3d853 100644 --- a/src/runtime/runtime_wasip2.go +++ b/src/runtime/runtime_wasip2.go @@ -12,8 +12,6 @@ import ( "internal/cm" ) -type timeUnit int64 - func init() { wasiclirun.Exports.Run = func() cm.BoolResult { callMain() diff --git a/src/runtime/runtime_wasm_js.go b/src/runtime/runtime_wasm_js.go index 1766cd7e6..92359e4da 100644 --- a/src/runtime/runtime_wasm_js.go +++ b/src/runtime/runtime_wasm_js.go @@ -2,8 +2,6 @@ package runtime -type timeUnit int64 - var handleEvent func() //go:linkname setEventHandler syscall/js.setEventHandler diff --git a/src/runtime/runtime_wasm_unknown.go b/src/runtime/runtime_wasm_unknown.go index 27e248579..70c42f2a9 100644 --- a/src/runtime/runtime_wasm_unknown.go +++ b/src/runtime/runtime_wasm_unknown.go @@ -5,8 +5,6 @@ package runtime // TODO: this is essentially reactor mode wasm. So we might want to support // -buildmode=c-shared (and default to it). -type timeUnit int64 - // libc constructors // //export __wasm_call_ctors @@ -24,9 +22,6 @@ func nanosecondsToTicks(ns int64) timeUnit { return timeUnit(ns) } -// with the wasm32-unknown-unknown target there is no way to determine any `precision` -const timePrecisionNanoseconds = 1000 - func sleepTicks(d timeUnit) { } diff --git a/src/runtime/runtime_windows.go b/src/runtime/runtime_windows.go index 88857fc3a..17ee2ce72 100644 --- a/src/runtime/runtime_windows.go +++ b/src/runtime/runtime_windows.go @@ -123,8 +123,6 @@ func preinit() { heapEnd = heapStart + heapSize } -type timeUnit int64 - var stackTop uintptr func ticksToNanoseconds(ticks timeUnit) int64 { diff --git a/src/runtime/time_nxpmk66f18.go b/src/runtime/time_nxpmk66f18.go index ca3120f84..d6114556a 100644 --- a/src/runtime/time_nxpmk66f18.go +++ b/src/runtime/time_nxpmk66f18.go @@ -39,8 +39,6 @@ import ( "runtime/volatile" ) -type timeUnit int64 - func ticksToNanoseconds(ticks timeUnit) int64 { return int64(ticks) * 1000 }