Skip to content

Commit 6b57720

Browse files
authored
Add a test for yielding (#1385)
* Add a test for yielding Also update to the new name of the yield intrinsic. * Fix async tests
1 parent 844f862 commit 6b57720

File tree

8 files changed

+87
-1
lines changed

8 files changed

+87
-1
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ jobs:
136136
--artifacts target/artifacts \
137137
--rust-wit-bindgen-path ./crates/guest-rust \
138138
--rust-target wasm32-wasip1 \
139+
--c-target wasm32-wasip1 \
139140
--runner "$WASMTIME -W component-model-async -Sp3"
140141
141142
test_unit:

crates/c/src/lib.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,8 @@ void {snake}_backpressure_inc(void);
761761
void {snake}_backpressure_dec(void);
762762
void* {snake}_context_get(void);
763763
void {snake}_context_set(void*);
764+
void {snake}_yield(void);
765+
uint32_t {snake}_yield_cancellable(void);
764766
"
765767
);
766768
uwriteln!(
@@ -855,6 +857,20 @@ extern void __context_set(void*);
855857
void {snake}_context_set(void *val) {{
856858
return __context_set(val);
857859
}}
860+
861+
__attribute__((__import_module__("$root"), __import_name__("[thread-yield]")))
862+
extern uint32_t __thread_yield(void);
863+
864+
void {snake}_yield(void) {{
865+
__thread_yield();
866+
}}
867+
868+
__attribute__((__import_module__("$root"), __import_name__("[cancellable][thread-yield]")))
869+
extern uint32_t __thread_yield_cancellable(void);
870+
871+
uint32_t {snake}_yield_cancellable(void) {{
872+
return __thread_yield_cancellable();
873+
}}
858874
"#
859875
);
860876
}

crates/guest-rust/src/rt/async_support.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ pub fn yield_blocking() -> bool {
464464
#[cfg(target_arch = "wasm32")]
465465
#[link(wasm_import_module = "$root")]
466466
extern "C" {
467-
#[link_name = "[yield]"]
467+
#[link_name = "[thread-yield]"]
468468
fn yield_() -> bool;
469469
}
470470
// Note that the return value from the raw intrinsic is inverted, the
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ args = '--rename a:b/i=test'
2+
#include <assert.h>
3+
#include <runner.h>
4+
5+
int main() {
6+
runner_subtask_status_t status = test_async_f();
7+
assert(RUNNER_SUBTASK_STATE(status) == RUNNER_SUBTASK_STARTED);
8+
runner_subtask_t task = RUNNER_SUBTASK_HANDLE(status);
9+
10+
runner_waitable_set_t set = runner_waitable_set_new();
11+
runner_waitable_join(task, set);
12+
runner_event_t event;
13+
runner_waitable_set_wait(set, &event);
14+
assert(event.event == RUNNER_EVENT_SUBTASK);
15+
assert(event.waitable == task);
16+
assert(event.code == RUNNER_SUBTASK_RETURNED);
17+
runner_waitable_join(task, 0);
18+
runner_waitable_set_drop(set);
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include!(env!("BINDINGS"));
2+
3+
fn main() {
4+
wit_bindgen::block_on(async {
5+
crate::a::b::i::f().await;
6+
});
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ args = '--rename a:b/i=test'
2+
3+
#include <assert.h>
4+
#include <test.h>
5+
#include <stdio.h>
6+
7+
test_subtask_status_t exports_test_async_f() {
8+
return TEST_CALLBACK_CODE_YIELD;
9+
}
10+
11+
test_subtask_status_t exports_test_async_f_callback(test_event_t *event) {
12+
assert(event->event == TEST_EVENT_NONE);
13+
assert(event->waitable == 0);
14+
assert(event->code == 0);
15+
test_yield();
16+
exports_test_async_f_return();
17+
return TEST_CALLBACK_CODE_EXIT;
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
include!(env!("BINDINGS"));
2+
3+
struct Component;
4+
5+
export!(Component);
6+
7+
impl crate::exports::a::b::i::Guest for Component {
8+
async fn f() {
9+
wit_bindgen::yield_async().await;
10+
assert!(wit_bindgen::yield_blocking());
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package a:b;
2+
3+
interface i {
4+
f: async func();
5+
}
6+
7+
world test {
8+
export i;
9+
}
10+
11+
world runner {
12+
import i;
13+
}

0 commit comments

Comments
 (0)