]> Cypherpunks repositories - gostls13.git/commitdiff
runtime/internal/atomic: add generic implementation for And/Or
authorMauri de Souza Meneguzzo <mauri870@gmail.com>
Sat, 18 Nov 2023 17:52:22 +0000 (17:52 +0000)
committerCherry Mui <cherryyz@google.com>
Mon, 20 Nov 2023 02:02:59 +0000 (02:02 +0000)
Without having all the architectures implementing the And/Or operators
merged I can't proceed with the public sync/atomic apis. This CL adds a
generic implementation that should work for all the remaining arches,
while waiting for the native assembly implementations in CL 531835,
CL 531678, CL 531895.

I regret the oversight of not pushing this earlier.

For #61395

Change-Id: Ib2d67f359fe324b4743eb79e9c8e52e8f6f5476c
GitHub-Last-Rev: d350927ba1c51d1f708be2f2904f826fdb79b8cd
GitHub-Pull-Request: golang/go#64214
Reviewed-on: https://go-review.googlesource.com/c/go/+/543175
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>

src/runtime/internal/atomic/atomic_andor_generic.go [new file with mode: 0644]
src/runtime/internal/atomic/atomic_andor_test.go

diff --git a/src/runtime/internal/atomic/atomic_andor_generic.go b/src/runtime/internal/atomic/atomic_andor_generic.go
new file mode 100644 (file)
index 0000000..c790e06
--- /dev/null
@@ -0,0 +1,67 @@
+// Copyright 2023 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.
+
+//go:build s390x || loong64 || mips || mipsle || mips64 || mips64le
+
+package atomic
+
+//go:nosplit
+func And32(ptr *uint32, val uint32) uint32 {
+       for {
+               old := *ptr
+               if Cas(ptr, old, old&val) {
+                       return old
+               }
+       }
+}
+
+//go:nosplit
+func Or32(ptr *uint32, val uint32) uint32 {
+       for {
+               old := *ptr
+               if Cas(ptr, old, old|val) {
+                       return old
+               }
+       }
+}
+
+//go:nosplit
+func And64(ptr *uint64, val uint64) uint64 {
+       for {
+               old := *ptr
+               if Cas64(ptr, old, old&val) {
+                       return old
+               }
+       }
+}
+
+//go:nosplit
+func Or64(ptr *uint64, val uint64) uint64 {
+       for {
+               old := *ptr
+               if Cas64(ptr, old, old|val) {
+                       return old
+               }
+       }
+}
+
+//go:nosplit
+func Anduintptr(ptr *uintptr, val uintptr) uintptr {
+       for {
+               old := *ptr
+               if Casuintptr(ptr, old, old&val) {
+                       return old
+               }
+       }
+}
+
+//go:nosplit
+func Oruintptr(ptr *uintptr, val uintptr) uintptr {
+       for {
+               old := *ptr
+               if Casuintptr(ptr, old, old|val) {
+                       return old
+               }
+       }
+}
index 9dd8b60ae435ba808e59b87bb052c70decff48e1..a2f3b6f3a90b64813b6f3aa3f9eb3d0896765e13 100644 (file)
@@ -1,6 +1,3 @@
-//go:build 386 || amd64 || arm || arm64 || ppc64 || ppc64le || riscv64 || wasm
-
-//
 // Copyright 2023 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.