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

Function loop_interrupt_from_afar

tests/all/iloop.rs:57–101  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

55
56#[test]
57fn loop_interrupt_from_afar() -> wasmtime::Result<()> {
58 // Create an instance which calls an imported function on each iteration of
59 // the loop so we can count the number of loop iterations we've executed so
60 // far.
61 static HITS: AtomicUsize = AtomicUsize::new(0);
62 static STOP: AtomicBool = AtomicBool::new(false);
63 let mut store = interruptible_store();
64 let module = Module::new(
65 store.engine(),
66 r#"
67 (import "" "" (func))
68
69 (func (export "loop")
70 (loop
71 call 0
72 br 0)
73 )
74 "#,
75 )?;
76 let func = Func::wrap(&mut store, || {
77 HITS.fetch_add(1, SeqCst);
78 });
79 let instance = Instance::new(&mut store, &module, &[func.into()])?;
80
81 // Use the engine to wait for it to enter the loop long enough and then we
82 // signal an interrupt happens.
83 let engine = store.engine().clone();
84 let thread = std::thread::spawn(move || {
85 while HITS.load(SeqCst) <= NUM_HITS && !STOP.load(SeqCst) {
86 // continue ...
87 }
88 println!("interrupting");
89 engine.increment_epoch();
90 });
91
92 // Enter the infinitely looping function and assert that our interrupt
93 // handle does indeed actually interrupt the function.
94 let iloop = instance.get_typed_func::<(), ()>(&mut store, "loop")?;
95 let trap = iloop.call(&mut store, ()).unwrap_err().downcast::<Trap>()?;
96 STOP.store(true, SeqCst);
97 thread.join().unwrap();
98 assert!(HITS.load(SeqCst) > NUM_HITS);
99 assert_eq!(trap, Trap::Interrupt);
100 Ok(())
101}
102
103#[test]
104fn function_interrupt_from_afar() -> wasmtime::Result<()> {

Callers

nothing calls this directly

Calls 12

interruptible_storeFunction · 0.85
spawnFunction · 0.85
OkFunction · 0.85
newFunction · 0.50
engineMethod · 0.45
cloneMethod · 0.45
loadMethod · 0.45
increment_epochMethod · 0.45
callMethod · 0.45
storeMethod · 0.45
unwrapMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected