forked from eosspark/geos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goroutine_reactor.go
88 lines (69 loc) · 1.29 KB
/
goroutine_reactor.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
package asio
import "github.com/eapache/channels"
const Infinity = false
type GoroutineReactor struct {
opq chan operation
opQueue *channels.InfiniteChannel
sigQueue chan operation
down chan struct{}
}
func NewGoroutineReactor() *GoroutineReactor {
r := new(GoroutineReactor)
if Infinity {
r.opQueue = channels.NewInfiniteChannel()
} else {
r.opq = make(chan operation, 128)
}
r.sigQueue = make(chan operation, 1)
r.down = make(chan struct{}, 1)
return r
}
func (i *IoContext) GetService() ReactorService {
if i.service == nil {
i.service = NewGoroutineReactor()
}
return i.service
}
func (g *GoroutineReactor) run() {
if Infinity {
LP1:
for {
select {
case <-g.down:
break LP1
case op := <-g.sigQueue:
op.call()
case op := <-g.opQueue.Out():
op.(operation).call()
default:
}
}
} else {
LP2:
for {
select {
case <-g.down:
break LP2
case op := <-g.sigQueue:
op.call()
case op := <-g.opq:
op.call()
default:
}
}
}
}
func (g *GoroutineReactor) stop() {
g.down <- struct{}{}
}
func (g *GoroutineReactor) post(op operation) {
//g.opQueue <- operation{op, args}
if Infinity {
g.opQueue.In() <- op
} else {
g.opq <- op
}
}
func (g *GoroutineReactor) notify(op operation) {
g.sigQueue <- op
}