Skip to content

Commit

Permalink
sync: release m.mu during (*RWMutexMap).Range callbacks in sync_test
Browse files Browse the repository at this point in the history
The mainline sync.Map has allowed mutations within Range callbacks
since https://golang.org/cl/37342. The reference implementations need
to do the same.

This change integrates https://go-review.googlesource.com/c/42956/
from x/sync.

Change-Id: I6b58cf874bb31cd4f6fdb8bfa8278888ed617a5a
Reviewed-on: https://go-review.googlesource.com/42957
Run-TryBot: Bryan Mills <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
  • Loading branch information
Bryan C. Mills committed Jul 20, 2017
1 parent f5eb871 commit 9311af7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 1 addition & 3 deletions src/sync/map_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,10 @@ func BenchmarkAdversarialDelete(b *testing.B) {
m.Load(i)

if i%mapSize == 0 {
var key int
m.Range(func(k, _ interface{}) bool {
key = k.(int)
m.Delete(k)
return false
})
m.Delete(key)
m.Store(i, i)
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/sync/map_reference_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,17 @@ func (m *RWMutexMap) Delete(key interface{}) {

func (m *RWMutexMap) Range(f func(key, value interface{}) (shouldContinue bool)) {
m.mu.RLock()
defer m.mu.RUnlock()
for k, v := range m.dirty {
keys := make([]interface{}, 0, len(m.dirty))
for k := range m.dirty {
keys = append(keys, k)
}
m.mu.RUnlock()

for _, k := range keys {
v, ok := m.Load(k)
if !ok {
continue
}
if !f(k, v) {
break
}
Expand Down

0 comments on commit 9311af7

Please sign in to comment.