forked from vova616/GarageEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Coroutine.go
170 lines (152 loc) · 2.65 KB
/
Coroutine.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package cr
import (
"fmt"
"runtime"
"time"
)
type Command byte
const (
Continue = Command(iota)
Close = Command(iota)
Idle = Command(iota)
)
var (
coroutines []*Coroutine = make([]*Coroutine, 0, 100)
current *Coroutine
index int = 0
end chan bool = make(chan bool)
runningCoroutines bool = false
)
type Coroutine struct {
in chan Command
State Command
UserData interface{}
}
func (gr *Coroutine) WaitForCommand() {
gr.State = Idle
act := <-gr.in
gr.State = act
switch act {
case Continue:
break
case Close:
coroutines[len(coroutines)-1], coroutines[index], coroutines = nil, coroutines[len(coroutines)-1], coroutines[:len(coroutines)-1]
}
}
func Start(fnc func()) *Coroutine {
gr := &Coroutine{make(chan Command, 1), Idle, nil}
coroutines = append(coroutines, gr)
go start(fnc, gr)
return gr
}
func runNext() {
index++
if index < len(coroutines) {
current = coroutines[index]
current.in <- Continue
} else {
end <- true
}
}
func start(fnc func(), gr *Coroutine) {
defer errorFunc()
defer gr.endFunc()
gr.WaitForCommand()
fnc()
}
func (gr *Coroutine) endFunc() {
gr.State = Close
coroutines[len(coroutines)-1], coroutines[index], coroutines = nil, coroutines[len(coroutines)-1], coroutines[:len(coroutines)-1]
runNext()
}
func errorFunc() {
if p := recover(); p != nil {
fmt.Println(p, PanicPath())
}
}
func Clear() {
coroutines = coroutines[:0]
}
func Skip() {
if !runningCoroutines {
return
}
c := current
runNext()
c.WaitForCommand()
}
func YieldCoroutine(gr *Coroutine) {
if !runningCoroutines {
return
}
for gr.State != Close {
Skip()
}
}
func YieldUntil(done <-chan bool) {
if !runningCoroutines {
return
}
for {
select {
case <-done:
return
default:
Skip()
}
}
}
func Sleep(seconds float32) {
if !runningCoroutines {
return
}
start := time.Now()
for {
Skip()
if time.Since(start).Seconds() >= float64(seconds) {
break
}
}
/*
Signal := NewSignal()
go func() {
<-time.After(time.Second * 3)
Signal.SendEnd()
}()
Yield(Signal)
*/
}
func Run() {
runningCoroutines = true
index = 0
if len(coroutines) > 0 {
current = coroutines[0]
coroutines[0].in <- Continue
<-end
}
runningCoroutines = false
current = nil
}
func PanicPath() string {
fullPath := ""
skip := 3
for i := skip; ; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
if i > skip {
fullPath += ", "
}
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
fullPath += fmt.Sprintf("%s:%d", file, line)
}
return fullPath
}