diff --git a/pkg/util/fastrand/BUILD.bazel b/pkg/util/fastrand/BUILD.bazel index 84df5c88e8..ba17841cba 100644 --- a/pkg/util/fastrand/BUILD.bazel +++ b/pkg/util/fastrand/BUILD.bazel @@ -2,7 +2,11 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "fastrand", - srcs = ["random.go"], + srcs = [ + "random.go", + "runtime.go", + "runtime_1.22.go", + ], importpath = "github.com/pingcap/tidb/pkg/util/fastrand", visibility = ["//visibility:public"], ) diff --git a/pkg/util/fastrand/random.go b/pkg/util/fastrand/random.go index 33458a3d87..d823876ad6 100644 --- a/pkg/util/fastrand/random.go +++ b/pkg/util/fastrand/random.go @@ -16,7 +16,6 @@ package fastrand import ( "math/bits" - _ "unsafe" // required by go:linkname ) // wyrand is a fast PRNG. See https://github.com/wangyi-fudan/wyhash @@ -48,11 +47,6 @@ func Buf(size int) []byte { return buf } -// Uint32 returns a lock free uint32 value. -// -//go:linkname Uint32 runtime.fastrand -func Uint32() uint32 - // Uint32N returns, as an uint32, a pseudo-random number in [0,n). func Uint32N(n uint32) uint32 { // This is similar to Uint32() % n, but faster. diff --git a/pkg/util/fastrand/runtime.go b/pkg/util/fastrand/runtime.go new file mode 100644 index 0000000000..dda6549090 --- /dev/null +++ b/pkg/util/fastrand/runtime.go @@ -0,0 +1,26 @@ +// Copyright 2024 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build !go1.22 + +package fastrand + +import ( + _ "unsafe" // required by go:linkname +) + +// Uint32 returns a lock free uint32 value. +// +//go:linkname Uint32 runtime.fastrand +func Uint32() uint32 diff --git a/pkg/util/fastrand/runtime_1.22.go b/pkg/util/fastrand/runtime_1.22.go new file mode 100644 index 0000000000..0e8cf2a87a --- /dev/null +++ b/pkg/util/fastrand/runtime_1.22.go @@ -0,0 +1,26 @@ +// Copyright 2024 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//go:build go1.22 + +package fastrand + +import ( + _ "unsafe" // required by go:linkname +) + +// Uint32 returns a lock free uint32 value. +// +//go:linkname Uint32 runtime.cheaprand +func Uint32() uint32