-
Notifications
You must be signed in to change notification settings - Fork 11
/
reentrant_lock.go
61 lines (52 loc) · 1.12 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
package async
import (
"sync"
)
// ReentrantLock allows goroutines to enter the lock more than once.
// Implements the sync.Locker interface.
//
// A ReentrantLock must not be copied after first use.
type ReentrantLock struct {
outer sync.Mutex
inner sync.Mutex
goroutine uint64
depth int32
}
var _ sync.Locker = (*ReentrantLock)(nil)
// Lock locks the resource.
// Panics if the GoroutineID call returns an error.
func (r *ReentrantLock) Lock() {
r.inner.Lock()
current, err := GoroutineID()
if err != nil {
panic("async: Error on GoroutineID call")
}
switch r.goroutine {
case current:
// reentrant lock request
r.depth++
r.inner.Unlock()
default:
// initial or another goroutine lock request
r.init(current)
}
}
func (r *ReentrantLock) init(goroutine uint64) {
r.inner.Unlock()
r.outer.Lock()
r.inner.Lock()
r.goroutine = goroutine
r.depth = 1
r.inner.Unlock()
}
// Unlock unlocks the resource.
// Panics on trying to unlock the unlocked lock.
func (r *ReentrantLock) Unlock() {
r.inner.Lock()
defer r.inner.Unlock()
r.depth--
if r.depth == 0 {
r.goroutine = 0
r.outer.Unlock()
}
}