-
Notifications
You must be signed in to change notification settings - Fork 11
/
reentrant_lock.go
73 lines (67 loc) · 1.38 KB
/
reentrant_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package async
import "sync"
// ReentrantLock allows goroutines to enter the lock more than once.
type ReentrantLock struct {
g *sync.Mutex
l *sync.Mutex
goroutineID uint
counter uint
lockBalance int
}
// NewReentrantLock returns a new ReentrantLock.
func NewReentrantLock() *ReentrantLock {
return &ReentrantLock{
g: &sync.Mutex{},
l: &sync.Mutex{},
lockBalance: 1,
}
}
func (r *ReentrantLock) handleLock() {
if r.lockBalance > 0 {
r.lockBalance--
r.g.Lock()
}
}
// Lock locks the resource.
// Panics if the GoroutineID call returns an error.
func (r *ReentrantLock) Lock() {
curr, err := GoroutineID()
if err != nil {
panic("async: Error on GoroutineID call")
}
for {
r.l.Lock()
// first time lock
if r.goroutineID == 0 {
r.handleLock()
r.goroutineID = curr
r.counter++
break
} else if r.goroutineID == curr {
// reentrant lock request
r.counter++
break
} else {
// another goroutine lock request
r.lockBalance--
r.l.Unlock()
r.g.Lock()
}
}
r.l.Unlock()
}
// Unlock unlocks the resource.
// Panics on trying to unlock the unlocked lock.
func (r *ReentrantLock) Unlock() {
if r.counter == 0 && r.goroutineID == 0 {
panic("async: Unlock of unlocked ReentrantLock")
}
r.l.Lock()
r.counter--
if r.counter == 0 {
r.goroutineID = 0
r.lockBalance++
r.g.Unlock()
}
r.l.Unlock()
}