Debian Codegraph @

tests/cli/stack-switching/control-lwt.wast

raw
1 ;; RUN: wast --assert default --snapshot tests/snapshots % -f stack-switching
2
3 ;; dynamic lightweight threads via control/prompt
4
5 ;; interface to control/prompt
6 (module $control
7 (type $func (func)) ;; [] -> []
8 (type $cont (cont $func)) ;; cont ([] -> [])
9
10 (type $cont-func (func (param (ref $cont)))) ;; [cont ([] -> [])] -> []
11 (type $cont-cont (cont $cont-func)) ;; cont ([cont ([] -> [])] -> [])
12
13 ;; Implementation of a generic delimited control operator using
14 ;; effect handlers.
15 ;;
16 ;; For lightweight threads we have no payload. More general types
17 ;; for control and prompt are:
18 ;;
19 ;; control : ([cont ([ta*] -> [tr*])] -> [tr*]) -> [ta*]
20 ;; prompt : cont ([] -> [tr*]) -> [tr*]
21 ;;
22 ;; (We can also give more refined types if we want to support
23 ;; answer-type modification and various flavours of answer-type
24 ;; polymorphism - but these are well outside the scope of a Wasm
25 ;; proposal!)
26 ;;
27 ;; (Technically this is control0/prompt0 rather than
28 ;; control/prompt.)
29 (tag $control (export "control") (param (ref $cont-cont))) ;; control : ([cont ([] -> [])] -> []) -> []
30 (func $prompt (export "prompt") (param $nextk (ref null $cont)) ;; prompt : cont ([] -> []) -> []
31 (local $h (ref $cont-cont))
32 (local $k (ref $cont))
33 (block $on_control (result (ref $cont-cont) (ref $cont))
34 (resume $cont (on $control $on_control)
35 (local.get $nextk))
36 (return)
37 ) ;; $on_control (param (ref $cont-func) (ref $cont))
38 (local.set $k)
39 (local.set $h)
40 (resume $cont-cont (local.get $k) (local.get $h))
41 )
42 )
43 (register "control")
44
45 ;; With control/prompt we use functions for abstracting over yield and
46 ;; fork operations rather than tags.
47
48 (module $example
49 (type $func (func)) ;; [] -> []
50 (type $cont (cont $func)) ;; cont ([] -> [])
51
52 (type $cont-func (func (param (ref $cont)))) ;; [cont ([] -> [])] -> []
53 (type $cont-cont (cont $cont-func)) ;; cont ([cont ([] -> [])] -> [])
54
55 (type $func-cont-func-func (func (param (ref $func)) (param (ref $cont-func)))) ;; ([] -> []) -> ([cont ([] -> [])] -> []) -> []
56 (type $func-cont-func-cont (cont $func-cont-func-func)) ;; cont (([] -> []) -> ([cont ([] -> [])] -> []) -> [])
57
58 (func $log (import "spectest" "print_i32") (param i32))
59
60 (elem declare func $main $thread1 $thread2 $thread3)
61
62 (func $main (export "main") (param $yield (ref $func)) (param $fork (ref $cont-func))
63 (call $log (i32.const 0))
64 (call_ref $cont-func
65 (cont.bind $func-cont-func-cont $cont (local.get $yield) (local.get $fork)
66 (cont.new $func-cont-func-cont (ref.func $thread1)))
67 (local.get $fork))
68 (call $log (i32.const 1))
69 (call_ref $cont-func
70 (cont.bind $func-cont-func-cont $cont (local.get $yield) (local.get $fork)
71 (cont.new $func-cont-func-cont (ref.func $thread2)))
72 (local.get $fork))
73 (call $log (i32.const 2))
74 (call_ref $cont-func
75 (cont.bind $func-cont-func-cont $cont (local.get $yield) (local.get $fork)
76 (cont.new $func-cont-func-cont (ref.func $thread3)))
77 (local.get $fork))
78 (call $log (i32.const 3))
79 )
80
81 (func $thread1 (param $yield (ref $func)) (param $fork (ref $cont-func))
82 (call $log (i32.const 10))
83 (call_ref $func (local.get $yield))
84 (call $log (i32.const 11))
85 (call_ref $func (local.get $yield))
86 (call $log (i32.const 12))
87 )
88
89 (func $thread2 (param $yield (ref $func)) (param $fork (ref $cont-func))
90 (call $log (i32.const 20))
91 (call_ref $func (local.get $yield))
92 (call $log (i32.const 21))
93 (call_ref $func (local.get $yield))
94 (call $log (i32.const 22))
95 )
96
97 (func $thread3 (param $yield (ref $func)) (param $fork (ref $cont-func))
98 (call $log (i32.const 30))
99 (call_ref $func (local.get $yield))
100 (call $log (i32.const 31))
101 (call_ref $func (local.get $yield))
102 (call $log (i32.const 32))
103 )
104 )
105 (register "example")
106
107 (module $queue
108 (type $func (func))
109 (type $cont (cont $func))
110
111 (func $log (import "spectest" "print_i32") (param i32))
112
113 ;; Table as simple queue (keeping it simple, no ring buffer)
114 (table $queue 0 (ref null $cont))
115 (global $qdelta i32 (i32.const 10))
116 (global $qback (mut i32) (i32.const 0))
117 (global $qfront (mut i32) (i32.const 0))
118
119 (func $queue-empty (export "queue-empty") (result i32)
120 (i32.eq (global.get $qfront) (global.get $qback))
121 )
122
123 (func $dequeue (export "dequeue") (result (ref null $cont))
124 (local $i i32)
125 (if (call $queue-empty)
126 (then (return (ref.null $cont)))
127 )
128 (local.set $i (global.get $qfront))
129 (global.set $qfront (i32.add (local.get $i) (i32.const 1)))
130 (table.get $queue (local.get $i))
131 )
132
133 (func $enqueue (export "enqueue") (param $k (ref $cont))
134 ;; Check if queue is full
135 (if (i32.eq (global.get $qback) (table.size $queue))
136 (then
137 ;; Check if there is enough space in the front to compact
138 (if (i32.lt_u (global.get $qfront) (global.get $qdelta))
139 (then
140 ;; Space is below threshold, grow table instead
141 (drop (table.grow $queue (ref.null $cont) (global.get $qdelta)))
142 )
143 (else
144 ;; Enough space, move entries up to head of table
145 (global.set $qback (i32.sub (global.get $qback) (global.get $qfront)))
146 (table.copy $queue $queue
147 (i32.const 0) ;; dest = new front = 0
148 (global.get $qfront) ;; src = old front
149 (global.get $qback) ;; len = new back = old back - old front
150 )
151 (table.fill $queue ;; null out old entries to avoid leaks
152 (global.get $qback) ;; start = new back
153 (ref.null $cont) ;; init value
154 (global.get $qfront) ;; len = old front = old front - new front
155 )
156 (global.set $qfront (i32.const 0))
157 )
158 )
159 )
160 )
161 (table.set $queue (global.get $qback) (local.get $k))
162 (global.set $qback (i32.add (global.get $qback) (i32.const 1)))
163 )
164 )
165 (register "queue")
166
167 (module $scheduler
168 (type $func (func)) ;; [] -> []
169 (type $cont (cont $func)) ;; cont ([] -> [])
170
171 (type $cont-func (func (param (ref $cont)))) ;; [cont ([] -> [])] -> []
172 (type $cont-cont (cont $cont-func)) ;; cont ([cont ([] -> [])] -> [])
173
174 (type $func-cont-func-func (func (param (ref $func)) (param (ref $cont-func)))) ;; ([] -> []) -> ([cont ([] -> [])] -> []) -> []
175 (type $func-cont-func-cont (cont $func-cont-func-func)) ;; cont (([] -> []) -> ([cont ([] -> [])] -> []) -> [])
176
177 (type $func-cont-cont (func (param (ref $cont)) (param (ref $cont))))
178 (type $cont-cont-func (cont $func-cont-cont))
179
180 (func $log (import "spectest" "print_i32") (param i32))
181
182 ;; queue interface
183 (func $queue-empty (import "queue" "queue-empty") (result i32))
184 (func $dequeue (import "queue" "dequeue") (result (ref null $cont)))
185 (func $enqueue (import "queue" "enqueue") (param $k (ref $cont)))
186
187 (elem declare func
188 $handle-yield-sync $handle-yield
189 $handle-fork-sync $handle-fork-kt $handle-fork-tk $handle-fork-ykt $handle-fork-ytk
190 $yield
191 $fork-sync $fork-kt $fork-tk $fork-ykt $fork-ytk)
192
193 ;; control/prompt interface
194 (tag $control (import "control" "control") (param (ref $cont-cont))) ;; control : ([cont ([] -> [])] -> []) -> []
195 (func $prompt (import "control" "prompt") (param $nextk (ref null $cont))) ;; prompt : cont ([] -> []) -> []
196
197 ;; generic boilerplate scheduler
198 ;;
199 ;; with control/prompt the core scheduler loop must be decoupled
200 ;; from the implementations of each operation (yield / fork) as the
201 ;; latter are passed in as arguments to user code
202 (func $scheduler (param $nextk (ref null $cont))
203 (loop $loop
204 (if (ref.is_null (local.get $nextk)) (then (return)))
205 (call $prompt (local.get $nextk))
206 (local.set $nextk (call $dequeue))
207 (br $loop)
208 )
209 )
210
211 ;; func.bind is needed in the implementations of fork
212 ;;
213 ;; More generally func.bind is needed for any operation that
214 ;; takes arguments.
215 ;;
216 ;; One could use another continuation here instead, but constructing
217 ;; a new continuation every time an operation is invoked seems
218 ;; unnecessarily wasteful.
219
220 ;; synchronous scheduler
221 (func $handle-yield-sync (param $k (ref $cont))
222 (call $scheduler (local.get $k))
223 )
224 (func $yield-sync
225 (suspend $control (cont.new $cont-cont (ref.func $handle-yield)))
226 )
227 (func $handle-fork-sync (param $t (ref $cont)) (param $k (ref $cont))
228 (call $enqueue (local.get $t))
229 (call $scheduler (local.get $k))
230 )
231 (func $fork-sync (param $t (ref $cont))
232 (suspend $control
233 (cont.bind $cont-cont-func $cont-cont (local.get $t)
234 (cont.new $cont-cont-func (ref.func $handle-fork-sync))))
235 )
236 (func $sync (export "sync") (param $k (ref $func-cont-func-cont))
237 (call $scheduler
238 (cont.bind $func-cont-func-cont $cont (ref.func $yield) (ref.func $fork-sync) (local.get $k)))
239 )
240
241 ;; asynchronous yield (used by all asynchronous schedulers)
242 (func $handle-yield (param $k (ref $cont))
243 (call $enqueue (local.get $k))
244 (call $scheduler (call $dequeue))
245 )
246 (func $yield
247 (suspend $control (cont.new $cont-cont (ref.func $handle-yield)))
248 )
249 ;; four asynchronous implementations of fork:
250 ;; * kt and tk don't yield on encountering a fork
251 ;; 1) kt runs the continuation, queuing up the new thread for later
252 ;; 2) tk runs the new thread first, queuing up the continuation for later
253 ;; * ykt and ytk do yield on encountering a fork
254 ;; 3) ykt runs the continuation, queuing up the new thread for later
255 ;; 4) ytk runs the new thread first, queuing up the continuation for later
256
257 ;; no yield on fork, continuation first
258 (func $handle-fork-kt (param $t (ref $cont)) (param $k (ref $cont))
259 (call $enqueue (local.get $t))
260 (call $scheduler (local.get $k))
261 )
262 (func $fork-kt (param $t (ref $cont))
263 (suspend $control
264 (cont.bind $cont-cont-func $cont-cont (local.get $t)
265 (cont.new $cont-cont-func (ref.func $handle-fork-kt))))
266 )
267 (func $kt (export "kt") (param $k (ref $func-cont-func-cont))
268 (call $scheduler
269 (cont.bind $func-cont-func-cont $cont (ref.func $yield) (ref.func $fork-kt) (local.get $k)))
270 )
271
272 ;; no yield on fork, new thread first
273 (func $handle-fork-tk (param $t (ref $cont)) (param $k (ref $cont))
274 (call $enqueue (local.get $k))
275 (call $scheduler (local.get $t))
276 )
277 (func $fork-tk (param $t (ref $cont))
278 (suspend $control
279 (cont.bind $cont-cont-func $cont-cont (local.get $t)
280 (cont.new $cont-cont-func (ref.func $handle-fork-tk))))
281 )
282 (func $tk (export "tk") (param $k (ref $func-cont-func-cont))
283 (call $scheduler
284 (cont.bind $func-cont-func-cont $cont (ref.func $yield) (ref.func $fork-tk) (local.get $k)))
285 )
286
287 ;; yield on fork, continuation first
288 (func $handle-fork-ykt (param $t (ref $cont)) (param $k (ref $cont))
289 (call $enqueue (local.get $k))
290 (call $enqueue (local.get $t))
291 (call $scheduler (call $dequeue))
292 )
293 (func $fork-ykt (param $t (ref $cont))
294 (suspend $control
295 (cont.bind $cont-cont-func $cont-cont (local.get $t)
296 (cont.new $cont-cont-func (ref.func $handle-fork-ykt))))
297 )
298 (func $ykt (export "ykt") (param $k (ref $func-cont-func-cont))
299 (call $scheduler
300 (cont.bind $func-cont-func-cont $cont (ref.func $yield) (ref.func $fork-ykt) (local.get $k)))
301 )
302
303 ;; yield on fork, new thread first
304 (func $handle-fork-ytk (param $t (ref $cont)) (param $k (ref $cont))
305 (call $enqueue (local.get $t))
306 (call $enqueue (local.get $k))
307 (call $scheduler (call $dequeue))
308 )
309 (func $fork-ytk (param $t (ref $cont))
310 (suspend $control
311 (cont.bind $cont-cont-func $cont-cont (local.get $t)
312 (cont.new $cont-cont-func (ref.func $handle-fork-ytk))))
313 )
314 (func $ytk (export "ytk") (param $k (ref $func-cont-func-cont))
315 (call $scheduler
316 (cont.bind $func-cont-func-cont $cont (ref.func $yield) (ref.func $fork-ytk) (local.get $k)))
317 )
318 )
319 (register "scheduler")
320
321 (module
322 (type $func (func)) ;; [] -> []
323 (type $cont (cont $func)) ;; cont ([] -> [])
324
325 (type $cont-func (func (param (ref $cont)))) ;; [cont ([] -> [])] -> []
326 (type $cont-cont (cont $cont-func)) ;; cont ([cont ([] -> [])] -> [])
327
328 (type $func-cont-func-func (func (param (ref $func)) (param (ref $cont-func)))) ;; ([] -> []) -> ([cont ([] -> [])] -> []) -> []
329 (type $func-cont-func-cont (cont $func-cont-func-func)) ;; cont (([] -> []) -> ([cont ([] -> [])] -> []) -> [])
330
331 (func $scheduler-sync (import "scheduler" "sync") (param $nextk (ref $func-cont-func-cont)))
332 (func $scheduler-kt (import "scheduler" "kt") (param $nextk (ref $func-cont-func-cont)))
333 (func $scheduler-tk (import "scheduler" "tk") (param $nextk (ref $func-cont-func-cont)))
334 (func $scheduler-ykt (import "scheduler" "ykt") (param $nextk (ref $func-cont-func-cont)))
335 (func $scheduler-ytk (import "scheduler" "ytk") (param $nextk (ref $func-cont-func-cont)))
336
337 (func $log (import "spectest" "print_i32") (param i32))
338
339 (func $main (import "example" "main") (param $yield (ref $func)) (param $fork (ref $cont-func)))
340
341 (elem declare func $main)
342
343 (func $run (export "run")
344 (call $log (i32.const -1))
345 (call $scheduler-sync (cont.new $func-cont-func-cont (ref.func $main)))
346 (call $log (i32.const -2))
347 (call $scheduler-kt (cont.new $func-cont-func-cont (ref.func $main)))
348 (call $log (i32.const -3))
349 (call $scheduler-tk (cont.new $func-cont-func-cont (ref.func $main)))
350 (call $log (i32.const -4))
351 (call $scheduler-ykt (cont.new $func-cont-func-cont (ref.func $main)))
352 (call $log (i32.const -5))
353 (call $scheduler-ytk (cont.new $func-cont-func-cont (ref.func $main)))
354 (call $log (i32.const -6))
355 )
356 )
357
358 (invoke "run")