From 6ecd5f750454665f789e3d557548bb5a65ad5c3a Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Sat, 9 Sep 2023 13:41:35 +0000 Subject: [PATCH] runtime/internal/atomic: add wasm And/Or operators In the WebAssembly version of these operators we avoid using a CAS loop since the Go wasm implementation is single-threaded. A new test file has been added that has build tags in order to only test this feature on implemented architectures. This is part of a series of CLs aimed to add the primitives for And/Or atomic operations that will be used by the public sync/atomic apis. For #61395 Change-Id: Ic67ffefc9cfb626915ea86b6b21b500117710327 GitHub-Last-Rev: bbec3a5f356c55185af0357b929e76a9dfac230e GitHub-Pull-Request: golang/go#62517 Reviewed-on: https://go-review.googlesource.com/c/go/+/526656 Run-TryBot: Mauri de Souza Meneguzzo Reviewed-by: Keith Randall Reviewed-by: Keith Randall TryBot-Result: Gopher Robot Reviewed-by: Heschi Kreinick Auto-Submit: Keith Randall --- .../internal/atomic/atomic_andor_test.go | 169 ++++++++++++++++++ src/runtime/internal/atomic/atomic_wasm.go | 48 +++++ 2 files changed, 217 insertions(+) create mode 100644 src/runtime/internal/atomic/atomic_andor_test.go diff --git a/src/runtime/internal/atomic/atomic_andor_test.go b/src/runtime/internal/atomic/atomic_andor_test.go new file mode 100644 index 0000000000..0298d75c9b --- /dev/null +++ b/src/runtime/internal/atomic/atomic_andor_test.go @@ -0,0 +1,169 @@ +// +build 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. + +// TODO(61395): move these tests to atomic_test.go once And/Or have +// implementations for all architectures. +package atomic_test + +import ( + "testing" + "runtime/internal/atomic" +) + +func TestAnd32(t *testing.T) { + // Basic sanity check. + x := uint32(0xffffffff) + for i := uint32(0); i < 32; i++ { + old := x + v := atomic.And32(&x, ^(1 << i)) + if r := uint32(0xffffffff) << (i + 1); x != r || v != old { + t.Fatalf("clearing bit %#x: want %#x, got new %#x and old %#v", uint32(1<