MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / atomic_wait_notify_basic

Function atomic_wait_notify_basic

tests/all/wait_notify.rs:44–122  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

42
43#[test]
44fn 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.

Callers

nothing calls this directly

Calls 10

OkFunction · 0.85
spawnFunction · 0.85
engineFunction · 0.70
newFunction · 0.50
cloneMethod · 0.45
unwrapMethod · 0.45
callMethod · 0.45
joinMethod · 0.45
dataMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected