forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacklet.py
More file actions
289 lines (221 loc) · 7.21 KB
/
stacklet.py
File metadata and controls
289 lines (221 loc) · 7.21 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
py_object = object
import pixie.vm.object as object
from pixie.vm.object import affirm
from pixie.vm.primitives import nil, true, false
from pixie.vm.code import BaseCode
from pixie.vm.numbers import Integer
import pixie.vm.stdlib as proto
from pixie.vm.code import extend, as_var
from rpython.rlib.rarithmetic import r_uint, intmask, widen
import rpython.rlib.jit as jit
import pixie.vm.rt as rt
import pixie.vm.libs.uv as uv
import pixie.vm.libs.ring_buffer as ring_buffer
import rpython.rlib.rstacklet as rstacklet
from rpython.rtyper.lltypesystem import lltype, rffi
OP_NEW = 0x01
OP_SWITCH = 0x02
OP_CONTINUE = 0x03
OP_EXIT = 0x04
OP_YIELD = 0x05
OP_NEXT_PENDING = 0x06
OP_NEW_THREAD = 0x07
OP_EXECUTE_UV = 0x08
def to_main_loop():
global_state._h = global_state._th.switch(global_state._h)
class GlobalState(py_object):
def __init__(self):
self.reset()
def reset(self):
self._th = None
self._val = None
self._ex = None
self._op = 0x00
self._fn = None
self._init_fn = None
self._current = None
self._parent = None
def switch_back(self):
tmp = self._current
self._current = self._to
self._to = tmp
global_state = GlobalState()
def init():
if global_state._th is None:
global_state._th = rstacklet.StackletThread(rt.__config__)
global_state._h = global_state._th.get_null_handle()
def shutdown():
global_state._h = global_state._th.get_null_handle()
global_state.reset()
class FinishedToken(BaseCode):
_type = object.Type(u"pixie.stdlib.FinishedToken")
def __init__(self):
pass
def type(self):
return FinishedToken._type
finished_token = FinishedToken()
class WrappedHandler(BaseCode):
_type = object.Type(u"Stacklet")
def __init__(self, h):
self._h = h
self._is_finished = False
self._val = nil
def type(self):
return WrappedHandler._type
def is_finished(self):
return self._is_finished
def invoke(self, args):
affirm(len(args) == 1, u"Only one arg to continuation allowed")
affirm(not self._is_finished, u"Execution of this stacklet has completed")
global_state._parent = global_state._current
global_state._current = self
global_state._op = OP_SWITCH
global_state._val = args[0]
to_main_loop()
if global_state._val is finished_token:
global_state._parent._is_finished = True
global_state._parent._val = global_state._val
return global_state._val
def new_stacklet(f):
global_state._op = OP_NEW
global_state._val = f
to_main_loop()
val = global_state._val
return val
def new_thread(f):
global_state._op = OP_NEW_THREAD
global_state._val = f
to_main_loop()
def yield_stacklet():
global_state._op = OP_YIELD
global_state._val = nil
to_main_loop()
def execute_uv_func(func):
assert isinstance(func, uv.UVFunction)
global_state._op = OP_EXECUTE_UV
global_state._val = func
to_main_loop()
return global_state._val
def new_handler(h, o):
global_state._h = h
parent = global_state._parent
affirm(global_state._val is not None, u"Internal Stacklet Error")
f = global_state._val
global_state._val = None
to_main_loop()
#try:
f.invoke([global_state._parent])
global_state._parent.invoke([finished_token])
print "ENDED! Should never see this"
assert False
return global_state._h
def new_thread_handler(h, o):
global_state._h = h
affirm(global_state._val is not None, u"Internal Stacklet Error")
f = global_state._val
global_state._val = None
to_main_loop()
#try:
f.invoke([global_state._current])
global_state._op = OP_NEXT_PENDING
to_main_loop()
return global_state._h
def init_handler(h, o):
global_state._h = h
affirm(global_state._init_fn is not None, u"Internal Stacklet error")
f = global_state._init_fn
global_state._init_fn = None
#try:
f.invoke([])
#except Exception as ex:
# print "Uncaught Exception" + str(ex)
global_state._op = OP_EXIT
return global_state._h
pending_stacklets = ring_buffer.RingBuffer(r_uint(32))
def with_stacklets(f):
loop = uv.loop_new()
init()
global_state._init_fn = f
main_h = global_state._th.new(init_handler)
global_state._current = WrappedHandler(main_h)
while True:
if global_state._op == OP_NEW:
assert global_state._current
global_state._parent = global_state._current
new_h = global_state._th.new(new_handler)
wh = WrappedHandler(new_h)
global_state._val = wh
assert global_state._parent
global_state._current = global_state._parent
global_state._op = OP_SWITCH
continue
elif global_state._op == OP_NEW_THREAD:
wh = WrappedHandler(global_state._th.get_null_handle())
global_state._to = wh
wh._h = global_state._th.new(new_thread_handler)
pending_stacklets.push((wh, nil))
pending_stacklets.push((global_state._current, wh))
global_state._op = OP_NEXT_PENDING
continue
elif global_state._op == OP_SWITCH:
cur = global_state._current
cur._h = global_state._th.switch(cur._h)
continue
elif global_state._op == OP_EXIT:
shutdown()
return global_state._val
elif global_state._op == OP_YIELD:
assert global_state._current
pending_stacklets.push((global_state._current, nil))
global_state._op = OP_NEXT_PENDING
continue
elif global_state._op == OP_EXECUTE_UV:
assert global_state._current
k = global_state._current
f = global_state._val
f.execute_uv(loop, k)
global_state._op = OP_NEXT_PENDING
continue
elif global_state._op == OP_NEXT_PENDING:
while True:
if pending_stacklets.pending() == 0:
uv.run(loop, uv.RUN_DEFAULT)
continue
else:
uv.run(loop, uv.RUN_DEFAULT | uv.RUN_NO_WAIT)
break
(k, val) = pending_stacklets.pop()
global_state._val = val
global_state._current = k
k._h = global_state._th.switch(k._h)
continue
else:
assert False
shutdown()
return None
@as_var("create-stacklet")
def _new_stacklet(f):
stacklet = new_stacklet(f)
stacklet.invoke([nil]) # prime it
return stacklet
@as_var("create-thread")
def _new_stacklet(f):
stacklet = new_thread(f)
return nil
@as_var("yield-stacklet")
def _stacklet_yield():
yield_stacklet()
return nil
@extend(proto._at_end_QMARK_, WrappedHandler)
def _at_end(self):
assert isinstance(self, WrappedHandler)
return rt.wrap(self.is_finished())
@extend(proto._move_next_BANG_, WrappedHandler)
def _move_next(self):
assert isinstance(self, WrappedHandler)
self.invoke([nil])
return self
@extend(proto._current, WrappedHandler)
def _current(self):
assert isinstance(self, WrappedHandler)
return self._val