Debian Codegraph @

tests/cli/stack-switching/lwt.wast

raw
1 ;; RUN: wast --assert default --snapshot tests/snapshots % -f stack-switching
2
3 ;; dynamic lightweight threads
4
5 ;; interface to lightweight threads
6 (module $lwt
7 (type $func (func)) ;; [] -> []
8 (type $cont (cont $func)) ;; cont ([] -> [])
9
10 (tag $yield (export "yield")) ;; [] -> []
11 (tag $fork (export "fork") (param (ref $cont))) ;; [cont ([] -> [])] -> []
12 )
13 (register "lwt")
14
15 (module $example
16 (type $func (func)) ;; [] -> []
17 (type $cont (cont $func)) ;; cont ([] -> [])
18
19 (tag $yield (import "lwt" "yield")) ;; [] -> []
20 (tag $fork (import "lwt" "fork") (param (ref $cont))) ;; [cont ([] -> [])] -> []
21
22 (func $log (import "spectest" "print_i32") (param i32))
23
24 (elem declare func $thread1 $thread2 $thread3)
25
26 (func $main (export "main")
27 (call $log (i32.const 0))
28 (suspend $fork (cont.new $cont (ref.func $thread1)))
29 (call $log (i32.const 1))
30 (suspend $fork (cont.new $cont (ref.func $thread2)))
31 (call $log (i32.const 2))
32 (suspend $fork (cont.new $cont (ref.func $thread3)))
33 (call $log (i32.const 3))
34 )
35
36 (func $thread1
37 (call $log (i32.const 10))
38 (suspend $yield)
39 (call $log (i32.const 11))
40 (suspend $yield)
41 (call $log (i32.const 12))
42 )
43
44 (func $thread2
45 (call $log (i32.const 20))
46 (suspend $yield)
47 (call $log (i32.const 21))
48 (suspend $yield)
49 (call $log (i32.const 22))
50 )
51
52 (func $thread3
53 (call $log (i32.const 30))
54 (suspend $yield)
55 (call $log (i32.const 31))
56 (suspend $yield)
57 (call $log (i32.const 32))
58 )
59 )
60 (register "example")
61
62 ;; queue of threads
63 (module $queue
64 (type $func (func)) ;; [] -> []
65 (type $cont (cont $func)) ;; cont ([] -> [])
66
67 ;; Table as simple queue (keeping it simple, no ring buffer)
68 (table $queue 0 (ref null $cont))
69 (global $qdelta i32 (i32.const 10))
70 (global $qback (mut i32) (i32.const 0))
71 (global $qfront (mut i32) (i32.const 0))
72
73 (func $queue-empty (export "queue-empty") (result i32)
74 (i32.eq (global.get $qfront) (global.get $qback))
75 )
76
77 (func $dequeue (export "dequeue") (result (ref null $cont))
78 (local $i i32)
79 (if (call $queue-empty)
80 (then (return (ref.null $cont)))
81 )
82 (local.set $i (global.get $qfront))
83 (global.set $qfront (i32.add (local.get $i) (i32.const 1)))
84 (table.get $queue (local.get $i))
85 )
86
87 (func $enqueue (export "enqueue") (param $k (ref null $cont))
88 ;; Check if queue is full
89 (if (i32.eq (global.get $qback) (table.size $queue))
90 (then
91 ;; Check if there is enough space in the front to compact
92 (if (i32.lt_u (global.get $qfront) (global.get $qdelta))
93 (then
94 ;; Space is below threshold, grow table instead
95 (drop (table.grow $queue (ref.null $cont) (global.get $qdelta)))
96 )
97 (else
98 ;; Enough space, move entries up to head of table
99 (global.set $qback (i32.sub (global.get $qback) (global.get $qfront)))
100 (table.copy $queue $queue
101 (i32.const 0) ;; dest = new front = 0
102 (global.get $qfront) ;; src = old front
103 (global.get $qback) ;; len = new back = old back - old front
104 )
105 (table.fill $queue ;; null out old entries to avoid leaks
106 (global.get $qback) ;; start = new back
107 (ref.null $cont) ;; init value
108 (global.get $qfront) ;; len = old front = old front - new front
109 )
110 (global.set $qfront (i32.const 0))
111 )
112 )
113 )
114 )
115 (table.set $queue (global.get $qback) (local.get $k))
116 (global.set $qback (i32.add (global.get $qback) (i32.const 1)))
117 )
118 )
119 (register "queue")
120
121 (module $scheduler
122 (type $func (func)) ;; [] -> []
123 (type $cont (cont $func)) ;; cont ([] -> [])
124
125 (tag $yield (import "lwt" "yield")) ;; [] -> []
126 (tag $fork (import "lwt" "fork") (param (ref $cont))) ;; [cont ([] -> [])] -> []
127
128 (func $queue-empty (import "queue" "queue-empty") (result i32))
129 (func $dequeue (import "queue" "dequeue") (result (ref null $cont)))
130 (func $enqueue (import "queue" "enqueue") (param $k (ref null $cont)))
131
132 ;; synchronous scheduler (run current thread to completion without
133 ;; yielding)
134 (func $sync (export "sync") (param $nextk (ref null $cont))
135 (loop $l
136 (if (ref.is_null (local.get $nextk)) (then (return)))
137 (block $on_yield (result (ref $cont))
138 (block $on_fork (result (ref $cont) (ref $cont))
139 (resume $cont
140 (on $yield $on_yield)
141 (on $fork $on_fork)
142 (local.get $nextk)
143 )
144 (local.set $nextk (call $dequeue))
145 (br $l) ;; thread terminated
146 ) ;; $on_fork (result (ref $cont) (ref $cont))
147 (local.set $nextk) ;; current thread
148 (call $enqueue) ;; new thread
149 (br $l)
150 ) ;; $on_yield (result (ref $cont))
151 (local.set $nextk) ;; carry on with current thread
152 (br $l)
153 )
154 )
155
156 ;; four asynchronous schedulers:
157 ;; * kt and tk don't yield on encountering a fork
158 ;; 1) kt runs the continuation, queuing up the new thread for later
159 ;; 2) tk runs the new thread first, queuing up the continuation for later
160 ;; * ykt and ytk do yield on encountering a fork
161 ;; 3) ykt runs the continuation, queuing up the new thread for later
162 ;; 4) ytk runs the new thread first, queuing up the continuation for later
163
164 ;; no yield on fork, continuation first
165 (func $kt (export "kt") (param $nextk (ref null $cont))
166 (loop $l
167 (if (ref.is_null (local.get $nextk)) (then (return)))
168 (block $on_yield (result (ref $cont))
169 (block $on_fork (result (ref $cont) (ref $cont))
170 (resume $cont
171 (on $yield $on_yield)
172 (on $fork $on_fork)
173 (local.get $nextk)
174 )
175 (local.set $nextk (call $dequeue))
176 (br $l) ;; thread terminated
177 ) ;; $on_fork (result (ref $cont) (ref $cont))
178 (local.set $nextk) ;; current thread
179 (call $enqueue) ;; new thread
180 (br $l)
181 ) ;; $on_yield (result (ref $cont))
182 (call $enqueue) ;; current thread
183 (local.set $nextk (call $dequeue)) ;; next thread
184 (br $l)
185 )
186 )
187
188 ;; no yield on fork, new thread first
189 (func $tk (export "tk") (param $nextk (ref null $cont))
190 (loop $l
191 (if (ref.is_null (local.get $nextk)) (then (return)))
192 (block $on_yield (result (ref $cont))
193 (block $on_fork (result (ref $cont) (ref $cont))
194 (resume $cont
195 (on $yield $on_yield)
196 (on $fork $on_fork)
197 (local.get $nextk)
198 )
199 (local.set $nextk (call $dequeue))
200 (br $l) ;; thread terminated
201 ) ;; $on_fork (result (ref $cont) (ref $cont))
202 (call $enqueue) ;; current thread
203 (local.set $nextk) ;; new thread
204 (br $l)
205 ) ;; $on_yield (result (ref $cont))
206 (call $enqueue) ;; current thread
207 (local.set $nextk (call $dequeue)) ;; next thread
208 (br $l)
209 )
210 )
211
212 ;; yield on fork, continuation first
213 (func $ykt (export "ykt") (param $nextk (ref null $cont))
214 (loop $l
215 (if (ref.is_null (local.get $nextk)) (then (return)))
216 (block $on_yield (result (ref $cont))
217 (block $on_fork (result (ref $cont) (ref $cont))
218 (resume $cont
219 (on $yield $on_yield)
220 (on $fork $on_fork)
221 (local.get $nextk)
222 )
223 (local.set $nextk (call $dequeue))
224 (br $l) ;; thread terminated
225 ) ;; $on_fork (result (ref $cont) (ref $cont))
226 (call $enqueue) ;; current thread
227 (call $enqueue) ;; new thread
228 (local.set $nextk (call $dequeue)) ;; next thread
229 (br $l)
230 ) ;; $on_yield (result (ref $cont))
231 (call $enqueue) ;; current thread
232 (local.set $nextk (call $dequeue)) ;; next thread
233 (br $l)
234 )
235 )
236
237 ;; yield on fork, new thread first
238 (func $ytk (export "ytk") (param $nextk (ref null $cont))
239 (loop $l
240 (if (ref.is_null (local.get $nextk)) (then (return)))
241 (block $on_yield (result (ref $cont))
242 (block $on_fork (result (ref $cont) (ref $cont))
243 (resume $cont
244 (on $yield $on_yield)
245 (on $fork $on_fork)
246 (local.get $nextk)
247 )
248 (local.set $nextk (call $dequeue))
249 (br $l) ;; thread terminated
250 ) ;; $on_fork (result (ref $cont) (ref $cont))
251 (local.set $nextk)
252 (call $enqueue) ;; new thread
253 (call $enqueue (local.get $nextk)) ;; current thread
254 (local.set $nextk (call $dequeue)) ;; next thread
255 (br $l)
256 ) ;; $on_yield (result (ref $cont))
257 (call $enqueue) ;; current thread
258 (local.set $nextk (call $dequeue)) ;; next thread
259 (br $l)
260 )
261 )
262 )
263 (register "scheduler")
264
265 (module
266 (type $func (func)) ;; [] -> []
267 (type $cont (cont $func)) ;; cont ([] -> [])
268
269 (func $scheduler-sync (import "scheduler" "sync") (param $nextk (ref null $cont)))
270 (func $scheduler-kt (import "scheduler" "kt") (param $nextk (ref null $cont)))
271 (func $schedule-tk (import "scheduler" "tk") (param $nextk (ref null $cont)))
272 (func $scheduler-ykt (import "scheduler" "ykt") (param $nextk (ref null $cont)))
273 (func $scheduler-ytk (import "scheduler" "ytk") (param $nextk (ref null $cont)))
274
275 (func $log (import "spectest" "print_i32") (param i32))
276
277 (func $main (import "example" "main"))
278
279 (elem declare func $main)
280
281 (func (export "run")
282 (call $log (i32.const -1))
283 (call $scheduler-sync (cont.new $cont (ref.func $main)))
284 (call $log (i32.const -2))
285 (call $scheduler-kt (cont.new $cont (ref.func $main)))
286 (call $log (i32.const -3))
287 (call $schedule-tk (cont.new $cont (ref.func $main)))
288 (call $log (i32.const -4))
289 (call $scheduler-ykt (cont.new $cont (ref.func $main)))
290 (call $log (i32.const -5))
291 (call $scheduler-ytk (cont.new $cont (ref.func $main)))
292 (call $log (i32.const -6))
293 )
294 )
295
296 (invoke "run")