all: clean up code with Go 1.24+ in mind (#5489)

* all: clean up code with Go 1.24+ in mind

* all: more old go cleanup

* all: even remove rand_fastrand64

* runtime: revert rand changes

* runtime: re-drop rand_fastrand64
This commit is contained in:
Jake Bailey
2026-07-03 10:10:55 -07:00
committed by GitHub
parent bf80e9b446
commit e79edb58b2
19 changed files with 78 additions and 271 deletions
-8
View File
@@ -7,14 +7,6 @@ import (
"unsafe"
)
// This function is needed by math/rand since Go 1.20.
// See: https://github.com/golang/go/issues/54880
//
//go:linkname rand_fastrand64 math/rand.fastrand64
func rand_fastrand64() uint64 {
return fastrand64()
}
// This function is used by hash/maphash.
// This function isn't required anymore since Go 1.22, so should be removed once
// that becomes the minimum requirement.
-14
View File
@@ -37,20 +37,6 @@ func syscallClearenv(env map[string]int) {
}
}
// Compatibility with Go 1.19 and below.
//
//go:linkname syscall_setenv_c syscall.setenv_c
func syscall_setenv_c(key string, val string) {
syscallSetenv(key, val)
}
// Compatibility with Go 1.19 and below.
//
//go:linkname syscall_unsetenv_c syscall.unsetenv_c
func syscall_unsetenv_c(key string) {
syscallUnsetenv(key)
}
// cstring converts a Go string to a C string.
// borrowed from syscall
func cstring(s string) []byte {
+1 -6
View File
@@ -160,12 +160,7 @@ func write(fd uintptr, p unsafe.Pointer, n int32) int32 {
if fd == 2 { // stderr
// Convert to a string, because we know that p won't change during the
// call to printstring.
// TODO: use unsafe.String instead once we require Go 1.20.
s := _string{
ptr: (*byte)(p),
length: uintptr(n),
}
str := *(*string)(unsafe.Pointer(&s))
str := unsafe.String((*byte)(p), int(n))
printstring(str)
return n
}
+2 -26
View File
@@ -438,19 +438,7 @@ func tinygo_signal_disable(s uint32)
//
//export tinygo_signal_handler
func tinygo_signal_handler(s int32) {
// The following loop is equivalent to the following:
//
// receivedSignals.Or(uint32(1) << uint32(s))
//
// TODO: use this instead of a loop once we drop support for Go 1.22.
for {
mask := uint32(1) << uint32(s)
val := receivedSignals.Load()
swapped := receivedSignals.CompareAndSwap(val, val|mask)
if swapped {
break
}
}
receivedSignals.Or(uint32(1) << uint32(s))
// Notify the main thread that there was a signal.
// This will exit the call to Wait or WaitUntil early.
@@ -485,19 +473,7 @@ func signal_recv() uint32 {
num := uint32(bits.TrailingZeros32(val))
// Atomically clear the signal number from receivedSignals.
// TODO: use atomic.Uint32.And once we drop support for Go 1.22 instead
// of this loop, like so:
//
// receivedSignals.And(^(uint32(1) << num))
//
for {
newVal := val &^ (1 << num)
swapped := receivedSignals.CompareAndSwap(val, newVal)
if swapped {
break
}
val = receivedSignals.Load()
}
receivedSignals.And(^(uint32(1) << num))
return num
}
+66
View File
@@ -1,5 +1,71 @@
package runtime
import "unsafe"
// This is the timer that's used internally inside the runtime.
type timer struct {
// When to call the timer, and the interval for the ticker.
when int64
period int64
// Callback from the time package.
f func(arg any, seq uintptr, delta int64)
arg any
}
func (tim *timer) callCallback(delta int64) {
tim.f(tim.arg, 0, delta)
}
// This is the struct used internally in the runtime. The first two fields are
// the same as time.Timer and time.Ticker so it can be used as-is in the time
// package.
type timeTimer struct {
c unsafe.Pointer // <-chan time.Time
init bool
timer
}
//go:linkname newTimer time.newTimer
func newTimer(when, period int64, f func(arg any, seq uintptr, delta int64), arg any, c unsafe.Pointer) *timeTimer {
tim := &timeTimer{
c: c,
init: true,
timer: timer{
when: when,
period: period,
f: f,
arg: arg,
},
}
scheduleLog("new timer")
addTimer(&timerNode{
timer: &tim.timer,
callback: timerCallback,
})
return tim
}
//go:linkname stopTimer time.stopTimer
func stopTimer(tim *timeTimer) bool {
return removeTimer(&tim.timer) != nil
}
//go:linkname resetTimer time.resetTimer
func resetTimer(t *timeTimer, when, period int64) bool {
t.timer.when = when
t.timer.period = period
n := removeTimer(&t.timer)
removed := n != nil
if n == nil {
n = new(timerNode)
}
n.timer = &t.timer
n.callback = timerCallback
addTimer(n)
return removed
}
//go:linkname time_runtimeNano time.runtimeNano
func time_runtimeNano() int64 {
// Note: we're ignoring sync groups here (package testing/synctest).
-65
View File
@@ -1,65 +0,0 @@
//go:build !go1.23
// Portions copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package runtime
// Time functions for Go 1.22 and below.
type puintptr uintptr
// Package time knows the layout of this structure.
// If this struct changes, adjust ../time/sleep.go:/runtimeTimer.
type timer struct {
// If this timer is on a heap, which P's heap it is on.
// puintptr rather than *p to match uintptr in the versions
// of this struct defined in other packages.
pp puintptr
// Timer wakes up at when, and then at when+period, ... (period > 0 only)
// each time calling f(arg, now) in the timer goroutine, so f must be
// a well-behaved function and not block.
//
// when must be positive on an active timer.
when int64
period int64
f func(any, uintptr)
arg any
seq uintptr
// What to set the when field to in timerModifiedXX status.
nextwhen int64
// The status field holds one of the values below.
status uint32
}
func (tim *timer) callCallback(delta int64) {
tim.f(tim.arg, 0)
}
// Defined in the time package, implemented here in the runtime.
//
//go:linkname startTimer time.startTimer
func startTimer(tim *timer) {
addTimer(&timerNode{
timer: tim,
callback: timerCallback,
})
scheduleLog("adding timer")
}
//go:linkname stopTimer time.stopTimer
func stopTimer(tim *timer) bool {
return removeTimer(tim) != nil
}
//go:linkname resetTimer time.resetTimer
func resetTimer(tim *timer, when int64) bool {
tim.when = when
n := removeTimer(tim)
startTimer(tim)
return n != nil
}
-71
View File
@@ -1,71 +0,0 @@
//go:build go1.23
package runtime
import "unsafe"
// Time functions for Go 1.23 and above.
// This is the timer that's used internally inside the runtime.
type timer struct {
// When to call the timer, and the interval for the ticker.
when int64
period int64
// Callback from the time package.
f func(arg any, seq uintptr, delta int64)
arg any
}
func (tim *timer) callCallback(delta int64) {
tim.f(tim.arg, 0, delta)
}
// This is the struct used internally in the runtime. The first two fields are
// the same as time.Timer and time.Ticker so it can be used as-is in the time
// package.
type timeTimer struct {
c unsafe.Pointer // <-chan time.Time
init bool
timer
}
//go:linkname newTimer time.newTimer
func newTimer(when, period int64, f func(arg any, seq uintptr, delta int64), arg any, c unsafe.Pointer) *timeTimer {
tim := &timeTimer{
c: c,
init: true,
timer: timer{
when: when,
period: period,
f: f,
arg: arg,
},
}
scheduleLog("new timer")
addTimer(&timerNode{
timer: &tim.timer,
callback: timerCallback,
})
return tim
}
//go:linkname stopTimer time.stopTimer
func stopTimer(tim *timeTimer) bool {
return removeTimer(&tim.timer) != nil
}
//go:linkname resetTimer time.resetTimer
func resetTimer(t *timeTimer, when, period int64) bool {
t.timer.when = when
t.timer.period = period
n := removeTimer(&t.timer)
removed := n != nil
if n == nil {
n = new(timerNode)
}
n.timer = &t.timer
n.callback = timerCallback
addTimer(n)
return removed
}