mirror of
https://github.com/garethgeorge/backrest.git
synced 2025-12-15 18:15:37 +00:00
44 lines
743 B
Go
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()
|
|
}
|