Skip to content

Commit 57af736

Browse files
author
Bryan C. Mills
committed
syncmap: release m.mu during (*RWMutexMap).Range callbacks
The mainline syncmap.Map has allowed mutations within Range callbacks since https://golang.org/cl/37342. The reference implementations used in map_bench_test need to do the same. Change-Id: Id73d254fa01cc64a1f00eb1903488796e1282423 Reviewed-on: https://go-review.googlesource.com/42956 Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 305c7b3 commit 57af736

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

syncmap/map_bench_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,10 @@ func BenchmarkAdversarialDelete(b *testing.B) {
204204
m.Load(i)
205205

206206
if i%mapSize == 0 {
207-
var key int
208207
m.Range(func(k, _ interface{}) bool {
209-
key = k.(int)
208+
m.Delete(k)
210209
return false
211210
})
212-
m.Delete(key)
213211
m.Store(i, i)
214212
}
215213
}

syncmap/map_reference_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,17 @@ func (m *RWMutexMap) Delete(key interface{}) {
6464

6565
func (m *RWMutexMap) Range(f func(key, value interface{}) (shouldContinue bool)) {
6666
m.mu.RLock()
67-
defer m.mu.RUnlock()
68-
for k, v := range m.dirty {
67+
keys := make([]interface{}, 0, len(m.dirty))
68+
for k := range m.dirty {
69+
keys = append(keys, k)
70+
}
71+
m.mu.RUnlock()
72+
73+
for _, k := range keys {
74+
v, ok := m.Load(k)
75+
if !ok {
76+
continue
77+
}
6978
if !f(k, v) {
7079
break
7180
}

0 commit comments

Comments
 (0)