| 1 |
(module $scheduler |
| 2 |
(type $proc (;0;) (func)) |
| 3 |
(type $cont (;1;) (cont $proc)) |
| 4 |
(type (;2;) (func (param (ref $cont)))) |
| 5 |
(type (;3;) (func (result i32))) |
| 6 |
(type (;4;) (func (result (ref null $cont)))) |
| 7 |
(type (;5;) (func (result (ref $cont) (ref $cont)))) |
| 8 |
(table $queue (;0;) 0 (ref null $cont)) |
| 9 |
(tag $yield (;0;) (type $proc)) |
| 10 |
(tag $spawn (;1;) (type 2) (param (ref $cont))) |
| 11 |
(global $qdelta (;0;) i32 i32.const 10) |
| 12 |
(global $qback (;1;) (mut i32) i32.const 0) |
| 13 |
(global $qfront (;2;) (mut i32) i32.const 0) |
| 14 |
(export "yield" (tag 0)) |
| 15 |
(export "spawn" (tag 1)) |
| 16 |
(export "scheduler" (func $scheduler)) |
| 17 |
(func $queue-empty (;0;) (type 3) (result i32) |
| 18 |
global.get $qfront |
| 19 |
global.get $qback |
| 20 |
i32.eq |
| 21 |
) |
| 22 |
(func $dequeue (;1;) (type 4) (result (ref null $cont)) |
| 23 |
(local $i i32) |
| 24 |
call $queue-empty |
| 25 |
if ;; label = @1 |
| 26 |
ref.null $cont |
| 27 |
return |
| 28 |
end |
| 29 |
global.get $qfront |
| 30 |
local.set $i |
| 31 |
local.get $i |
| 32 |
i32.const 1 |
| 33 |
i32.add |
| 34 |
global.set $qfront |
| 35 |
local.get $i |
| 36 |
table.get $queue |
| 37 |
) |
| 38 |
(func $enqueue (;2;) (type 2) (param $k (ref $cont)) |
| 39 |
global.get $qback |
| 40 |
table.size $queue |
| 41 |
i32.eq |
| 42 |
if ;; label = @1 |
| 43 |
global.get $qfront |
| 44 |
global.get $qdelta |
| 45 |
i32.lt_u |
| 46 |
if ;; label = @2 |
| 47 |
ref.null $cont |
| 48 |
global.get $qdelta |
| 49 |
table.grow $queue |
| 50 |
drop |
| 51 |
else |
| 52 |
global.get $qback |
| 53 |
global.get $qfront |
| 54 |
i32.sub |
| 55 |
global.set $qback |
| 56 |
i32.const 0 |
| 57 |
global.get $qfront |
| 58 |
global.get $qback |
| 59 |
table.copy |
| 60 |
global.get $qback |
| 61 |
ref.null $cont |
| 62 |
global.get $qfront |
| 63 |
table.fill $queue |
| 64 |
i32.const 0 |
| 65 |
global.set $qfront |
| 66 |
end |
| 67 |
end |
| 68 |
global.get $qback |
| 69 |
local.get $k |
| 70 |
table.set $queue |
| 71 |
global.get $qback |
| 72 |
i32.const 1 |
| 73 |
i32.add |
| 74 |
global.set $qback |
| 75 |
) |
| 76 |
(func $scheduler (;3;) (type 2) (param $main (ref $cont)) |
| 77 |
local.get $main |
| 78 |
call $enqueue |
| 79 |
loop $l |
| 80 |
call $queue-empty |
| 81 |
if ;; label = @2 |
| 82 |
return |
| 83 |
end |
| 84 |
block $on_yield (result (ref $cont)) |
| 85 |
block $on_spawn (type 5) (result (ref $cont) (ref $cont)) |
| 86 |
call $dequeue |
| 87 |
resume $cont (on $yield $on_yield) (on $spawn $on_spawn) |
| 88 |
br $l |
| 89 |
end |
| 90 |
call $enqueue |
| 91 |
call $enqueue |
| 92 |
br $l |
| 93 |
end |
| 94 |
call $enqueue |
| 95 |
br $l |
| 96 |
end |
| 97 |
) |
| 98 |
) |