From 743bf45e002fd596edc8af49299c9e4093e61211 Mon Sep 17 00:00:00 2001 From: deadprogram Date: Fri, 9 Jan 2026 21:31:08 +0100 Subject: [PATCH] runtime: make timeoffset atomic Make timeoffset atomic to be able to handle changing the system time. Otherwise the scheduler can gets rather confused if you call AdjustTimeOffset when there are multiple goroutines already running. Signed-off-by: deadprogram --- src/runtime/baremetal.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/runtime/baremetal.go b/src/runtime/baremetal.go index aecb18972..9915f191b 100644 --- a/src/runtime/baremetal.go +++ b/src/runtime/baremetal.go @@ -3,6 +3,7 @@ package runtime import ( + "sync/atomic" "unsafe" ) @@ -69,13 +70,14 @@ const baremetal = true // timeOffset is how long the monotonic clock started after the Unix epoch. It // should be a positive integer under normal operation or zero when it has not // been set. -var timeOffset int64 +var timeOffset atomic.Int64 //go:linkname now time.now func now() (sec int64, nsec int32, mono int64) { mono = nanotime() - sec = (mono + timeOffset) / (1000 * 1000 * 1000) - nsec = int32((mono + timeOffset) - sec*(1000*1000*1000)) + to := timeOffset.Load() + sec = (mono + to) / (1000 * 1000 * 1000) + nsec = int32((mono + to) - sec*(1000*1000*1000)) return } @@ -83,8 +85,7 @@ func now() (sec int64, nsec int32, mono int64) { // positive value adds to the time (skipping some time), a negative value moves // the clock into the past. func AdjustTimeOffset(offset int64) { - // TODO: do this atomically? - timeOffset += offset + timeOffset.Add(offset) } // Picolibc is not configured to define its own errno value, instead it calls