-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.go
98 lines (76 loc) · 1.39 KB
/
interface.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package multilock
import (
"fmt"
"sync/atomic"
)
// public
type MultiLocker[K comparable] interface {
TryLockByKey(K) bool
LockByKey(K)
UnlockByKey(K)
TryLock(K) (got bool, unlock func())
Lock(K) (unlock func())
}
type MultiRWLocker[K comparable] interface {
TryLockByKey(K) bool
LockByKey(K)
UnlockByKey(K)
TryLock(K) (got bool, unlock func())
Lock(K) (unlock func())
TryRLockByKey(K) bool
RLockByKey(K)
RUnlockByKey(K)
TryRLock(K) (got bool, unlock func())
RLock(K) (unlock func())
}
var ErrLockKeyNotFound = fmt.Errorf("locker key not found")
// private
var noop = func() {}
type refTryLocker interface {
tryLocker
refCountable
}
type refTryRWLocker interface {
tryRWLocker
refCountable
}
type tryLocker interface {
Lock()
TryLock() bool
Unlock()
}
type tryRLocker interface {
RLock()
TryRLock() bool
RUnlock()
}
type tryRWLocker interface {
tryRLocker
tryLocker
}
type lockerFactory interface {
Get() refTryLocker
Put(refTryLocker)
}
type rwLockerFactory interface {
Get() refTryRWLocker
Put(refTryRWLocker)
}
type refCountable interface {
GetRefCount() int64
IncRefCount() int64
DecRefCount() int64
}
// 默认实现
type refCounter struct {
atomic.Int64
}
func (c *refCounter) GetRefCount() int64 {
return c.Int64.Load()
}
func (c *refCounter) IncRefCount() int64 {
return c.Int64.Add(1)
}
func (c *refCounter) DecRefCount() int64 {
return c.Int64.Add(-1)
}