From 49dd7726a9e2e4371de984871082ea0e3791cbdd Mon Sep 17 00:00:00 2001 From: Mateusz Poliwczak Date: Sat, 28 Sep 2024 16:50:03 +0000 Subject: [PATCH] internal/testlog: use atomic.Pointer instead of atomic.Value 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 Reviewed-by: Michael Knyszek Auto-Submit: Ian Lance Taylor LUCI-TryBot-Result: Go LUCI --- src/internal/testlog/log.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/internal/testlog/log.go b/src/internal/testlog/log.go index 3c5f780ac4..d8b9dcfafe 100644 --- a/src/internal/testlog/log.go +++ b/src/internal/testlog/log.go @@ -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. -- 2.48.1