| 1 |
;; RUN: wast --assert default --snapshot tests/snapshots % -f stack-switching |
| 2 |
|
| 3 |
;; functional 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 |
(module $queue |
| 63 |
(type $func (func)) ;; [] -> [] |
| 64 |
(type $cont (cont $func)) ;; cont ([] -> []) |
| 65 |
|
| 66 |
;; Table as simple queue (keeping it simple, no ring buffer) |
| 67 |
(table $queue 0 (ref null $cont)) |
| 68 |
(global $qdelta i32 (i32.const 10)) |
| 69 |
(global $qback (mut i32) (i32.const 0)) |
| 70 |
(global $qfront (mut i32) (i32.const 0)) |
| 71 |
|
| 72 |
(func $queue-empty (export "queue-empty") (result i32) |
| 73 |
(i32.eq (global.get $qfront) (global.get $qback)) |
| 74 |
) |
| 75 |
|
| 76 |
(func $dequeue (export "dequeue") (result (ref null $cont)) |
| 77 |
(local $i i32) |
| 78 |
(if (call $queue-empty) |
| 79 |
(then (return (ref.null $cont))) |
| 80 |
) |
| 81 |
(local.set $i (global.get $qfront)) |
| 82 |
(global.set $qfront (i32.add (local.get $i) (i32.const 1))) |
| 83 |
(table.get $queue (local.get $i)) |
| 84 |
) |
| 85 |
|
| 86 |
(func $enqueue (export "enqueue") (param $k (ref $cont)) |
| 87 |
;; Check if queue is full |
| 88 |
(if (i32.eq (global.get $qback) (table.size $queue)) |
| 89 |
(then |
| 90 |
;; Check if there is enough space in the front to compact |
| 91 |
(if (i32.lt_u (global.get $qfront) (global.get $qdelta)) |
| 92 |
(then |
| 93 |
;; Space is below threshold, grow table instead |
| 94 |
(drop (table.grow $queue (ref.null $cont) (global.get $qdelta))) |
| 95 |
) |
| 96 |
(else |
| 97 |
;; Enough space, move entries up to head of table |
| 98 |
(global.set $qback (i32.sub (global.get $qback) (global.get $qfront))) |
| 99 |
(table.copy $queue $queue |
| 100 |
(i32.const 0) ;; dest = new front = 0 |
| 101 |
(global.get $qfront) ;; src = old front |
| 102 |
(global.get $qback) ;; len = new back = old back - old front |
| 103 |
) |
| 104 |
(table.fill $queue ;; null out old entries to avoid leaks |
| 105 |
(global.get $qback) ;; start = new back |
| 106 |
(ref.null $cont) ;; init value |
| 107 |
(global.get $qfront) ;; len = old front = old front - new front |
| 108 |
) |
| 109 |
(global.set $qfront (i32.const 0)) |
| 110 |
) |
| 111 |
) |
| 112 |
) |
| 113 |
) |
| 114 |
(table.set $queue (global.get $qback) (local.get $k)) |
| 115 |
(global.set $qback (i32.add (global.get $qback) (i32.const 1))) |
| 116 |
) |
| 117 |
) |
| 118 |
(register "queue") |
| 119 |
|
| 120 |
(module $scheduler |
| 121 |
(type $func (func)) ;; [] -> [] |
| 122 |
(type $cont (cont $func)) ;; cont ([] -> []) |
| 123 |
|
| 124 |
(tag $yield (import "lwt" "yield")) ;; [] -> [] |
| 125 |
(tag $fork (import "lwt" "fork") (param (ref $cont))) ;; [cont ([] -> [])] -> [] |
| 126 |
|
| 127 |
(func $queue-empty (import "queue" "queue-empty") (result i32)) |
| 128 |
(func $dequeue (import "queue" "dequeue") (result (ref null $cont))) |
| 129 |
(func $enqueue (import "queue" "enqueue") (param $k (ref $cont))) |
| 130 |
|
| 131 |
;; synchronous scheduler (run current thread to completion without |
| 132 |
;; yielding) |
| 133 |
(func $sync (export "sync") (param $nextk (ref null $cont)) |
| 134 |
(if (ref.is_null (local.get $nextk)) (then (return))) |
| 135 |
(block $on_yield (result (ref $cont)) |
| 136 |
(block $on_fork (result (ref $cont) (ref $cont)) |
| 137 |
(resume $cont |
| 138 |
(on $yield $on_yield) |
| 139 |
(on $fork $on_fork) |
| 140 |
(local.get $nextk) |
| 141 |
) |
| 142 |
(return_call $sync (call $dequeue)) |
| 143 |
) ;; $on_fork (result (ref $func) (ref $cont)) |
| 144 |
(local.set $nextk) |
| 145 |
(call $enqueue) |
| 146 |
(return_call $sync (local.get $nextk)) |
| 147 |
) ;; $on_yield (result (ref $cont)) |
| 148 |
(return_call $sync) |
| 149 |
) |
| 150 |
|
| 151 |
;; four different schedulers: |
| 152 |
;; * kt and tk don't yield on encountering a fork |
| 153 |
;; 1) kt runs the continuation, queuing up the new thread for later |
| 154 |
;; 2) tk runs the new thread first, queuing up the continuation for later |
| 155 |
;; * ykt and ytk do yield on encountering a fork |
| 156 |
;; 3) ykt runs the continuation, queuing up the new thread for later |
| 157 |
;; 4) ytk runs the new thread first, queuing up the continuation for later |
| 158 |
|
| 159 |
;; no yield on fork, continuation first |
| 160 |
(func $kt (export "kt") (param $nextk (ref null $cont)) |
| 161 |
(if (ref.is_null (local.get $nextk)) (then (return))) |
| 162 |
(block $on_yield (result (ref $cont)) |
| 163 |
(block $on_fork (result (ref $cont) (ref $cont)) |
| 164 |
(resume $cont |
| 165 |
(on $yield $on_yield) |
| 166 |
(on $fork $on_fork) |
| 167 |
(local.get $nextk) |
| 168 |
) |
| 169 |
(return_call $tk (call $dequeue)) |
| 170 |
) ;; $on_fork (result (ref $func) (ref $cont)) |
| 171 |
(local.set $nextk) |
| 172 |
(call $enqueue) |
| 173 |
(return_call $tk (local.get $nextk)) |
| 174 |
) ;; $on_yield (result (ref $cont)) |
| 175 |
(call $enqueue) |
| 176 |
(return_call $tk (call $dequeue)) |
| 177 |
) |
| 178 |
|
| 179 |
;; no yield on fork, new thread first |
| 180 |
(func $tk (export "tk") (param $nextk (ref null $cont)) |
| 181 |
(if (ref.is_null (local.get $nextk)) (then (return))) |
| 182 |
(block $on_yield (result (ref $cont)) |
| 183 |
(block $on_fork (result (ref $cont) (ref $cont)) |
| 184 |
(resume $cont |
| 185 |
(on $yield $on_yield) |
| 186 |
(on $fork $on_fork) |
| 187 |
(local.get $nextk)) |
| 188 |
(return_call $kt (call $dequeue)) |
| 189 |
) ;; $on_fork (result (ref $cont) (ref $cont)) |
| 190 |
(return_call $kt (call $enqueue)) |
| 191 |
) ;; $on_yield (result (ref $cont)) |
| 192 |
(call $enqueue) |
| 193 |
(return_call $kt (call $dequeue)) |
| 194 |
) |
| 195 |
|
| 196 |
;; yield on fork, continuation first |
| 197 |
(func $ykt (export "ykt") (param $nextk (ref null $cont)) |
| 198 |
(if (ref.is_null (local.get $nextk)) (then (return))) |
| 199 |
(block $on_yield (result (ref $cont)) |
| 200 |
(block $on_fork (result (ref $cont) (ref $cont)) |
| 201 |
(resume $cont |
| 202 |
(on $yield $on_yield) |
| 203 |
(on $fork $on_fork) |
| 204 |
(local.get $nextk) |
| 205 |
) |
| 206 |
(return_call $ykt (call $dequeue)) |
| 207 |
) ;; $on_fork (result (ref $cont) (ref $cont)) |
| 208 |
(call $enqueue) |
| 209 |
(call $enqueue) |
| 210 |
(return_call $ykt (call $dequeue)) |
| 211 |
) ;; $on_yield (result (ref $cont)) |
| 212 |
(call $enqueue) |
| 213 |
(return_call $ykt (call $dequeue)) |
| 214 |
) |
| 215 |
|
| 216 |
;; yield on fork, new thread first |
| 217 |
(func $ytk (export "ytk") (param $nextk (ref null $cont)) |
| 218 |
(local $k (ref $cont)) |
| 219 |
(if (ref.is_null (local.get $nextk)) (then (return))) |
| 220 |
(block $on_yield (result (ref $cont)) |
| 221 |
(block $on_fork (result (ref $cont) (ref $cont)) |
| 222 |
(resume $cont (on $yield $on_yield) (on $fork $on_fork) (local.get $nextk)) |
| 223 |
(return_call $ytk (call $dequeue)) |
| 224 |
) ;; $on_fork (result (ref $cont) (ref $cont)) |
| 225 |
(local.set $k) |
| 226 |
(call $enqueue) |
| 227 |
(call $enqueue (local.get $k)) |
| 228 |
(return_call $ytk (call $dequeue)) |
| 229 |
) ;; $on_yield (result (ref $cont)) |
| 230 |
(call $enqueue) |
| 231 |
(return_call $ytk (call $dequeue)) |
| 232 |
) |
| 233 |
) |
| 234 |
|
| 235 |
(register "scheduler") |
| 236 |
|
| 237 |
(module |
| 238 |
(type $func (func)) ;; [] -> [] |
| 239 |
(type $cont (cont $func)) ;; cont ([] -> []) |
| 240 |
|
| 241 |
(func $scheduler-sync (import "scheduler" "sync") (param $nextk (ref null $cont))) |
| 242 |
(func $scheduler-kt (import "scheduler" "kt") (param $nextk (ref null $cont))) |
| 243 |
(func $schedule-tk (import "scheduler" "tk") (param $nextk (ref null $cont))) |
| 244 |
(func $scheduler-ykt (import "scheduler" "ykt") (param $nextk (ref null $cont))) |
| 245 |
(func $scheduler-ytk (import "scheduler" "ytk") (param $nextk (ref null $cont))) |
| 246 |
|
| 247 |
(func $log (import "spectest" "print_i32") (param i32)) |
| 248 |
|
| 249 |
(func $main (import "example" "main")) |
| 250 |
|
| 251 |
(elem declare func $main) |
| 252 |
|
| 253 |
(func (export "run") |
| 254 |
(call $log (i32.const -1)) |
| 255 |
(call $scheduler-sync (cont.new $cont (ref.func $main))) |
| 256 |
(call $log (i32.const -2)) |
| 257 |
(call $scheduler-kt (cont.new $cont (ref.func $main))) |
| 258 |
(call $log (i32.const -3)) |
| 259 |
(call $schedule-tk (cont.new $cont (ref.func $main))) |
| 260 |
(call $log (i32.const -4)) |
| 261 |
(call $scheduler-ykt (cont.new $cont (ref.func $main))) |
| 262 |
(call $log (i32.const -5)) |
| 263 |
(call $scheduler-ytk (cont.new $cont (ref.func $main))) |
| 264 |
(call $log (i32.const -6)) |
| 265 |
) |
| 266 |
) |
| 267 |
|
| 268 |
(invoke "run") |
| 269 |
|