Debian Codegraph @

tests/cli/stack-switching/fun-actor-lwt.wast

raw
1 ;; RUN: wast --assert default --snapshot tests/snapshots % -f stack-switching
2
3 ;; Actors via lightweight threads - functional version
4
5 ;; actor interface
6 (module $actor
7 (type $func (func))
8 (type $cont (cont $func))
9
10 (tag $self (export "self") (result i32))
11 (tag $spawn (export "spawn") (param (ref $cont)) (result i32))
12 (tag $send (export "send") (param i32 i32))
13 (tag $recv (export "recv") (result i32))
14 )
15 (register "actor")
16
17 ;; a simple example - pass a message through a chain of actors
18 (module $chain
19 (type $func (func))
20 (type $cont (cont $func))
21
22 (type $i-func (func (param i32)))
23 (type $i-cont (cont $i-func))
24
25 (tag $self (import "actor" "self") (result i32))
26 (tag $spawn (import "actor" "spawn") (param (ref $cont)) (result i32))
27 (tag $send (import "actor" "send") (param i32 i32))
28 (tag $recv (import "actor" "recv") (result i32))
29
30 (elem declare func $next)
31
32 (func $log (import "spectest" "print_i32") (param i32))
33
34 (func $next (param $p i32)
35 (local $s i32)
36 (local.set $s (suspend $recv))
37 (call $log (i32.const -1))
38 (suspend $send (local.get $s) (local.get $p))
39 )
40
41 ;; send the message 42 through a chain of n actors
42 (func $chain (export "chain") (param $n i32)
43 (local $s i32)
44 (local $p i32)
45 (local.set $p (suspend $self))
46
47 (loop $l
48 (if (i32.eqz (local.get $n))
49 (then (suspend $send (i32.const 42) (local.get $p)))
50 (else (local.set $p (suspend $spawn (cont.bind $i-cont $cont (local.get $p) (cont.new $i-cont (ref.func $next)))))
51 (local.set $n (i32.sub (local.get $n) (i32.const 1)))
52 (br $l))
53 )
54 )
55 (local.set $s (suspend $recv))
56 (call $log (local.get $s))
57 )
58 )
59 (register "chain")
60
61 ;; interface to lightweight threads
62 (module $lwt
63 (type $func (func))
64 (type $cont (cont $func))
65
66 (tag $yield (export "yield"))
67 (tag $fork (export "fork") (param (ref $cont)))
68 )
69 (register "lwt")
70
71 ;; queue of threads
72 (module $queue
73 (type $func (func))
74 (type $cont (cont $func))
75
76 ;; Table as simple queue (keeping it simple, no ring buffer)
77 (table $queue 0 (ref null $cont))
78 (global $qdelta i32 (i32.const 10))
79 (global $qback (mut i32) (i32.const 0))
80 (global $qfront (mut i32) (i32.const 0))
81
82 (func $queue-empty (export "queue-empty") (result i32)
83 (i32.eq (global.get $qfront) (global.get $qback))
84 )
85
86 (func $dequeue (export "dequeue") (result (ref null $cont))
87 (local $i i32)
88 (if (call $queue-empty)
89 (then (return (ref.null $cont)))
90 )
91 (local.set $i (global.get $qfront))
92 (global.set $qfront (i32.add (local.get $i) (i32.const 1)))
93 (table.get $queue (local.get $i))
94 )
95
96 (func $enqueue (export "enqueue") (param $k (ref $cont))
97 ;; Check if queue is full
98 (if (i32.eq (global.get $qback) (table.size $queue))
99 (then
100 ;; Check if there is enough space in the front to compact
101 (if (i32.lt_u (global.get $qfront) (global.get $qdelta))
102 (then
103 ;; Space is below threshold, grow table instead
104 (drop (table.grow $queue (ref.null $cont) (global.get $qdelta)))
105 )
106 (else
107 ;; Enough space, move entries up to head of table
108 (global.set $qback (i32.sub (global.get $qback) (global.get $qfront)))
109 (table.copy $queue $queue
110 (i32.const 0) ;; dest = new front = 0
111 (global.get $qfront) ;; src = old front
112 (global.get $qback) ;; len = new back = old back - old front
113 )
114 (table.fill $queue ;; null out old entries to avoid leaks
115 (global.get $qback) ;; start = new back
116 (ref.null $cont) ;; init value
117 (global.get $qfront) ;; len = old front = old front - new front
118 )
119 (global.set $qfront (i32.const 0))
120 )
121 )
122 )
123 )
124 (table.set $queue (global.get $qback) (local.get $k))
125 (global.set $qback (i32.add (global.get $qback) (i32.const 1)))
126 )
127 )
128 (register "queue")
129
130 ;; simple scheduler
131 (module $scheduler
132 (type $func (func))
133 (type $cont (cont $func))
134
135 (tag $yield (import "lwt" "yield"))
136 (tag $fork (import "lwt" "fork") (param (ref $cont)))
137
138 (func $queue-empty (import "queue" "queue-empty") (result i32))
139 (func $dequeue (import "queue" "dequeue") (result (ref null $cont)))
140 (func $enqueue (import "queue" "enqueue") (param $k (ref $cont)))
141
142 (func $run (export "run") (param $main (ref $cont))
143 (call $enqueue (local.get $main))
144 (loop $l
145 (if (call $queue-empty) (then (return)))
146 (block $on_yield (result (ref $cont))
147 (block $on_fork (result (ref $cont) (ref $cont))
148 (resume $cont (on $yield $on_yield) (on $fork $on_fork)
149 (call $dequeue)
150 )
151 (br $l) ;; thread terminated
152 ) ;; $on_fork (result (ref $cont) (ref $cont))
153 (call $enqueue) ;; current thread
154 (call $enqueue) ;; new thread
155 (br $l)
156 )
157 ;; $on_yield (result (ref $cont))
158 (call $enqueue) ;; current thread
159 (br $l)
160 )
161 )
162 )
163 (register "scheduler")
164
165 (module $mailboxes
166 ;; Stupid implementation of mailboxes that raises an exception if
167 ;; there are too many mailboxes or if more than one message is sent
168 ;; to any given mailbox.
169 ;;
170 ;; Sufficient for the simple chain example.
171
172 ;; -1 means empty
173
174 (tag $too-many-mailboxes)
175 (tag $too-many-messages)
176
177 (memory 1)
178
179 (global $msize (mut i32) (i32.const 0))
180 (global $mmax i32 (i32.const 1024)) ;; maximum number of mailboxes
181
182 (func $init (export "init")
183 (memory.fill (i32.const 0) (i32.const -1) (i32.mul (global.get $mmax) (i32.const 4)))
184 )
185
186 (func $empty-mb (export "empty-mb") (param $mb i32) (result i32)
187 (local $offset i32)
188 (local.set $offset (i32.mul (local.get $mb) (i32.const 4)))
189 (i32.eq (i32.load (local.get $offset)) (i32.const -1))
190 )
191
192 (func $new-mb (export "new-mb") (result i32)
193 (local $mb i32)
194
195 (if (i32.ge_u (global.get $msize) (global.get $mmax))
196 (then (throw $too-many-mailboxes))
197 )
198
199 (local.set $mb (global.get $msize))
200 (global.set $msize (i32.add (global.get $msize) (i32.const 1)))
201 (return (local.get $mb))
202 )
203
204 (func $send-to-mb (export "send-to-mb") (param $v i32) (param $mb i32)
205 (local $offset i32)
206 (local.set $offset (i32.mul (local.get $mb) (i32.const 4)))
207 (if (call $empty-mb (local.get $mb))
208 (then (i32.store (local.get $offset) (local.get $v)))
209 (else (throw $too-many-messages))
210 )
211 )
212
213 (func $recv-from-mb (export "recv-from-mb") (param $mb i32) (result i32)
214 (local $v i32)
215 (local $offset i32)
216 (local.set $offset (i32.mul (local.get $mb) (i32.const 4)))
217 (local.set $v (i32.load (local.get $offset)))
218 (i32.store (local.get $offset) (i32.const -1))
219 (local.get $v)
220 )
221 )
222 (register "mailboxes")
223
224 ;; actors via lightweight threads
225 (module $actor-as-lwt
226 (type $func (func))
227 (type $cont (cont $func))
228
229 (type $i-func (func (param i32)))
230 (type $i-cont (cont $i-func))
231
232 (type $icont-func (func (param i32 (ref $cont))))
233 (type $icont-cont (cont $icont-func))
234
235 (func $log (import "spectest" "print_i32") (param i32))
236
237 ;; lwt interface
238 (tag $yield (import "lwt" "yield"))
239 (tag $fork (import "lwt" "fork") (param (ref $cont)))
240
241 ;; mailbox interface
242 (func $init (import "mailboxes" "init"))
243 (func $empty-mb (import "mailboxes" "empty-mb") (param $mb i32) (result i32))
244 (func $new-mb (import "mailboxes" "new-mb") (result i32))
245 (func $send-to-mb (import "mailboxes" "send-to-mb") (param $v i32) (param $mb i32))
246 (func $recv-from-mb (import "mailboxes" "recv-from-mb") (param $mb i32) (result i32))
247
248 ;; actor interface
249 (tag $self (import "actor" "self") (result i32))
250 (tag $spawn (import "actor" "spawn") (param (ref $cont)) (result i32))
251 (tag $send (import "actor" "send") (param i32 i32))
252 (tag $recv (import "actor" "recv") (result i32))
253
254 (elem declare func $act-nullary $act-res)
255
256 ;; resume with $ik applied to $res
257 (func $act-res (param $mine i32) (param $res i32) (param $ik (ref $i-cont))
258 (local $yours i32)
259 (local $k (ref $cont))
260 (local $you (ref $cont))
261 (block $on_self (result (ref $i-cont))
262 (block $on_spawn (result (ref $cont) (ref $i-cont))
263 (block $on_send (result i32 i32 (ref $cont))
264 (block $on_recv (result (ref $i-cont))
265 ;; this should really be a tail call to the continuation
266 ;; do we need a 'return_resume' operator?
267 (resume $i-cont (on $self $on_self)
268 (on $spawn $on_spawn)
269 (on $send $on_send)
270 (on $recv $on_recv)
271 (local.get $res) (local.get $ik)
272 )
273 (return)
274 ) ;; $on_recv (result (ref $i-cont))
275 (local.set $ik)
276 ;; block this thread until the mailbox is non-empty
277 (loop $l
278 (if (call $empty-mb (local.get $mine))
279 (then (suspend $yield)
280 (br $l))
281 )
282 )
283 (call $recv-from-mb (local.get $mine))
284 (local.set $res)
285 (return_call $act-res (local.get $mine) (local.get $res) (local.get $ik))
286 ) ;; $on_send (result i32 i32 (ref $cont))
287 (local.set $k)
288 (call $send-to-mb)
289 (return_call $act-nullary (local.get $mine) (local.get $k))
290 ) ;; $on_spawn (result (ref $cont) (ref $i-cont))
291 (local.set $ik)
292 (local.set $you)
293 (call $new-mb)
294 (local.set $yours)
295 (suspend $fork (cont.bind $icont-cont $cont
296 (local.get $yours)
297 (local.get $you)
298 (cont.new $icont-cont (ref.func $act-nullary))))
299 (return_call $act-res (local.get $mine) (local.get $yours) (local.get $ik))
300 ) ;; $on_self (result (ref $i-cont))
301 (local.set $ik)
302 (return_call $act-res (local.get $mine) (local.get $mine) (local.get $ik))
303 )
304
305 ;; resume with nullary continuation
306 (func $act-nullary (param $mine i32) (param $k (ref $cont))
307 (local $res i32)
308 (local $ik (ref $i-cont))
309 (local $you (ref $cont))
310 (local $yours i32)
311 (block $on_self (result (ref $i-cont))
312 (block $on_spawn (result (ref $cont) (ref $i-cont))
313 (block $on_send (result i32 i32 (ref $cont))
314 (block $on_recv (result (ref $i-cont))
315 ;; this should really be a tail call to the continuation
316 ;; do we need a 'return_resume' operator?
317 (resume $cont (on $self $on_self)
318 (on $spawn $on_spawn)
319 (on $send $on_send)
320 (on $recv $on_recv)
321 (local.get $k)
322 )
323 (return)
324 ) ;; $on_recv (result (ref $i-cont))
325 (local.set $ik)
326 ;; block this thread until the mailbox is non-empty
327 (loop $l
328 (if (call $empty-mb (local.get $mine))
329 (then (suspend $yield)
330 (br $l))
331 )
332 )
333 (call $recv-from-mb (local.get $mine))
334 (local.set $res)
335 (return_call $act-res (local.get $mine) (local.get $res) (local.get $ik))
336 ) ;; $on_send (result i32 i32 (ref $cont))
337 (local.set $k)
338 (call $send-to-mb)
339 (return_call $act-nullary (local.get $mine) (local.get $k))
340 ) ;; $on_spawn (result (ref $cont) (ref $i-cont))
341 (local.set $ik)
342 (local.set $you)
343 (call $new-mb)
344 (local.set $yours)
345 (suspend $fork (cont.bind $icont-cont $cont
346 (local.get $yours)
347 (local.get $you)
348 (cont.new $icont-cont (ref.func $act-nullary))))
349 (return_call $act-res (local.get $mine) (local.get $yours) (local.get $ik))
350 ) ;; $on_self (result (ref $i-cont))
351 (local.set $ik)
352 (return_call $act-res (local.get $mine) (local.get $mine) (local.get $ik))
353 )
354
355 (func $act (export "act") (param $k (ref $cont))
356 (call $init)
357 (call $act-nullary (call $new-mb) (local.get $k))
358 )
359 )
360 (register "actor-as-lwt")
361
362 ;; composing the actor and scheduler handlers together
363 (module $actor-scheduler
364 (type $func (func))
365 (type $cont (cont $func))
366
367 (type $cont-func (func (param (ref $cont))))
368 (type $cont-cont (cont $cont-func))
369
370 (type $f-func (func (param (ref $func))))
371
372 (elem declare func $act $scheduler)
373
374 (func $act (import "actor-as-lwt" "act") (param $k (ref $cont)))
375 (func $scheduler (import "scheduler" "run") (param $k (ref $cont)))
376
377 (func $run-actor (export "run-actor") (param $k (ref $cont))
378 (call $scheduler (cont.bind $cont-cont $cont (local.get $k) (cont.new $cont-cont (ref.func $act))))
379 )
380 )
381 (register "actor-scheduler")
382
383 (module
384 (type $func (func))
385 (type $cont (cont $func))
386
387 (type $i-func (func (param i32)))
388 (type $i-cont (cont $i-func))
389
390 (elem declare func $chain)
391
392 (func $run-actor (import "actor-scheduler" "run-actor") (param $k (ref $cont)))
393 (func $chain (import "chain" "chain") (param $n i32))
394
395 (func $run-chain (export "run-chain") (param $n i32)
396 (call $run-actor (cont.bind $i-cont $cont (local.get $n) (cont.new $i-cont (ref.func $chain))))
397 )
398 )
399
400 (invoke "run-chain" (i32.const 64))