From f6b16a7178b75ff27e7d9f276a9cb39ddfcc1f8d Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Thu, 23 Feb 2023 07:23:58 -0800 Subject: [PATCH] wasi-threads: fix use of `wait` in test (#5858) As @yamt points out [here], the `wait`/`notify` pairing used in this manual WAT test was not effective. The `wait` always immediately returned, meaning that the main thread essentially spins until a counter is atomically incremented. This is fine for test correctness, but was not the original intent, which was lost in a refactoring. This change uses the `$i` local to keep track of the counter value we expect to see for the `wait`, so that the `wait`/`notify` pair actually waits as expected. [here]: https://github.com/bytecodealliance/wasmtime/pull/5484#discussion_r1101200012 --- tests/all/cli_tests/threads.wat | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/tests/all/cli_tests/threads.wat b/tests/all/cli_tests/threads.wat index 3e5b4397b9..def5011f8e 100644 --- a/tests/all/cli_tests/threads.wat +++ b/tests/all/cli_tests/threads.wat @@ -21,12 +21,15 @@ (drop (call $__wasi_thread_spawn (i32.const 0))) ;; Wait for all the threads to notify us that they are done. + (local.set $i (i32.const 0)) (loop $again - ;; Retrieve the i32 at address 128, compare it to -1 (it should always - ;; fail) and load it atomically to check if all three threads are - ;; complete. This wait is for 1ms or until notified, whichever is first. - (drop (memory.atomic.wait32 (i32.const 128) (i32.const -1) (i64.const 1000000))) - (br_if $again (i32.lt_s (i32.atomic.load (i32.const 128)) (i32.const 3))) + ;; Wait for the i32 at address 128 to be incremented by each thread. We + ;; maintain a local $i with the atomically loaded value as the expected + ;; wait value and to check if all three threads are complete. This wait is + ;; for 1ms or until notified, whichever is first. + (drop (memory.atomic.wait32 (i32.const 128) (local.get $i) (i64.const 1000000))) + (local.set $i (i32.atomic.load (i32.const 128))) + (br_if $again (i32.lt_s (local.get $i) (i32.const 3))) ) ;; Print "Done".