Files
backrest/internal/logstore/shardedmutex.go
2024-10-12 11:26:22 -07:00

44 lines
743 B
Go

package logstore
import (
"hash/fnv"
"sync"
)
type shardedRWMutex struct {
mu []sync.RWMutex
}
func newShardedRWMutex(n int) shardedRWMutex {
mu := make([]sync.RWMutex, n)
return shardedRWMutex{
mu: mu,
}
}
func (sm *shardedRWMutex) Lock(key string) {
idx := hash(key) % uint32(len(sm.mu))
sm.mu[idx].Lock()
}
func (sm *shardedRWMutex) Unlock(key string) {
idx := hash(key) % uint32(len(sm.mu))
sm.mu[idx].Unlock()
}
func (sm *shardedRWMutex) RLock(key string) {
idx := hash(key) % uint32(len(sm.mu))
sm.mu[idx].RLock()
}
func (sm *shardedRWMutex) RUnlock(key string) {
idx := hash(key) % uint32(len(sm.mu))
sm.mu[idx].RUnlock()
}
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}