|
| 1 | +;; This test has two components $C and $D, where $D imports and calls $C |
| 2 | +;; $C exports two functions: 'blocker' and 'unblocker' |
| 3 | +;; 'blocker' blocks on an empty waitable set |
| 4 | +;; 'unblocker' wakes blocker by adding a resolved future to blocker's waitable set |
| 5 | +;; $D calls 'blocker' then 'unblocker', then waits for 'blocker' to finish |
| 6 | +(component |
| 7 | + (component $C |
| 8 | + (core module $Memory (memory (export "mem") 1)) |
| 9 | + (core instance $memory (instantiate $Memory)) |
| 10 | + (core module $CM |
| 11 | + (import "" "mem" (memory 1)) |
| 12 | + (import "" "task.return" (func $task.return (param i32))) |
| 13 | + (import "" "waitable.join" (func $waitable.join (param i32 i32))) |
| 14 | + (import "" "waitable-set.new" (func $waitable-set.new (result i32))) |
| 15 | + (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32))) |
| 16 | + (import "" "future.new" (func $future.new (result i64))) |
| 17 | + (import "" "future.read" (func $future.read (param i32 i32) (result i32))) |
| 18 | + (import "" "future.write" (func $future.write (param i32 i32) (result i32))) |
| 19 | + (import "" "future.close-readable" (func $future.close-readable (param i32))) |
| 20 | + (import "" "future.close-writable" (func $future.close-writable (param i32))) |
| 21 | + |
| 22 | + ;; $ws is waited on by 'blocker' and added to by 'unblocker' |
| 23 | + (global $ws (mut i32) (i32.const 0)) |
| 24 | + (func $start (global.set $ws (call $waitable-set.new))) |
| 25 | + (start $start) |
| 26 | + |
| 27 | + ;; 'unblocker' initializes $futr with the readable end of a resolved future |
| 28 | + (global $futr (mut i32) (i32.const 0)) |
| 29 | + |
| 30 | + (func $blocker (export "blocker") (result i32) |
| 31 | + ;; wait on $ws which is currently empty; 'unblocker' will wake us up |
| 32 | + (i32.or (i32.const 2 (; WAIT ;)) (i32.shl (global.get $ws) (i32.const 4))) |
| 33 | + ) |
| 34 | + (func $blocker_cb (export "blocker_cb") (param $event_code i32) (param $index i32) (param $payload i32) (result i32) |
| 35 | + ;; assert that we were in fact woken by 'unblocker' adding $futr to $ws |
| 36 | + (if (i32.ne (i32.const 4 (; FUTURE_READ ;)) (local.get $event_code)) |
| 37 | + (then unreachable)) |
| 38 | + (if (i32.ne (global.get $futr) (local.get $index)) |
| 39 | + (then unreachable)) |
| 40 | + (if (i32.ne (i32.const 0x10) (local.get $payload)) ;; TODO: 0x11? (b/c CLOSED) |
| 41 | + (then unreachable)) |
| 42 | + |
| 43 | + (call $future.close-readable (global.get $futr)) |
| 44 | + |
| 45 | + ;; return 42 to $D.run |
| 46 | + (call $task.return (i32.const 42)) |
| 47 | + (i32.const 0) |
| 48 | + ) |
| 49 | + |
| 50 | + (func $unblocker (export "unblocker") (result i32) |
| 51 | + (local $ret64 i64) (local $ret i32) (local $futw i32) |
| 52 | + |
| 53 | + ;; create a future that will be used to unblock 'blocker', storing r/w ends in $futr/$futw |
| 54 | + (local.set $ret64 (call $future.new)) |
| 55 | + (global.set $futr (i32.wrap_i64 (local.get $ret64))) |
| 56 | + (if (i32.ne (i32.const 3) (global.get $futr)) ;; TODO: 2? |
| 57 | + (then unreachable)) |
| 58 | + (local.set $futw (i32.wrap_i64 (i64.shr_u (local.get $ret64) (i64.const 32)))) |
| 59 | + (if (i32.ne (i32.const 2) (local.get $futw)) ;; TODO: 3? |
| 60 | + (then unreachable)) |
| 61 | + |
| 62 | + ;; perform a future.read which will block, and add this future to the waitable-set |
| 63 | + ;; being waited on by 'blocker' |
| 64 | + (local.set $ret (call $future.read (global.get $futr) (i32.const 0xdeadbeef))) |
| 65 | + (if (i32.ne (i32.const -1 (; BLOCKED ;)) (local.get $ret)) |
| 66 | + (then unreachable)) |
| 67 | + (call $waitable.join (global.get $futr) (global.get $ws)) |
| 68 | + |
| 69 | + ;; perform a future.write which will rendezvous with the write and complete |
| 70 | + (local.set $ret (call $future.write (local.get $futw) (i32.const 0xdeadbeef))) |
| 71 | + (if (i32.ne (i32.const 0x10) (local.get $ret)) ;; TODO: 0x11? (b/c CLOSED) |
| 72 | + (then unreachable)) |
| 73 | + |
| 74 | + (call $future.close-writable (local.get $futw)) |
| 75 | + |
| 76 | + ;; return 43 to $D.run |
| 77 | + (call $task.return (i32.const 43)) |
| 78 | + (i32.const 0) |
| 79 | + ) |
| 80 | + (func $unblocker_cb (export "unblocker_cb") (param i32 i32 i32) (result i32) |
| 81 | + ;; 'unblocker' doesn't block |
| 82 | + unreachable |
| 83 | + ) |
| 84 | + ) |
| 85 | + (type $FT (future)) |
| 86 | + (canon task.return (result u32) (core func $task.return)) |
| 87 | + (canon waitable.join (core func $waitable.join)) |
| 88 | + (canon waitable-set.new (core func $waitable-set.new)) |
| 89 | + (canon waitable-set.wait (memory $memory "mem") (core func $waitable-set.wait)) |
| 90 | + (canon future.new $FT (core func $future.new)) |
| 91 | + (canon future.read $FT async (memory $memory "mem") (core func $future.read)) |
| 92 | + (canon future.write $FT async (memory $memory "mem") (core func $future.write)) |
| 93 | + (canon future.close-readable $FT (core func $future.close-readable)) |
| 94 | + (canon future.close-writable $FT (core func $future.close-writable)) |
| 95 | + (core instance $cm (instantiate $CM (with "" (instance |
| 96 | + (export "mem" (memory $memory "mem")) |
| 97 | + (export "task.return" (func $task.return)) |
| 98 | + (export "waitable.join" (func $waitable.join)) |
| 99 | + (export "waitable-set.new" (func $waitable-set.new)) |
| 100 | + (export "waitable-set.wait" (func $waitable-set.wait)) |
| 101 | + (export "future.new" (func $future.new)) |
| 102 | + (export "future.read" (func $future.read)) |
| 103 | + (export "future.write" (func $future.write)) |
| 104 | + (export "future.close-readable" (func $future.close-readable)) |
| 105 | + (export "future.close-writable" (func $future.close-writable)) |
| 106 | + )))) |
| 107 | + (func (export "blocker") (result u32) (canon lift |
| 108 | + (core func $cm "blocker") |
| 109 | + async (memory $memory "mem") (callback (func $cm "blocker_cb")) |
| 110 | + )) |
| 111 | + (func (export "unblocker") (result u32) (canon lift |
| 112 | + (core func $cm "unblocker") |
| 113 | + async (memory $memory "mem") (callback (func $cm "unblocker_cb")) |
| 114 | + )) |
| 115 | + ) |
| 116 | + |
| 117 | + (component $D |
| 118 | + (import "c" (instance $c |
| 119 | + (export "blocker" (func (result u32))) |
| 120 | + (export "unblocker" (func (result u32))) |
| 121 | + )) |
| 122 | + |
| 123 | + (core module $Memory (memory (export "mem") 1)) |
| 124 | + (core instance $memory (instantiate $Memory)) |
| 125 | + (core module $DM |
| 126 | + (import "" "mem" (memory 1)) |
| 127 | + (import "" "waitable.join" (func $waitable.join (param i32 i32))) |
| 128 | + (import "" "waitable-set.new" (func $waitable-set.new (result i32))) |
| 129 | + (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32))) |
| 130 | + (import "" "blocker" (func $blocker (param i32 i32) (result i32))) |
| 131 | + (import "" "unblocker" (func $unblocker (param i32 i32) (result i32))) |
| 132 | + |
| 133 | + (global $ws (mut i32) (i32.const 0)) |
| 134 | + (func $start (global.set $ws (call $waitable-set.new))) |
| 135 | + (start $start) |
| 136 | + |
| 137 | + (func $run (export "run") (result i32) |
| 138 | + (local $ret i32) (local $subtaski i32) (local $event_code i32) (local $retp1 i32) (local $retp2 i32) |
| 139 | + |
| 140 | + ;; call 'blocker'; it should block |
| 141 | + (local.set $retp1 (i32.const 4)) |
| 142 | + (local.set $ret (call $blocker (i32.const 0xdeadbeef) (local.get $retp1))) |
| 143 | + (if (i32.ne (i32.const 1 (; STARTING ;)) (i32.and (local.get $ret) (i32.const 0xf))) |
| 144 | + (then unreachable)) |
| 145 | + (local.set $subtaski (i32.shr_u (local.get $ret) (i32.const 4))) |
| 146 | + (if (i32.ne (i32.const 2) (local.get $subtaski)) |
| 147 | + (then unreachable)) |
| 148 | + |
| 149 | + ;; call 'unblocker' to unblock 'blocker'; it should complete eagerly |
| 150 | + (local.set $retp2 (i32.const 8)) |
| 151 | + (local.set $ret (call $unblocker (i32.const 0xbeefdead) (local.get $retp2))) |
| 152 | + (if (i32.ne (i32.const 2) (local.get $ret)) |
| 153 | + (then unreachable)) |
| 154 | + (if (i32.ne (i32.const 43) (i32.load (local.get $retp2))) |
| 155 | + (then unreachable)) |
| 156 | + |
| 157 | + ;; wait for 'blocker' to be scheduled, run, and return |
| 158 | + (call $waitable.join (local.get $subtaski) (global.get $ws)) |
| 159 | + (local.set $retp2 (i32.const 8)) |
| 160 | + (local.set $event_code (call $waitable-set.wait (global.get $ws) (local.get $retp2))) |
| 161 | + (if (i32.ne (i32.const 1 (; SUBTASK ;)) (local.get $event_code)) |
| 162 | + (then unreachable)) |
| 163 | + (if (i32.ne (local.get $subtaski) (i32.load (local.get $retp2))) |
| 164 | + (then unreachable)) |
| 165 | + (if (i32.ne (i32.const 2 (; RETURNED ;)) (i32.load offset=4 (local.get $retp2))) |
| 166 | + (then unreachable)) |
| 167 | + (if (i32.ne (i32.const 42) (i32.load (local.get $retp1))) |
| 168 | + (then unreachable)) |
| 169 | + |
| 170 | + ;; return 44 to the top-level test harness |
| 171 | + (i32.const 44) |
| 172 | + ) |
| 173 | + ) |
| 174 | + (canon waitable.join (core func $waitable.join)) |
| 175 | + (canon waitable-set.new (core func $waitable-set.new)) |
| 176 | + (canon waitable-set.wait (memory $memory "mem") (core func $waitable-set.wait)) |
| 177 | + (canon lower (func $c "blocker") async (memory $memory "mem") (core func $blocker')) |
| 178 | + (canon lower (func $c "unblocker") async (memory $memory "mem") (core func $unblocker')) |
| 179 | + (core instance $dm (instantiate $DM (with "" (instance |
| 180 | + (export "mem" (memory $memory "mem")) |
| 181 | + (export "waitable.join" (func $waitable.join)) |
| 182 | + (export "waitable-set.new" (func $waitable-set.new)) |
| 183 | + (export "waitable-set.wait" (func $waitable-set.wait)) |
| 184 | + (export "blocker" (func $blocker')) |
| 185 | + (export "unblocker" (func $unblocker')) |
| 186 | + )))) |
| 187 | + (func (export "run") (result u32) (canon lift (core func $dm "run"))) |
| 188 | + ) |
| 189 | + |
| 190 | + (instance $c (instantiate $C)) |
| 191 | + (instance $d (instantiate $D (with "c" (instance $c)))) |
| 192 | + (func (export "run") (alias export $d "run")) |
| 193 | +) |
| 194 | +(assert_return (invoke "run") (u32.const 44)) |
0 commit comments