forked from name5566/leaf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo.go
122 lines (103 loc) · 1.99 KB
/
go.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
package g
import (
"container/list"
"github.com/name5566/leaf/conf"
"github.com/name5566/leaf/log"
"runtime"
"sync"
)
// one Go per goroutine (goroutine not safe)
type Go struct {
ChanCb chan func()
pendingGo int
}
type LinearGo struct {
f func()
cb func()
}
type LinearContext struct {
g *Go
linearGo *list.List
mutexLinearGo sync.Mutex
mutexExecution sync.Mutex
}
func New(l int) *Go {
g := new(Go)
g.ChanCb = make(chan func(), l)
return g
}
func (g *Go) Go(f func(), cb func()) {
g.pendingGo++
go func() {
defer func() {
g.ChanCb <- cb
if r := recover(); r != nil {
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
l := runtime.Stack(buf, false)
log.Error("%v: %s", r, buf[:l])
} else {
log.Error("%v", r)
}
}
}()
f()
}()
}
func (g *Go) Cb(cb func()) {
defer func() {
g.pendingGo--
if r := recover(); r != nil {
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
l := runtime.Stack(buf, false)
log.Error("%v: %s", r, buf[:l])
} else {
log.Error("%v", r)
}
}
}()
if cb != nil {
cb()
}
}
func (g *Go) Close() {
for g.pendingGo > 0 {
g.Cb(<-g.ChanCb)
}
}
func (g *Go) Idle() bool {
return g.pendingGo == 0
}
func (g *Go) NewLinearContext() *LinearContext {
c := new(LinearContext)
c.g = g
c.linearGo = list.New()
return c
}
func (c *LinearContext) Go(f func(), cb func()) {
c.g.pendingGo++
c.mutexLinearGo.Lock()
c.linearGo.PushBack(&LinearGo{f: f, cb: cb})
c.mutexLinearGo.Unlock()
go func() {
c.mutexExecution.Lock()
defer c.mutexExecution.Unlock()
c.mutexLinearGo.Lock()
e := c.linearGo.Remove(c.linearGo.Front()).(*LinearGo)
c.mutexLinearGo.Unlock()
defer func() {
c.g.ChanCb <- e.cb
if r := recover(); r != nil {
if conf.LenStackBuf > 0 {
buf := make([]byte, conf.LenStackBuf)
l := runtime.Stack(buf, false)
log.Error("%v: %s", r, buf[:l])
} else {
log.Error("%v", r)
}
}
}()
e.f()
}()
}