Skip to content

Commit

Permalink
internal/testlog: use atomic.Pointer instead of atomic.Value
Browse files Browse the repository at this point in the history
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: 0c053be
GitHub-Pull-Request: #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>
  • Loading branch information
mateusz834 authored and gopherbot committed Sep 30, 2024
1 parent 6536c20 commit 49dd772
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/internal/testlog/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down

0 comments on commit 49dd772

Please sign in to comment.