open-vault/helper/locksutil/locks.go

33 lines
473 B
Go
Raw Normal View History

2016-07-20 01:37:28 +00:00
package locksutil
import (
"crypto/md5"
2016-07-20 01:37:28 +00:00
"sync"
)
const (
LockCount = 256
)
type LockEntry struct {
sync.RWMutex
}
2016-08-30 20:36:58 +00:00
func CreateLocks() []*LockEntry {
ret := make([]*LockEntry, LockCount)
for i := range ret {
ret[i] = new(LockEntry)
2016-07-20 01:37:28 +00:00
}
return ret
}
func LockIndexForKey(key string) uint8 {
hf := md5.New()
hf.Write([]byte(key))
return uint8(hf.Sum(nil)[0])
}
func LockForKey(locks []*LockEntry, key string) *LockEntry {
return locks[LockIndexForKey(key)]
2016-07-20 01:37:28 +00:00
}