Debian Codegraph @

tests/cli/stack-switching/actor.wast

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