|
| 1 | +;; wait/notify |
| 2 | +(module |
| 3 | + (memory 1 1 shared) |
| 4 | + |
| 5 | + (func (export "init") (param $value i64) (i64.store (i32.const 0) (local.get $value))) |
| 6 | + |
| 7 | + (func (export "memory.atomic.notify") (param $addr i32) (param $count i32) (result i32) |
| 8 | + (memory.atomic.notify (local.get 0) (local.get 1))) |
| 9 | + (func (export "memory.atomic.wait32") (param $addr i32) (param $expected i32) (param $timeout i64) (result i32) |
| 10 | + (memory.atomic.wait32 (local.get 0) (local.get 1) (local.get 2))) |
| 11 | + (func (export "memory.atomic.wait64") (param $addr i32) (param $expected i64) (param $timeout i64) (result i32) |
| 12 | + (memory.atomic.wait64 (local.get 0) (local.get 1) (local.get 2))) |
| 13 | +) |
| 14 | + |
| 15 | +(invoke "init" (i64.const 0xffffffffffff)) |
| 16 | + |
| 17 | +;; wait returns immediately if values do not match |
| 18 | +(assert_return (invoke "memory.atomic.wait32" (i32.const 0) (i32.const 0) (i64.const 0)) (i32.const 1)) |
| 19 | +(assert_return (invoke "memory.atomic.wait64" (i32.const 0) (i64.const 0) (i64.const 0)) (i32.const 1)) |
| 20 | + |
| 21 | +;; wait times out if values do match and timeout is small |
| 22 | +(assert_return (invoke "memory.atomic.wait32" (i32.const 0) (i32.const 0xffffffff) (i64.const 10)) (i32.const 2)) |
| 23 | +(assert_return (invoke "memory.atomic.wait64" (i32.const 0) (i64.const 0xffffffffffff) (i64.const 10)) (i32.const 2)) |
| 24 | + |
| 25 | +;; notify always returns |
| 26 | +(assert_return (invoke "memory.atomic.notify" (i32.const 0) (i32.const 0)) (i32.const 0)) |
| 27 | +(assert_return (invoke "memory.atomic.notify" (i32.const 0) (i32.const 10)) (i32.const 0)) |
| 28 | + |
| 29 | +;; OOB wait and notify always trap |
| 30 | +(assert_trap (invoke "memory.atomic.wait32" (i32.const 65536) (i32.const 0) (i64.const 0)) "out of bounds memory access") |
| 31 | +(assert_trap (invoke "memory.atomic.wait64" (i32.const 65536) (i64.const 0) (i64.const 0)) "out of bounds memory access") |
| 32 | + |
| 33 | +;; in particular, notify always traps even if waking 0 threads |
| 34 | +(assert_trap (invoke "memory.atomic.notify" (i32.const 65536) (i32.const 0)) "out of bounds memory access") |
| 35 | + |
| 36 | +;; similarly, unaligned wait and notify always trap |
| 37 | +(assert_trap (invoke "memory.atomic.wait32" (i32.const 65531) (i32.const 0) (i64.const 0)) "unaligned atomic") |
| 38 | +(assert_trap (invoke "memory.atomic.wait64" (i32.const 65524) (i64.const 0) (i64.const 0)) "unaligned atomic") |
| 39 | + |
| 40 | +(assert_trap (invoke "memory.atomic.notify" (i32.const 65531) (i32.const 0)) "unaligned atomic") |
| 41 | + |
| 42 | +;; atomic.wait traps on unshared memory even if it wouldn't block |
| 43 | +(module |
| 44 | + (memory 1 1) |
| 45 | + |
| 46 | + (func (export "init") (param $value i64) (i64.store (i32.const 0) (local.get $value))) |
| 47 | + |
| 48 | + (func (export "memory.atomic.notify") (param $addr i32) (param $count i32) (result i32) |
| 49 | + (memory.atomic.notify (local.get 0) (local.get 1))) |
| 50 | + (func (export "memory.atomic.wait32") (param $addr i32) (param $expected i32) (param $timeout i64) (result i32) |
| 51 | + (memory.atomic.wait32 (local.get 0) (local.get 1) (local.get 2))) |
| 52 | + (func (export "memory.atomic.wait64") (param $addr i32) (param $expected i64) (param $timeout i64) (result i32) |
| 53 | + (memory.atomic.wait64 (local.get 0) (local.get 1) (local.get 2))) |
| 54 | +) |
| 55 | + |
| 56 | +(invoke "init" (i64.const 0xffffffffffff)) |
| 57 | + |
| 58 | +(assert_trap (invoke "memory.atomic.wait32" (i32.const 0) (i32.const 0) (i64.const 0)) "expected shared memory") |
| 59 | +(assert_trap (invoke "memory.atomic.wait64" (i32.const 0) (i64.const 0) (i64.const 0)) "expected shared memory") |
| 60 | + |
| 61 | +;; notify still works |
| 62 | +(assert_return (invoke "memory.atomic.notify" (i32.const 0) (i32.const 0)) (i32.const 0)) |
| 63 | + |
| 64 | +;; OOB and unaligned notify still trap |
| 65 | +(assert_trap (invoke "memory.atomic.notify" (i32.const 65536) (i32.const 0)) "out of bounds memory access") |
| 66 | +(assert_trap (invoke "memory.atomic.notify" (i32.const 65531) (i32.const 0)) "unaligned atomic") |
| 67 | + |
| 68 | +;; test that looping notify eventually unblocks a parallel waiting thread |
| 69 | +(module $Mem |
| 70 | + (memory (export "shared") 1 1 shared) |
| 71 | +) |
| 72 | + |
| 73 | +(thread $T1 (shared (module $Mem)) |
| 74 | + (register "mem" $Mem) |
| 75 | + (module |
| 76 | + (memory (import "mem" "shared") 1 10 shared) |
| 77 | + (func (export "run") (result i32) |
| 78 | + (memory.atomic.wait32 (i32.const 0) (i32.const 0) (i64.const -1)) |
| 79 | + ) |
| 80 | + ) |
| 81 | + ;; test that this thread eventually gets unblocked |
| 82 | + (assert_return (invoke "run") (i32.const 0)) |
| 83 | +) |
| 84 | + |
| 85 | +(thread $T2 (shared (module $Mem)) |
| 86 | + (register "mem" $Mem) |
| 87 | + (module |
| 88 | + (memory (import "mem" "shared") 1 1 shared) |
| 89 | + (func (export "notify-0") (result i32) |
| 90 | + (memory.atomic.notify (i32.const 0) (i32.const 0)) |
| 91 | + ) |
| 92 | + (func (export "notify-1-while") |
| 93 | + (loop |
| 94 | + (i32.const 1) |
| 95 | + (memory.atomic.notify (i32.const 0) (i32.const 1)) |
| 96 | + (i32.ne) |
| 97 | + (br_if 0) |
| 98 | + ) |
| 99 | + ) |
| 100 | + ) |
| 101 | + ;; notifying with a count of 0 will not unblock |
| 102 | + (assert_return (invoke "notify-0") (i32.const 0)) |
| 103 | + ;; loop until something is notified |
| 104 | + (assert_return (invoke "notify-1-while")) |
| 105 | +) |
| 106 | + |
| 107 | +(wait $T1) |
| 108 | +(wait $T2) |
0 commit comments