Skip to content

Commit 34ba273

Browse files
authored
remove callback_code::POLL and make waitable-set.poll not yield (#12182)
As of WebAssembly/component-model#578, there is no more `POLL` callback code, and `waitable-set.poll` should not yield. Guest toolchains can emulate the old code with a combination of `YIELD` and `waitable-set.poll`. Note that I've removed the now-redundant copy of `trap-if-block-and-sync.wast` in favor of the upstream version.
1 parent 7da79ce commit 34ba273

File tree

9 files changed

+164
-568
lines changed

9 files changed

+164
-568
lines changed

Cargo.lock

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ io-lifetimes = { version = "2.0.3", default-features = false }
337337
io-extras = "0.18.4"
338338
rustix = "1.0.8"
339339
# wit-bindgen:
340-
wit-bindgen = { version = "0.49.0", default-features = false }
341-
wit-bindgen-rust-macro = { version = "0.49.0", default-features = false }
340+
wit-bindgen = { version = "0.50.0", default-features = false }
341+
wit-bindgen-rust-macro = { version = "0.50.0", default-features = false }
342342

343343
# wasm-tools family:
344344
wasmparser = { version = "0.243.0", default-features = false, features = ['simd'] }

crates/test-programs/src/async_.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ pub unsafe extern "C" fn task_cancel() {
125125
unreachable!()
126126
}
127127

128+
#[cfg(target_arch = "wasm32")]
129+
#[link(wasm_import_module = "$root")]
130+
unsafe extern "C" {
131+
#[link_name = "[thread-yield]"]
132+
pub fn thread_yield() -> u32;
133+
}
134+
#[cfg(not(target_arch = "wasm32"))]
135+
pub unsafe fn thread_yield() -> u32 {
136+
unreachable!()
137+
}
138+
128139
pub const STATUS_STARTING: u32 = 0;
129140
pub const STATUS_STARTED: u32 = 1;
130141
pub const STATUS_RETURNED: u32 = 2;
@@ -142,8 +153,10 @@ pub const EVENT_CANCELLED: u32 = 6;
142153
pub const CALLBACK_CODE_EXIT: u32 = 0;
143154
pub const CALLBACK_CODE_YIELD: u32 = 1;
144155
pub const CALLBACK_CODE_WAIT: u32 = 2;
145-
pub const CALLBACK_CODE_POLL: u32 = 3;
146156

147157
pub const BLOCKED: u32 = 0xffff_ffff;
148158
pub const DROPPED: u32 = 1;
149159
pub const COMPLETED: u32 = 0;
160+
161+
pub const SUSPEND_RESULT_NOT_CANCELLED: u32 = 0;
162+
pub const SUSPEND_RESULT_CANCELLED: u32 = 1;

crates/test-programs/src/bin/async_poll_stackless.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ mod bindings {
1010
use {
1111
bindings::local::local::ready,
1212
test_programs::async_::{
13-
CALLBACK_CODE_EXIT, CALLBACK_CODE_POLL, EVENT_NONE, EVENT_SUBTASK, STATUS_RETURNED,
13+
CALLBACK_CODE_EXIT, CALLBACK_CODE_YIELD, EVENT_NONE, EVENT_SUBTASK, STATUS_RETURNED,
1414
context_get, context_set, subtask_drop, waitable_join, waitable_set_drop, waitable_set_new,
15+
waitable_set_poll,
1516
},
1617
};
1718

@@ -59,7 +60,7 @@ unsafe extern "C" fn export_run() -> u32 {
5960
}
6061

6162
#[unsafe(export_name = "[callback][async-lift]local:local/run#run")]
62-
unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32 {
63+
unsafe extern "C" fn callback_run(event0: u32, _: u32, _: u32) -> u32 {
6364
let state = &mut *(usize::try_from(context_get()).unwrap() as *mut State);
6465
match state {
6566
State::S0 => {
@@ -71,13 +72,14 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
7172

7273
*state = State::S1 { set };
7374

74-
CALLBACK_CODE_POLL | (set << 4)
75+
CALLBACK_CODE_YIELD
7576
}
7677

77-
State::S1 { set } => {
78+
&mut State::S1 { set } => {
79+
let (event0, _, _) = waitable_set_poll(set);
80+
7881
assert_eq!(event0, EVENT_NONE);
7982

80-
let set = *set;
8183
let result = async_when_ready();
8284
let status = result & 0xf;
8385
let call = result >> 4;
@@ -86,52 +88,55 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
8688

8789
*state = State::S2 { set, call };
8890

89-
CALLBACK_CODE_POLL | (set << 4)
91+
CALLBACK_CODE_YIELD
9092
}
9193

92-
State::S2 { set, call } => {
94+
&mut State::S2 { set, call } => {
95+
let (event0, _, _) = waitable_set_poll(set);
96+
9397
assert_eq!(event0, EVENT_NONE);
9498

95-
let set = *set;
96-
let call = *call;
9799
ready::set_ready(true);
98100

99101
*state = State::S3 { set, call };
100102

101-
CALLBACK_CODE_POLL | (set << 4)
103+
CALLBACK_CODE_YIELD
102104
}
103105

104-
State::S3 { set, call } => {
105-
let set = *set;
106+
&mut State::S3 { set, call } => {
107+
let (event0, event1, event2) = waitable_set_poll(set);
106108

107109
if event0 != EVENT_NONE {
108110
assert_eq!(event0, EVENT_SUBTASK);
109-
assert_eq!(event1, *call);
111+
assert_eq!(event1, call);
110112
assert_eq!(event2, STATUS_RETURNED);
111113

112-
subtask_drop(*call);
114+
subtask_drop(call);
113115

114116
*state = State::S4 { set };
115117
}
116118

117-
CALLBACK_CODE_POLL | (set << 4)
119+
CALLBACK_CODE_YIELD
118120
}
119121

120-
State::S4 { set } => {
122+
&mut State::S4 { set } => {
123+
let (event0, _, _) = waitable_set_poll(set);
124+
121125
assert_eq!(event0, EVENT_NONE);
122126

123-
let set = *set;
124127
assert_eq!(async_when_ready(), STATUS_RETURNED);
125128

126129
*state = State::S5 { set };
127130

128-
CALLBACK_CODE_POLL | (set << 4)
131+
CALLBACK_CODE_YIELD
129132
}
130133

131-
State::S5 { set } => {
134+
&mut State::S5 { set } => {
135+
let (event0, _, _) = waitable_set_poll(set);
136+
132137
assert_eq!(event0, EVENT_NONE);
133138

134-
waitable_set_drop(*set);
139+
waitable_set_drop(set);
135140

136141
drop(Box::from_raw(state));
137142

crates/test-programs/src/bin/async_poll_synchronous.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ mod bindings {
1212
use {
1313
bindings::{exports::local::local::run::Guest, local::local::ready},
1414
test_programs::async_::{
15-
EVENT_NONE, EVENT_SUBTASK, STATUS_RETURNED, subtask_drop, waitable_join, waitable_set_drop,
16-
waitable_set_new, waitable_set_poll,
15+
EVENT_NONE, EVENT_SUBTASK, STATUS_RETURNED, SUSPEND_RESULT_NOT_CANCELLED, subtask_drop,
16+
thread_yield, waitable_join, waitable_set_drop, waitable_set_new, waitable_set_poll,
1717
},
1818
};
1919

@@ -55,6 +55,8 @@ impl Guest for Component {
5555

5656
ready::set_ready(true);
5757

58+
assert_eq!(thread_yield(), SUSPEND_RESULT_NOT_CANCELLED);
59+
5860
let (event, task, code) = waitable_set_poll(set);
5961
assert_eq!(event, EVENT_SUBTASK);
6062
assert_eq!(call, task);

crates/test-util/src/wast.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,6 @@ impl WastTest {
701701
"component-model/test/values/trap-in-post-return.wast",
702702
"component-model/test/wasmtime/resources.wast",
703703
"component-model/test/wasm-tools/naming.wast",
704-
// FIXME(#12129)
705-
"component-model/test/async/trap-if-block-and-sync.wast",
706704
// TODO: Remove this once
707705
// https://github.com/bytecodealliance/wasm-tools/pull/2406 is
708706
// merged and released, and Wasmtime has been updated to use it:

0 commit comments

Comments
 (0)