| 42 | |
| 43 | #[test] |
| 44 | fn atomic_wait_notify_basic() -> Result<()> { |
| 45 | let wat = r#"(module |
| 46 | (import "env" "memory" (memory 1 1 shared)) |
| 47 | |
| 48 | (func (export "first_thread") (result i32) |
| 49 | (drop (memory.atomic.wait32 (i32.const 4) (i32.const 0) (i64.const -1))) |
| 50 | (i32.atomic.store (i32.const 0) (i32.const 42)) |
| 51 | (drop (memory.atomic.notify (i32.const 0) (i32.const -1))) |
| 52 | (i32.atomic.load (i32.const 0)) |
| 53 | ) |
| 54 | |
| 55 | (func (export "second_thread") (result i32) |
| 56 | (i32.atomic.store (i32.const 4) (i32.const 21)) |
| 57 | (drop (memory.atomic.notify (i32.const 4) (i32.const -1))) |
| 58 | (drop (memory.atomic.wait32 (i32.const 0) (i32.const 0) (i64.const -1))) |
| 59 | (i32.atomic.load (i32.const 0)) |
| 60 | ) |
| 61 | |
| 62 | (data (i32.const 0) "\00\00\00\00") |
| 63 | (data (i32.const 4) "\00\00\00\00") |
| 64 | )"#; |
| 65 | let Some(engine) = engine() else { |
| 66 | return Ok(()); |
| 67 | }; |
| 68 | let module = Module::new(&engine, wat)?; |
| 69 | let mut store = Store::new(&engine, ()); |
| 70 | let shared_memory = SharedMemory::new(&engine, MemoryType::shared(1, 1))?; |
| 71 | let instance1 = Instance::new(&mut store, &module, &[shared_memory.clone().into()])?; |
| 72 | |
| 73 | let thread = { |
| 74 | let engine = engine.clone(); |
| 75 | let module = module.clone(); |
| 76 | let shared_memory = shared_memory.clone(); |
| 77 | std::thread::spawn(move || { |
| 78 | let mut store = Store::new(&engine, ()); |
| 79 | let instance2 = Instance::new(&mut store, &module, &[shared_memory.into()]).unwrap(); |
| 80 | |
| 81 | let instance2_first_word = instance2 |
| 82 | .get_typed_func::<(), i32>(&mut store, "second_thread") |
| 83 | .unwrap() |
| 84 | .call(&mut store, ()) |
| 85 | .unwrap(); |
| 86 | |
| 87 | assert_eq!(instance2_first_word, 42); |
| 88 | }) |
| 89 | }; |
| 90 | |
| 91 | let instance1_first_word = instance1 |
| 92 | .get_typed_func::<(), i32>(&mut store, "first_thread") |
| 93 | .unwrap() |
| 94 | .call(&mut store, ()) |
| 95 | .unwrap(); |
| 96 | assert_eq!(instance1_first_word, 42); |
| 97 | |
| 98 | thread.join().unwrap(); |
| 99 | |
| 100 | let data = shared_memory.data(); |
| 101 | // Verify that the memory is the same in all shared locations. |