Skip to content

Commit f2d243d

Browse files
mauri870mknyszek
authored andcommitted
iter: fix race instrumentation for 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 #64651 Change-Id: I20554da930b0e19594e0e267f01a1e7a9cbc577a GitHub-Last-Rev: 7c1f192 GitHub-Pull-Request: #64653 Reviewed-on: https://go-review.googlesource.com/c/go/+/548895 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Michael Knyszek <[email protected]>
1 parent 58c28ba commit f2d243d

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/iter/iter.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ package iter
1414
import (
1515
"internal/race"
1616
"unsafe"
17-
_ "unsafe"
18-
) // for linkname
17+
)
1918

2019
// Seq is an iterator over sequences of individual values.
2120
// When called as seq(yield), seq calls yield(v) for each value v in the sequence,
@@ -122,39 +121,48 @@ func Pull[V any](seq Seq[V]) (next func() (V, bool), stop func()) {
122121
// simultaneously.
123122
func Pull2[K, V any](seq Seq2[K, V]) (next func() (K, V, bool), stop func()) {
124123
var (
125-
k K
126-
v V
127-
ok bool
128-
done bool
124+
k K
125+
v V
126+
ok bool
127+
done bool
128+
racer int
129129
)
130130
c := newcoro(func(c *coro) {
131+
race.Acquire(unsafe.Pointer(&racer))
131132
yield := func(k1 K, v1 V) bool {
132133
if done {
133134
return false
134135
}
135136
k, v, ok = k1, v1, true
137+
race.Release(unsafe.Pointer(&racer))
136138
coroswitch(c)
139+
race.Acquire(unsafe.Pointer(&racer))
137140
return !done
138141
}
139142
seq(yield)
140143
var k0 K
141144
var v0 V
142145
k, v, ok = k0, v0, false
143146
done = true
147+
race.Release(unsafe.Pointer(&racer))
144148
})
145149
next = func() (k1 K, v1 V, ok1 bool) {
146-
race.Write(unsafe.Pointer(&c)) // detect races
150+
race.Write(unsafe.Pointer(&racer)) // detect races
147151
if done {
148152
return
149153
}
154+
race.Release(unsafe.Pointer(&racer))
150155
coroswitch(c)
156+
race.Acquire(unsafe.Pointer(&racer))
151157
return k, v, ok
152158
}
153159
stop = func() {
154-
race.Write(unsafe.Pointer(&c)) // detect races
160+
race.Write(unsafe.Pointer(&racer)) // detect races
155161
if !done {
156162
done = true
163+
race.Release(unsafe.Pointer(&racer))
157164
coroswitch(c)
165+
race.Acquire(unsafe.Pointer(&racer))
158166
}
159167
}
160168
return next, stop

0 commit comments

Comments
 (0)