-
Notifications
You must be signed in to change notification settings - Fork 1
/
RWMutex.go
137 lines (121 loc) · 2.83 KB
/
RWMutex.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package part
import (
"errors"
"fmt"
"runtime"
"strings"
"sync"
"time"
)
// const (
// lock int32 = -1
// ulock int32 = 0
// rlock int32 = 1
// )
var (
ErrTimeoutToLock = errors.New("ErrTimeoutToLock")
ErrTimeoutToULock = errors.New("ErrTimeoutToULock")
)
type RWMutex struct {
rlc sync.RWMutex
PanicFunc func(any)
}
// func parse(i int32) string {
// switch i {
// case -2:
// return "lock"
// case -1:
// return "ulock"
// }
// return "rlock"
// }
// // i == oldt -> i = t -> pass
// //
// // otherwish block until i == oldt
// func cas(i *atomic.Int32, oldt, t int32, to ...time.Duration) error {
// c := time.Now()
// for !i.CompareAndSwap(oldt, t) {
// if len(to) != 0 && time.Since(c) > to[0] {
// return fmt.Errorf("timeout to set %s => %s while is %s", parse(oldt), parse(t), parse(i.Load()))
// }
// runtime.Gosched()
// }
// return nil
// }
// // i == t -> pass
// //
// // i == oldt -> i = t -> pass
// //
// // otherwish block until i == oldt
// func lcas(i *atomic.Int32, oldt, t int32, to ...time.Duration) error {
// c := time.Now()
// for i.Load() != t && !i.CompareAndSwap(oldt, t) {
// if len(to) != 0 && time.Since(c) > to[0] {
// return fmt.Errorf("timeout to set %s => %s while is %s", parse(oldt), parse(t), parse(i.Load()))
// }
// runtime.Gosched()
// }
// return nil
// }
func (m *RWMutex) panicFunc(s any) {
if m.PanicFunc != nil {
m.PanicFunc(s)
} else {
panic(s)
}
}
// call inTimeCall() in time or panic(callTree)
func (m *RWMutex) tof(to time.Duration, e error) (inTimeCall func() (called bool)) {
callTree := getCall(2)
return time.AfterFunc(to, func() {
m.panicFunc(errors.Join(e, errors.New(callTree)))
}).Stop
}
// to[0]: wait lock timeout
//
// to[1]: wait ulock timeout
//
// 不要在Rlock内设置变量,有DATA RACE风险
func (m *RWMutex) RLock(to ...time.Duration) (unlockf func(beforeUlock ...func())) {
if len(to) > 0 {
defer m.tof(to[0], ErrTimeoutToLock)()
}
m.rlc.RLock()
return func(beforeUlock ...func()) {
if len(to) > 1 {
defer m.tof(to[1], ErrTimeoutToULock)()
}
for i := 0; i < len(beforeUlock); i++ {
beforeUlock[i]()
}
m.rlc.RUnlock()
}
}
// to[0]: wait lock timeout
//
// to[1]: wait ulock timeout
func (m *RWMutex) Lock(to ...time.Duration) (unlockf func(beforeUlock ...func())) {
if len(to) > 0 {
defer m.tof(to[0], ErrTimeoutToLock)()
}
m.rlc.Lock()
return func(beforeUlock ...func()) {
if len(to) > 1 {
defer m.tof(to[1], ErrTimeoutToULock)()
}
for i := 0; i < len(beforeUlock); i++ {
beforeUlock[i]()
}
m.rlc.Unlock()
}
}
func getCall(i int) (calls string) {
for i += 1; true; i++ {
if pc, file, line, ok := runtime.Caller(i); !ok || strings.HasPrefix(file, runtime.GOROOT()) {
break
} else {
calls += fmt.Sprintf("\ncall by %s\n\t%s:%d", runtime.FuncForPC(pc).Name(), file, line)
}
}
return
}