-
Notifications
You must be signed in to change notification settings - Fork 0
/
x_block_strategies.go
149 lines (121 loc) · 3.01 KB
/
x_block_strategies.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
138
139
140
141
142
143
144
145
146
147
148
149
package ipc
import (
"runtime"
"sync"
"sync/atomic"
"time"
"unsafe"
"github.com/benz9527/xboot/lib/infra"
)
var (
_ BlockStrategy = (*xGoSchedBlockStrategy)(nil)
_ BlockStrategy = (*xSleepBlockStrategy)(nil)
_ BlockStrategy = (*xCpuNoOpLoopBlockStrategy)(nil)
_ BlockStrategy = (*xOsYieldBlockStrategy)(nil)
_ BlockStrategy = (*xCacheChannelBlockStrategy)(nil)
_ BlockStrategy = (*xCondBlockStrategy)(nil)
)
type xGoSchedBlockStrategy struct{}
func NewXGoSchedBlockStrategy() BlockStrategy {
return &xGoSchedBlockStrategy{}
}
func (x *xGoSchedBlockStrategy) WaitFor(eqFn func() bool) {
// Let go runtime schedule other goroutines
// Current goroutine will yield CPU
runtime.Gosched()
}
func (x *xGoSchedBlockStrategy) Done() {
// do nothing
}
type xSleepBlockStrategy struct {
sleepTime time.Duration
}
func NewXSleepBlockStrategy(sleepTime time.Duration) BlockStrategy {
// around 10us, cpu load 2-3%
// eq to 5us, cpu load 10%
// lt 5us, cpu load 100%
return &xSleepBlockStrategy{
sleepTime: sleepTime,
}
}
func (bs *xSleepBlockStrategy) WaitFor(eqFn func() bool) {
time.Sleep(bs.sleepTime)
}
func (bs *xSleepBlockStrategy) Done() {
// do nothing
}
type xCpuNoOpLoopBlockStrategy struct {
cycles uint32
}
func NewXCpuNoOpLoopBlockStrategy(cycles uint32) BlockStrategy {
return &xCpuNoOpLoopBlockStrategy{
cycles: cycles,
}
}
func (bs *xCpuNoOpLoopBlockStrategy) WaitFor(eqFn func() bool) {
infra.ProcYield(bs.cycles)
}
func (bs *xCpuNoOpLoopBlockStrategy) Done() {}
type xOsYieldBlockStrategy struct{}
func NewXOsYieldBlockStrategy() BlockStrategy {
return &xOsYieldBlockStrategy{}
}
func (bs *xOsYieldBlockStrategy) WaitFor(fn func() bool) {
infra.OsYield()
}
func (bs *xOsYieldBlockStrategy) Done() {}
type xCacheChannelBlockStrategy struct {
_ [cacheLinePadSize - unsafe.Sizeof(*new(uint64))]byte
status uint64
_ [cacheLinePadSize - unsafe.Sizeof(*new(uint64))]byte
ch chan struct{}
}
func NewXCacheChannelBlockStrategy() BlockStrategy {
return &xCacheChannelBlockStrategy{
ch: make(chan struct{}, 1),
status: 0,
}
}
func (bs *xCacheChannelBlockStrategy) WaitFor(eqFn func() bool) {
// Try to block
if atomic.CompareAndSwapUint64(&bs.status, 0, 1) {
// Double check
if !eqFn() {
// Block, wait for signal
<-bs.ch
} else {
// Double check failed, reset status
if !atomic.CompareAndSwapUint64(&bs.status, 1, 0) {
// Wait for release
<-bs.ch
}
}
}
}
func (bs *xCacheChannelBlockStrategy) Done() {
// Release
if atomic.CompareAndSwapUint64(&bs.status, 1, 0) {
// Send signal
bs.ch <- struct{}{}
}
}
// https://gfw.go101.org/article/concurrent-synchronization-more.html
type xCondBlockStrategy struct {
cond *sync.Cond
}
func NewXCondBlockStrategy() BlockStrategy {
return &xCondBlockStrategy{
cond: sync.NewCond(&sync.Mutex{}),
}
}
func (bs *xCondBlockStrategy) WaitFor(eqFn func() bool) {
bs.cond.L.Lock()
defer bs.cond.L.Unlock()
if eqFn() {
return
}
bs.cond.Wait()
}
func (bs *xCondBlockStrategy) Done() {
bs.cond.Broadcast()
}