]> Cypherpunks repositories - gostls13.git/commitdiff
internal/testlog: use atomic.Pointer instead of atomic.Value
authorMateusz Poliwczak <mpoliwczak34@gmail.com>
Sat, 28 Sep 2024 16:50:03 +0000 (16:50 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 30 Sep 2024 12:40:01 +0000 (12:40 +0000)
We know the type (*Interface), so we can use the generic atomic.Pointer.
This change also makes sure that concurrent use of SetLogger also
causes a panic, currently it races (Load, then Store).

Change-Id: I6fae5ce0587b37eede2060342c3fcd0cde4386ff
GitHub-Last-Rev: 0c053be03e22d4afcee235a247a377d7bd4d5aea
GitHub-Pull-Request: golang/go#69701
Reviewed-on: https://go-review.googlesource.com/c/go/+/616516
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>

src/internal/testlog/log.go

index 3c5f780ac4d62b28b790e0c0d1a28e64deb5c681..d8b9dcfafe3d88d8e3b1416d96ab42af6e0dae12 100644 (file)
@@ -21,20 +21,19 @@ type Interface interface {
 }
 
 // logger is the current logger Interface.
-// We use an atomic.Value in case test startup
+// We use an atomic.Pointer in case test startup
 // is racing with goroutines started during init.
 // That must not cause a race detector failure,
 // although it will still result in limited visibility
 // into exactly what those goroutines do.
-var logger atomic.Value
+var logger atomic.Pointer[Interface]
 
 // SetLogger sets the test logger implementation for the current process.
 // It must be called only once, at process startup.
 func SetLogger(impl Interface) {
-       if logger.Load() != nil {
+       if !logger.CompareAndSwap(nil, &impl) {
                panic("testlog: SetLogger must be called only once")
        }
-       logger.Store(&impl)
 }
 
 // Logger returns the current test logger implementation.
@@ -44,7 +43,7 @@ func Logger() Interface {
        if impl == nil {
                return nil
        }
-       return *impl.(*Interface)
+       return *impl
 }
 
 // Getenv calls Logger().Getenv, if a logger has been set.