Repository navigation
Expand file tree
/
Copy pathcor.go
More file actions
256 lines (219 loc) · 5.01 KB
/
Copy pathcor.go
File metadata and controls
256 lines (219 loc) · 5.01 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package fpgo
import (
"sync"
"sync/atomic"
)
// AtomBool Atomic Bool
type AtomBool struct{ flag int32 }
// Set Set the bool atomically
func (atomBoolSelf *AtomBool) Set(value bool) {
var i int32
i = 0
if value {
i = 1
}
atomic.StoreInt32(&(atomBoolSelf.flag), int32(i))
}
// Get Get the bool atomically
func (atomBoolSelf *AtomBool) Get() bool {
if atomic.LoadInt32(&(atomBoolSelf.flag)) != 0 {
return true
}
return false
}
// CompareAndSwap atomically sets the bool to newValue if it currently equals
// oldValue, returning true when the swap happened. Useful for close-once guards.
func (atomBoolSelf *AtomBool) CompareAndSwap(oldValue, newValue bool) bool {
var oldInt, newInt int32
if oldValue {
oldInt = 1
}
if newValue {
newInt = 1
}
return atomic.CompareAndSwapInt32(&(atomBoolSelf.flag), oldInt, newInt)
}
// CorOp Cor Yield Operation/Delegation/Callback
type CorOp struct {
cor *CorDef
val interface{}
}
// CorDef Cor Coroutine inspired by Python/Ecmascript/Lua
type CorDef struct {
isStarted AtomBool
isClosed AtomBool
closedM sync.Mutex
opCh chan *CorOp
resultCh chan interface{}
done chan struct{}
effect func()
}
// New New a Cor instance
func (corSelf *CorDef) New(effect func()) *CorDef {
cor := &CorDef{
effect: effect,
opCh: make(chan *CorOp, 5),
resultCh: make(chan interface{}, 5),
done: make(chan struct{}),
isStarted: AtomBool{flag: 0},
}
return cor
}
// NewAndStart New a Cor instance and start it immediately
func (corSelf *CorDef) NewAndStart(effect func()) *CorDef {
cor := corSelf.New(effect)
cor.Start()
return cor
}
// DoNotation Do Notation by function (inspired by Haskell one)
func (corSelf *CorDef) DoNotation(effect func(*CorDef) interface{}) interface{} {
var result interface{}
var wg sync.WaitGroup
wg.Add(1)
var cor *CorDef
cor = corSelf.New(func() {
result = effect(cor)
wg.Done()
})
cor.Start()
wg.Wait()
return result
}
// StartWithVal Start the Cor with an initial value
func (corSelf *CorDef) StartWithVal(in interface{}) {
if corSelf.IsDone() || corSelf.isStarted.Get() {
return
}
corSelf.receive(nil, in)
corSelf.Start()
}
// Start Start the Cor
func (corSelf *CorDef) Start() {
if corSelf.IsDone() || corSelf.isStarted.Get() {
return
}
corSelf.isStarted.Set(true)
go func() {
corSelf.effect()
corSelf.close()
}()
}
// Yield Yield back(nil)
func (corSelf *CorDef) Yield() interface{} {
return corSelf.YieldRef(nil)
}
// YieldRef Yield a value
func (corSelf *CorDef) YieldRef(out interface{}) interface{} {
var result interface{}
if corSelf.IsDone() {
return result
}
var op *CorOp
var more bool
// fmt.Println(corSelf, "Wait for", "op")
op, more = <-corSelf.opCh
// fmt.Println(corSelf, "Wait for", "op", "done")
if more && op != nil && op.cor != nil {
cor := op.cor
cor.doCloseSafe(func() {
cor.resultCh <- out
})
}
if op != nil {
result = op.val
}
return result
}
// YieldFrom Yield from a given Cor
func (corSelf *CorDef) YieldFrom(target *CorDef, in interface{}) interface{} {
var result interface{}
if corSelf.IsDone() {
return result
}
if !target.receive(corSelf, in) {
return result
}
// resultCh is not closed (a close would race the send). target.done is.
// If both are ready, the done arm still drains a result that was sent
// before close: that send happens-before done is closed.
select {
case result = <-corSelf.resultCh:
case <-target.done:
select {
case result = <-corSelf.resultCh:
default:
}
case <-corSelf.done:
select {
case result = <-corSelf.resultCh:
default:
}
}
return result
}
func (corSelf *CorDef) receive(cor *CorDef, in interface{}) bool {
corSelf.closedM.Lock()
if corSelf.IsDone() || corSelf.opCh == nil {
corSelf.closedM.Unlock()
return false
}
ch := corSelf.opCh
done := corSelf.done
corSelf.closedM.Unlock()
if done == nil {
ch <- &CorOp{cor: cor, val: in}
return true
}
select {
case <-done:
return false
case ch <- &CorOp{cor: cor, val: in}:
return true
}
}
// YieldFromIO Yield from a given MonadIO
func (corSelf *CorDef) YieldFromIO(target *MonadIODef) interface{} {
var result interface{}
var wg sync.WaitGroup
wg.Add(1)
target.SubscribeOn(nil).Subscribe(Subscription{
OnNext: func(in interface{}) {
result = in
wg.Done()
},
})
wg.Wait()
return result
}
// IsDone Is the Cor done
func (corSelf *CorDef) IsDone() bool {
return corSelf.isClosed.Get()
}
// IsStarted Is the Cor started
func (corSelf *CorDef) IsStarted() bool {
return corSelf.isStarted.Get()
}
func (corSelf *CorDef) close() {
corSelf.closedM.Lock()
defer corSelf.closedM.Unlock()
if corSelf.isClosed.Get() {
return
}
corSelf.isClosed.Set(true)
// Close done only. Closing opCh/resultCh here races any in-flight send,
// and closing them while the sender holds closedM deadlocks.
if corSelf.done != nil {
close(corSelf.done)
}
}
func (corSelf *CorDef) doCloseSafe(fn func()) {
corSelf.closedM.Lock()
done := corSelf.IsDone()
corSelf.closedM.Unlock()
if done {
return
}
fn()
}
// Cor Cor utils instance
var Cor CorDef