optimize locks for sinterstore/sunionstore/sdiffstore

This commit is contained in:
hdt3213
2021-05-27 22:06:57 +08:00
parent f1455534d4
commit 6ee2789d6f
3 changed files with 54 additions and 15 deletions

View File

@@ -126,3 +126,41 @@ func (locks *Locks) RUnLocks(keys ...string) {
mu.RUnlock()
}
}
func (locks *Locks) RWLocks(writeKeys []string, readKeys []string) {
keys := append(writeKeys, readKeys...)
indices := locks.toLockIndices(keys, false)
writeIndices := locks.toLockIndices(writeKeys, false)
writeIndexSet := make(map[uint32]struct{})
for _, idx := range writeIndices {
writeIndexSet[idx] = struct{}{}
}
for _, index := range indices {
_, w := writeIndexSet[index]
mu := locks.table[index]
if w {
mu.Lock()
} else {
mu.RLock()
}
}
}
func (locks *Locks) RWUnLocks(writeKeys []string, readKeys []string) {
keys := append(writeKeys, readKeys...)
indices := locks.toLockIndices(keys, true)
writeIndices := locks.toLockIndices(writeKeys, true)
writeIndexSet := make(map[uint32]struct{})
for _, idx := range writeIndices {
writeIndexSet[idx] = struct{}{}
}
for _, index := range indices {
_, w := writeIndexSet[index]
mu := locks.table[index]
if w {
mu.Unlock()
} else {
mu.RUnlock()
}
}
}