runtime: mark map bucket slots as empty during map clear

So iterators that are in progress can know entries have been deleted and
terminate the iterator properly.

Update #55002
Update #56351
Fixes #59411

Change-Id: I924f16a00fe4ed6564f730a677348a6011d3fb67
Reviewed-on: https://go-review.googlesource.com/c/go/+/481935
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Keith Randall <khr@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Cuong Manh Le 2023-04-04 12:49:39 +07:00 committed by Gopher Robot
parent 66cac9e1e4
commit 231f290e51
2 changed files with 93 additions and 0 deletions

View file

@ -1008,6 +1008,22 @@ func mapclear(t *maptype, h *hmap) {
h.flags ^= hashWriting
// Mark buckets empty, so existing iterators can be terminated, see issue #59411.
markBucketsEmpty := func(bucket unsafe.Pointer, mask uintptr) {
for i := uintptr(0); i <= mask; i++ {
b := (*bmap)(add(bucket, i*uintptr(t.bucketsize)))
for ; b != nil; b = b.overflow(t) {
for i := uintptr(0); i < bucketCnt; i++ {
b.tophash[i] = emptyRest
}
}
}
}
markBucketsEmpty(h.buckets, bucketMask(h.B))
if oldBuckets := h.oldbuckets; oldBuckets != nil {
markBucketsEmpty(oldBuckets, h.oldbucketmask())
}
h.flags &^= sameSizeGrow
h.oldbuckets = nil
h.nevacuate = 0