aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMateusz Poliwczak <mpoliwczak34@gmail.com>2024-09-28 16:50:03 +0000
committerGopher Robot <gobot@golang.org>2024-09-30 12:40:01 +0000
commit49dd7726a9e2e4371de984871082ea0e3791cbdd (patch)
tree4f72573b6c061aab04246a17aae05e253b3f2d71 /src
parent6536c207c2309da7c1c21e3669f8ddf491e31f5b (diff)
downloadgo-49dd7726a9e2e4371de984871082ea0e3791cbdd.tar.xz
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 <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>
Diffstat (limited to 'src')
-rw-r--r--src/internal/testlog/log.go9
1 files 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.