mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
runtime/coverage: use atomic access for counter reads
Read counters using atomic ops so as to avoid problems with the race detector if a goroutine happens to still be executing at the end of a test run when we're writing out counter data. In theory we could guard the atomic use on the counter mode, but it's better just to do it in all cases, leaves us with a simpler implementation. Fixes #56006. Change-Id: I81c2234b5a1c3b00cff6c77daf2c2315451b7f6c Reviewed-on: https://go-review.googlesource.com/c/go/+/438256 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Bryan Mills <bcmills@google.com> Reviewed-by: David Chase <drchase@google.com> Run-TryBot: Than McIntosh <thanm@google.com>
This commit is contained in:
parent
8bd803fd4e
commit
cddf792428
5 changed files with 95 additions and 15 deletions
|
|
@ -9,6 +9,7 @@ import (
|
|||
"internal/coverage"
|
||||
"io"
|
||||
"reflect"
|
||||
"sync/atomic"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
|
|
@ -151,7 +152,7 @@ func ClearCoverageCounters() error {
|
|||
// inconsistency when reading the counter array from the thread
|
||||
// running ClearCoverageCounters.
|
||||
|
||||
var sd []uint32
|
||||
var sd []atomic.Uint32
|
||||
|
||||
bufHdr := (*reflect.SliceHeader)(unsafe.Pointer(&sd))
|
||||
for _, c := range cl {
|
||||
|
|
@ -160,13 +161,14 @@ func ClearCoverageCounters() error {
|
|||
bufHdr.Cap = int(c.Len)
|
||||
for i := 0; i < len(sd); i++ {
|
||||
// Skip ahead until the next non-zero value.
|
||||
if sd[i] == 0 {
|
||||
sdi := sd[i].Load()
|
||||
if sdi == 0 {
|
||||
continue
|
||||
}
|
||||
// We found a function that was executed; clear its counters.
|
||||
nCtrs := sd[i]
|
||||
nCtrs := sdi
|
||||
for j := 0; j < int(nCtrs); j++ {
|
||||
sd[i+coverage.FirstCtrOffset+j] = 0
|
||||
sd[i+coverage.FirstCtrOffset+j].Store(0)
|
||||
}
|
||||
// Move to next function.
|
||||
i += coverage.FirstCtrOffset + int(nCtrs) - 1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue