Debian Codegraph @

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

raw
1 ;; RUN: wast --assert default --snapshot tests/snapshots % -f stack-switching
2
3 ;; Actors via lightweight threads
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 ;; interface to lightweight threads
68 (module $lwt
69 (type $func (func))
70 (type $cont (cont $func))
71
72 (tag $yield (export "yield"))
73 (tag $fork (export "fork") (param (ref $cont)))
74 )
75 (register "lwt")
76
77 ;; queue of threads
78 (module $queue
79 (type $func (func))
80 (type $cont (cont $func))
81
82 ;; Table as simple queue (keeping it simple, no ring buffer)
83 (table $queue 0 (ref null $cont))
84 (global $qdelta i32 (i32.const 10))
85 (global $qback (mut i32) (i32.const 0))
86 (global $qfront (mut i32) (i32.const 0))
87
88 (func $queue-empty (export "queue-empty") (result i32)
89 (i32.eq (global.get $qfront) (global.get $qback))
90 )
91
92 (func $dequeue (export "dequeue") (result (ref null $cont))
93 (local $i i32)
94 (if (call $queue-empty)
95 (then (return (ref.null $cont)))
96 )
97 (local.set $i (global.get $qfront))
98 (global.set $qfront (i32.add (local.get $i) (i32.const 1)))
99 (table.get $queue (local.get $i))
100 )
101
102 (func $enqueue (export "enqueue") (param $k (ref $cont))
103 ;; Check if queue is full
104 (if (i32.eq (global.get $qback) (table.size $queue))
105 (then
106 ;; Check if there is enough space in the front to compact
107 (if (i32.lt_u (global.get $qfront) (global.get $qdelta))
108 (then
109 ;; Space is below threshold, grow table instead
110 (drop (table.grow $queue (ref.null $cont) (global.get $qdelta)))
111 )
112 (else
113 ;; Enough space, move entries up to head of table
114 (global.set $qback (i32.sub (global.get $qback) (global.get $qfront)))
115 (table.copy $queue $queue
116 (i32.const 0) ;; dest = new front = 0
117 (global.get $qfront) ;; src = old front
118 (global.get $qback) ;; len = new back = old back - old front
119 )
120 (table.fill $queue ;; null out old entries to avoid leaks
121 (global.get $qback) ;; start = new back
122 (ref.null $cont) ;; init value
123 (global.get $qfront) ;; len = old front = old front - new front
124 )
125 (global.set $qfront (i32.const 0))
126 )
127 )
128 )
129 )
130 (table.set $queue (global.get $qback) (local.get $k))
131 (global.set $qback (i32.add (global.get $qback) (i32.const 1)))
132 )
133 )
134 (register "queue")
135
136 ;; simple scheduler for lightweight threads
137 (module $scheduler
138 (type $func (func))
139 (type $cont (cont $func))
140
141 (tag $yield (import "lwt" "yield"))
142 (tag $fork (import "lwt" "fork") (param (ref $cont)))
143
144 (func $queue-empty (import "queue" "queue-empty") (result i32))
145 (func $dequeue (import "queue" "dequeue") (result (ref null $cont)))
146 (func $enqueue (import "queue" "enqueue") (param $k (ref $cont)))
147
148 (func $run (export "run") (param $main (ref $cont))
149 (call $enqueue (local.get $main))
150 (loop $l
151 (if (call $queue-empty) (then (return)))
152 (block $on_yield (result (ref $cont))
153 (block $on_fork (result (ref $cont) (ref $cont))
154 (resume $cont (on $yield $on_yield) (on $fork $on_fork)
155 (call $dequeue)
156 )
157 (br $l) ;; thread terminated
158 ) ;; $on_fork (result (ref $cont) (ref $cont))
159 (call $enqueue) ;; current thread
160 (call $enqueue) ;; new thread
161 (br $l)
162 )
163 ;; $on_yield (result (ref $cont))
164 (call $enqueue) ;; current thread
165 (br $l)
166 )
167 )
168 )
169 (register "scheduler")
170
171 (module $mailboxes
172 ;; Stupid implementation of mailboxes that raises an exception if
173 ;; there are too many mailboxes or if more than one message is sent
174 ;; to any given mailbox.
175 ;;
176 ;; Sufficient for the simple chain example.
177
178 ;; -1 means empty
179
180 (tag $too-many-mailboxes)
181 (tag $too-many-messages)
182
183 (memory 1)
184
185 (global $msize (mut i32) (i32.const 0))
186 (global $mmax i32 (i32.const 1024)) ;; maximum number of mailboxes
187
188 (func $init (export "init")
189 (memory.fill (i32.const 0) (i32.const -1) (i32.mul (global.get $mmax) (i32.const 4)))
190 )
191
192 (func $empty-mb (export "empty-mb") (param $mb i32) (result i32)
193 (local $offset i32)
194 (local.set $offset (i32.mul (local.get $mb) (i32.const 4)))
195 (i32.eq (i32.load (local.get $offset)) (i32.const -1))
196 )
197
198 (func $new-mb (export "new-mb") (result i32)
199 (local $mb i32)
200
201 (if (i32.ge_u (global.get $msize) (global.get $mmax))
202 (then (throw $too-many-mailboxes))
203 )
204
205 (local.set $mb (global.get $msize))
206 (global.set $msize (i32.add (global.get $msize) (i32.const 1)))
207 (return (local.get $mb))
208 )
209
210 (func $send-to-mb (export "send-to-mb") (param $v i32) (param $mb i32)
211 (local $offset i32)
212 (local.set $offset (i32.mul (local.get $mb) (i32.const 4)))
213 (if (call $empty-mb (local.get $mb))
214 (then (i32.store (local.get $offset) (local.get $v)))
215 (else (throw $too-many-messages))
216 )
217 )
218
219 (func $recv-from-mb (export "recv-from-mb") (param $mb i32) (result i32)
220 (local $v i32)
221 (local $offset i32)
222 (local.set $offset (i32.mul (local.get $mb) (i32.const 4)))
223 (local.set $v (i32.load (local.get $offset)))
224 (i32.store (local.get $offset) (i32.const -1))
225 (local.get $v)
226 )
227 )
228 (register "mailboxes")
229
230 ;; actors implemented via lightweight threads
231 (module $actor-as-lwt
232 (type $func (func)) ;; [] -> []
233 (type $cont (cont $func)) ;; cont ([] -> [])
234
235 (type $i-func (func (param i32))) ;; [i32] -> []
236 (type $i-cont (cont $i-func)) ;; cont ([i32] -> [])
237
238 (type $ic-func (func (param i32 (ref $cont)))) ;; [i32 (cont ([] -> []))] -> []
239 (type $ic-cont (cont $ic-func)) ;; cont ([i32 (cont ([] -> []))] -> [])
240
241 (func $log (import "spectest" "print_i32") (param i32))
242
243 ;; lwt interface
244 (tag $yield (import "lwt" "yield"))
245 (tag $fork (import "lwt" "fork") (param (ref $cont)))
246
247 ;; mailbox interface
248 (func $init (import "mailboxes" "init"))
249 (func $empty-mb (import "mailboxes" "empty-mb") (param $mb i32) (result i32))
250 (func $new-mb (import "mailboxes" "new-mb") (result i32))
251 (func $send-to-mb (import "mailboxes" "send-to-mb") (param $v i32) (param $mb i32))
252 (func $recv-from-mb (import "mailboxes" "recv-from-mb") (param $mb i32) (result i32))
253
254 ;; queue interface
255 (func $queue-empty (import "queue" "queue-empty") (result i32))
256 (func $dequeue (import "queue" "dequeue") (result (ref null $cont)))
257 (func $enqueue (import "queue" "enqueue") (param $k (ref $cont)))
258
259 ;; actor interface
260 ;; self : [i32] -> []
261 ;; spawn : [cont ([] -> [])] -> [i32]
262 ;; send : [i32 i32] -> []
263 ;; recv : [] -> [i32]
264 (tag $self (import "actor" "self") (result i32))
265 (tag $spawn (import "actor" "spawn") (param (ref $cont)) (result i32))
266 (tag $send (import "actor" "send") (param i32 i32))
267 (tag $recv (import "actor" "recv") (result i32))
268
269 (elem declare func $actk)
270
271 (func $actk (param $mine i32) (param $nextk (ref $cont))
272 (local $ik (ref $i-cont))
273 (local $k (ref $cont))
274 (local $you (ref $cont))
275 (local $yours i32)
276 (loop $l
277 (block $on_self (result (ref $i-cont))
278 (block $on_spawn (result (ref $cont) (ref $i-cont))
279 (block $on_send (result i32 i32 (ref $cont))
280 (block $on_recv (result (ref $i-cont))
281 (resume $cont (on $self $on_self)
282 (on $spawn $on_spawn)
283 (on $send $on_send)
284 (on $recv $on_recv)
285 (local.get $nextk)
286 )
287 (return)
288 ) ;; $on_recv (result (ref $i-cont))
289 (local.set $ik)
290 ;; block this thread until the mailbox is non-empty
291 (loop $blocked
292 (if (call $empty-mb (local.get $mine))
293 (then (suspend $yield)
294 (br $blocked))
295 )
296 )
297 (local.set $nextk (cont.bind $i-cont $cont (call $recv-from-mb (local.get $mine)) (local.get $ik)))
298 (br $l)
299 ) ;; $on_send (result i32 i32 (ref $cont))
300 (local.set $k)
301 (call $send-to-mb)
302 (local.set $nextk (local.get $k))
303 (br $l)
304 ) ;; $on_spawn (result (ref $cont) (ref $i-cont))
305 (local.set $ik)
306 (local.set $you)
307 (call $new-mb)
308 (local.set $yours)
309 (suspend $fork (cont.bind $ic-cont $cont
310 (local.get $yours)
311 (local.get $you)
312 (cont.new $ic-cont (ref.func $actk))))
313 (local.set $nextk (cont.bind $i-cont $cont (local.get $yours) (local.get $ik)))
314 (br $l)
315 ) ;; $on_self (result (ref $i-cont))
316 (local.set $ik)
317 (local.set $nextk (cont.bind $i-cont $cont (local.get $mine) (local.get $ik)))
318 (br $l)
319 )
320 )
321
322 (func $act (export "act") (param $k (ref $cont))
323 (call $init)
324 (call $actk (call $new-mb) (local.get $k))
325 )
326 )
327 (register "actor-as-lwt")
328
329 ;; composing the actor and scheduler handlers together
330 (module $actor-scheduler
331 (type $func (func)) ;; [] -> []
332 (type $cont (cont $func)) ;; cont ([] -> [])
333
334 (type $cont-func (func (param (ref $cont)))) ;; [cont ([] -> []) -> []]
335 (type $cont-cont (cont $cont-func)) ;; cont ([cont ([] -> []) -> []])
336
337 (elem declare func $act $scheduler)
338
339 (func $act (import "actor-as-lwt" "act") (param $k (ref $cont)))
340 (func $scheduler (import "scheduler" "run") (param $k (ref $cont)))
341
342 (func $run-actor (export "run-actor") (param $k (ref $cont))
343 (call $scheduler (cont.bind $cont-cont $cont (local.get $k) (cont.new $cont-cont (ref.func $act))))
344 )
345 )
346 (register "actor-scheduler")
347
348 (module
349 (type $func (func)) ;; [] -> []
350 (type $cont (cont $func)) ;; cont ([] -> [])
351
352 (type $i-func (func (param i32))) ;; [i32] -> []
353 (type $i-cont (cont $i-func)) ;; cont ([i32] -> [])
354
355 (elem declare func $chain)
356
357 (func $run-actor (import "actor-scheduler" "run-actor") (param $k (ref $cont)))
358 (func $chain (import "chain" "chain") (param $n i32))
359
360 (func $run-chain (export "run-chain") (param $n i32)
361 (call $run-actor (cont.bind $i-cont $cont (local.get $n) (cont.new $i-cont (ref.func $chain))))
362 )
363 )
364
365 (invoke "run-chain" (i32.const 64))