forked from rosedblabs/rosedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.go
49 lines (41 loc) · 1.04 KB
/
lock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package rosedb
import "sync"
// LockMgr is a lock manager that manages read and write operations of different data structures.
// It is also be used to manage transaction.
type LockMgr struct {
locks map[DataType]*sync.RWMutex
}
func newLockMgr(db *RoseDB) *LockMgr {
locks := make(map[DataType]*sync.RWMutex)
// store the lock of different data types.
locks[String] = db.strIndex.mu
locks[List] = db.listIndex.mu
locks[Hash] = db.hashIndex.mu
locks[Set] = db.setIndex.mu
locks[ZSet] = db.zsetIndex.mu
return &LockMgr{locks: locks}
}
// Lock locks the rw of dTypes for writing.
func (lm *LockMgr) Lock(dTypes ...DataType) func() {
for _, t := range dTypes {
lm.locks[t].Lock()
}
unLockFunc := func() {
for _, t := range dTypes {
lm.locks[t].Unlock()
}
}
return unLockFunc
}
// RLock locks the rw of dTypes for reading.
func (lm *LockMgr) RLock(dTypes ...DataType) func() {
for _, t := range dTypes {
lm.locks[t].RLock()
}
unLockFunc := func() {
for _, t := range dTypes {
lm.locks[t].RUnlock()
}
}
return unLockFunc
}