iter: fix race instrumentation in Pull2

Pull2 tests are failing with -race, giving false-positive race conditions due to
bad race instrumentation.

No tests for this as it should be caught by the race builders. The only reason
it was not caught is because it is behind GOEXPERIMENT=rangefunc.

Fixes [reserved]
This commit is contained in:
Mauri de Souza Meneguzzo 2023-12-11 14:16:02 -03:00
parent 46ea4ab5cb
commit ec972f2878
1 changed files with 15 additions and 6 deletions

View File

@ -122,18 +122,22 @@ func Pull[V any](seq Seq[V]) (next func() (V, bool), stop func()) {
// simultaneously.
func Pull2[K, V any](seq Seq2[K, V]) (next func() (K, V, bool), stop func()) {
var (
k K
v V
ok bool
done bool
k K
v V
ok bool
done bool
racer int
)
c := newcoro(func(c *coro) {
race.Acquire(unsafe.Pointer(&racer))
yield := func(k1 K, v1 V) bool {
if done {
return false
}
k, v, ok = k1, v1, true
race.Release(unsafe.Pointer(&racer))
coroswitch(c)
race.Acquire(unsafe.Pointer(&racer))
return !done
}
seq(yield)
@ -141,20 +145,25 @@ func Pull2[K, V any](seq Seq2[K, V]) (next func() (K, V, bool), stop func()) {
var v0 V
k, v, ok = k0, v0, false
done = true
race.Release(unsafe.Pointer(&racer))
})
next = func() (k1 K, v1 V, ok1 bool) {
race.Write(unsafe.Pointer(&c)) // detect races
race.Write(unsafe.Pointer(&racer)) // detect races
if done {
return
}
race.Release(unsafe.Pointer(&racer))
coroswitch(c)
race.Acquire(unsafe.Pointer(&racer))
return k, v, ok
}
stop = func() {
race.Write(unsafe.Pointer(&c)) // detect races
race.Write(unsafe.Pointer(&racer)) // detect races
if !done {
done = true
race.Release(unsafe.Pointer(&racer))
coroswitch(c)
race.Acquire(unsafe.Pointer(&racer))
}
}
return next, stop