Path: blob/main/crates/test-programs/src/bin/async_poll_synchronous.rs
3068 views
mod bindings {1wit_bindgen::generate!({2path: "../misc/component-async-tests/wit",3world: "poll",4async: false,5});67use super::Component;8export!(Component);9}1011use {12bindings::{exports::local::local::run::Guest, local::local::ready},13test_programs::async_::{14EVENT_NONE, EVENT_SUBTASK, STATUS_RETURNED, SUSPEND_RESULT_NOT_CANCELLED, subtask_drop,15thread_yield, waitable_join, waitable_set_drop, waitable_set_new, waitable_set_poll,16},17};1819fn async_when_ready(handle: u32) -> u32 {20#[cfg(not(target_arch = "wasm32"))]21{22_ = handle;23unreachable!()24}2526#[cfg(target_arch = "wasm32")]27{28#[link(wasm_import_module = "local:local/ready")]29unsafe extern "C" {30#[link_name = "[async-lower][method]thing.when-ready"]31fn call_when_ready(handle: u32) -> u32;32}33unsafe { call_when_ready(handle) }34}35}3637struct Component;3839impl Guest for Component {40fn run() {41unsafe {42let thing = ready::Thing::new();43thing.set_ready(false);4445let set = waitable_set_new();4647assert_eq!(waitable_set_poll(set), (EVENT_NONE, 0, 0));4849let result = async_when_ready(thing.handle());50let status = result & 0xf;51let call = result >> 4;52assert!(status != STATUS_RETURNED);53waitable_join(call, set);5455assert_eq!(waitable_set_poll(set), (EVENT_NONE, 0, 0));5657thing.set_ready(true);5859assert_eq!(thread_yield(), SUSPEND_RESULT_NOT_CANCELLED);6061let (event, task, code) = waitable_set_poll(set);62assert_eq!(event, EVENT_SUBTASK);63assert_eq!(call, task);64assert_eq!(code, STATUS_RETURNED);6566subtask_drop(task);6768assert_eq!(waitable_set_poll(set), (EVENT_NONE, 0, 0));6970assert_eq!(async_when_ready(thing.handle()), STATUS_RETURNED);7172assert_eq!(waitable_set_poll(set), (EVENT_NONE, 0, 0));7374waitable_set_drop(set);75}76}77}7879// Unused function; required since this file is built as a `bin`:80fn main() {}818283