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 <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2026-01-09 21:31:08 +01:00
committed by Ron Evans
parent 9bc5d21185
commit 743bf45e00
+6 -5
View File
@@ -3,6 +3,7 @@
package runtime package runtime
import ( import (
"sync/atomic"
"unsafe" "unsafe"
) )
@@ -69,13 +70,14 @@ const baremetal = true
// timeOffset is how long the monotonic clock started after the Unix epoch. It // 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 // should be a positive integer under normal operation or zero when it has not
// been set. // been set.
var timeOffset int64 var timeOffset atomic.Int64
//go:linkname now time.now //go:linkname now time.now
func now() (sec int64, nsec int32, mono int64) { func now() (sec int64, nsec int32, mono int64) {
mono = nanotime() mono = nanotime()
sec = (mono + timeOffset) / (1000 * 1000 * 1000) to := timeOffset.Load()
nsec = int32((mono + timeOffset) - sec*(1000*1000*1000)) sec = (mono + to) / (1000 * 1000 * 1000)
nsec = int32((mono + to) - sec*(1000*1000*1000))
return 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 // positive value adds to the time (skipping some time), a negative value moves
// the clock into the past. // the clock into the past.
func AdjustTimeOffset(offset int64) { func AdjustTimeOffset(offset int64) {
// TODO: do this atomically? timeOffset.Add(offset)
timeOffset += offset
} }
// Picolibc is not configured to define its own errno value, instead it calls // Picolibc is not configured to define its own errno value, instead it calls