Debian Codegraph @

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

raw
1 ;; RUN: wast --assert default --snapshot tests/snapshots % -f stack-switching
2
3 ;; static lightweight threads
4
5 ;; interface to a fixed collection of lightweight threads
6 (module $lwt
7 (tag $yield (export "yield"))
8 )
9 (register "lwt")
10
11 (module $example
12 (tag $yield (import "lwt" "yield"))
13 (func $log (import "spectest" "print_i32") (param i32))
14
15 (func $thread1 (export "thread1")
16 (call $log (i32.const 10))
17 (suspend $yield)
18 (call $log (i32.const 11))
19 (suspend $yield)
20 (call $log (i32.const 12))
21 )
22
23 (func $thread2 (export "thread2")
24 (call $log (i32.const 20))
25 (suspend $yield)
26 (call $log (i32.const 21))
27 (suspend $yield)
28 (call $log (i32.const 22))
29 )
30
31 (func $thread3 (export "thread3")
32 (call $log (i32.const 30))
33 (suspend $yield)
34 (call $log (i32.const 31))
35 (suspend $yield)
36 (call $log (i32.const 32))
37 )
38 )
39 (register "example")
40
41 ;; queue of threads
42 (module $queue
43 (type $func (func))
44 (type $cont (cont $func))
45
46 ;; Table as simple queue (keeping it simple, no ring buffer)
47 (table $queue 0 (ref null $cont))
48 (global $qdelta i32 (i32.const 10))
49 (global $qback (mut i32) (i32.const 0))
50 (global $qfront (mut i32) (i32.const 0))
51
52 (func $queue-empty (export "queue-empty") (result i32)
53 (i32.eq (global.get $qfront) (global.get $qback))
54 )
55
56 (func $dequeue (export "dequeue") (result (ref null $cont))
57 (local $i i32)
58 (if (call $queue-empty)
59 (then (return (ref.null $cont)))
60 )
61 (local.set $i (global.get $qfront))
62 (global.set $qfront (i32.add (local.get $i) (i32.const 1)))
63 (table.get $queue (local.get $i))
64 )
65
66 (func $enqueue (export "enqueue") (param $k (ref $cont))
67 ;; Check if queue is full
68 (if (i32.eq (global.get $qback) (table.size $queue))
69 (then
70 ;; Check if there is enough space in the front to compact
71 (if (i32.lt_u (global.get $qfront) (global.get $qdelta))
72 (then
73 ;; Space is below threshold, grow table instead
74 (drop (table.grow $queue (ref.null $cont) (global.get $qdelta)))
75 )
76 (else
77 ;; Enough space, move entries up to head of table
78 (global.set $qback (i32.sub (global.get $qback) (global.get $qfront)))
79 (table.copy $queue $queue
80 (i32.const 0) ;; dest = new front = 0
81 (global.get $qfront) ;; src = old front
82 (global.get $qback) ;; len = new back = old back - old front
83 )
84 (table.fill $queue ;; null out old entries to avoid leaks
85 (global.get $qback) ;; start = new back
86 (ref.null $cont) ;; init value
87 (global.get $qfront) ;; len = old front = old front - new front
88 )
89 (global.set $qfront (i32.const 0))
90 )
91 )
92 )
93 )
94 (table.set $queue (global.get $qback) (local.get $k))
95 (global.set $qback (i32.add (global.get $qback) (i32.const 1)))
96 )
97 )
98 (register "queue")
99
100 (module $scheduler
101 (type $func (func))
102 (type $cont (cont $func))
103
104 (tag $yield (import "lwt" "yield"))
105
106 ;; queue interface
107 (func $queue-empty (import "queue" "queue-empty") (result i32))
108 (func $dequeue (import "queue" "dequeue") (result (ref null $cont)))
109 (func $enqueue (import "queue" "enqueue") (param $k (ref $cont)))
110
111 (func $run (export "run")
112 (loop $l
113 (if (call $queue-empty) (then (return)))
114 (block $on_yield (result (ref $cont))
115 (resume $cont (on $yield $on_yield)
116 (call $dequeue)
117 )
118 (br $l) ;; thread terminated
119 ) ;; $on_yield (result (ref $cont))
120 (call $enqueue) ;; continuation of current thread
121 (br $l)
122 )
123 )
124 )
125 (register "scheduler")
126
127 (module
128 (type $func (func))
129 (type $cont (cont $func))
130
131 (func $scheduler (import "scheduler" "run"))
132 (func $enqueue (import "queue" "enqueue") (param (ref $cont)))
133
134 (func $log (import "spectest" "print_i32") (param i32))
135
136 (func $thread1 (import "example" "thread1"))
137 (func $thread2 (import "example" "thread2"))
138 (func $thread3 (import "example" "thread3"))
139
140 (elem declare func $thread1 $thread2 $thread3)
141
142 (func (export "run")
143 (call $enqueue (cont.new $cont (ref.func $thread1)))
144 (call $enqueue (cont.new $cont (ref.func $thread2)))
145 (call $enqueue (cont.new $cont (ref.func $thread3)))
146
147 (call $log (i32.const -1))
148 (call $scheduler)
149 (call $log (i32.const -2))
150 )
151 )
152
153 (invoke "run")