Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit c31ea25

Browse files
authored
Merge pull request #143 from bytecodealliance/dicej/refactor-concurrent-rs
refactor/rewrite `concurrent.rs` event loop
2 parents 66bd2cb + cc0e9a4 commit c31ea25

18 files changed

Lines changed: 1388 additions & 1738 deletions

File tree

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

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ enum State {
102102
set: u32,
103103
waitable: u32,
104104
params: *mut SleepParams,
105+
},
106+
S4 {
107+
set: u32,
108+
waitable: u32,
109+
params: *mut SleepParams,
105110
params2: *mut u64,
106111
},
107112
}
@@ -292,17 +297,39 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
292297

293298
let result = subtask_cancel_async(waitable);
294299

295-
// NB: As of this writing, Wasmtime does *not* spawn a new fiber
296-
// for async->async guest calls, which means we'll block
297-
// synchronously when calling `subtask_cancel_async` even though
298-
// we're calling it with the `async` canonical option. The
299-
// following assertion relies on that behavior; it will need to
300-
// be changed (and this test case refactored) if/when that
301-
// behavior changes.
302-
assert_eq!(result, STATUS_RETURN_CANCELLED);
300+
// NB: As of this writing, Wasmtime spawns a new fiber for
301+
// async->async guest calls, which means the above call should
302+
// block asynchronously, giving us back control. However, the
303+
// runtime could alternatively execute the call on the original
304+
// fiber, in which case the above call would block synchronously
305+
// and return `STATUS_RETURN_CANCELLED`. If Wasmtime's behavior
306+
// changes, this test will need to be modified.
307+
assert_eq!(result, BLOCKED);
303308

304-
waitable_join(waitable, 0);
305-
subtask_drop(waitable);
309+
waitable_join(waitable, *set);
310+
311+
let set = *set;
312+
313+
*state = State::S3 {
314+
set,
315+
waitable,
316+
params: *params,
317+
};
318+
319+
CALLBACK_CODE_WAIT | (set << 4)
320+
}
321+
322+
State::S3 {
323+
set,
324+
waitable,
325+
params,
326+
} => {
327+
assert_eq!(event0, EVENT_SUBTASK);
328+
assert_eq!(event1, *waitable);
329+
assert_eq!(event2, STATUS_RETURN_CANCELLED);
330+
331+
waitable_join(*waitable, 0);
332+
subtask_drop(*waitable);
306333

307334
// Next, call and cancel `sleep::sleep_millis`, which the callee
308335
// implements using both an synchronous lift and asynchronous
@@ -327,7 +354,7 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
327354

328355
let set = *set;
329356

330-
*state = State::S3 {
357+
*state = State::S4 {
331358
set,
332359
waitable,
333360
params: *params,
@@ -337,7 +364,7 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
337364
CALLBACK_CODE_WAIT | (set << 4)
338365
}
339366

340-
State::S3 {
367+
State::S4 {
341368
set,
342369
waitable,
343370
params,

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,17 @@ unsafe extern "C" fn callback_run(event0: u32, event1: u32, event2: u32) -> u32
102102
}
103103

104104
State::S3 { set, call } => {
105-
assert_eq!(event0, EVENT_SUBTASK);
106-
assert_eq!(event1, *call);
107-
assert_eq!(event2, STATUS_RETURNED);
108-
109105
let set = *set;
110-
subtask_drop(*call);
111106

112-
*state = State::S4 { set };
107+
if event0 != EVENT_NONE {
108+
assert_eq!(event0, EVENT_SUBTASK);
109+
assert_eq!(event1, *call);
110+
assert_eq!(event2, STATUS_RETURNED);
111+
112+
subtask_drop(*call);
113+
114+
*state = State::S4 { set };
115+
}
113116

114117
CALLBACK_CODE_POLL | (set << 4)
115118
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ unsafe extern "C" fn export_foo(args: *mut u8) {
100100
let payload = Box::from_raw(payload);
101101
if event == EVENT_SUBTASK {
102102
assert_eq!(call, payload[0] as u32);
103-
assert_eq!(STATUS_RETURNED, payload[1] as u32);
104-
subtask_drop(call);
105-
waitable_set_drop(set);
106-
status = STATUS_RETURNED;
103+
status = payload[1] as u32;
104+
if status == STATUS_RETURNED {
105+
subtask_drop(call);
106+
waitable_set_drop(set);
107+
}
107108
}
108109
}
109110
alloc::dealloc(params, layout);

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ unsafe extern "C" fn export_foo(ptr: *mut u8, len: usize) {
8686
let payload = Box::from_raw(payload);
8787
if event == EVENT_SUBTASK {
8888
assert_eq!(call, payload[0] as u32);
89-
assert_eq!(STATUS_RETURNED, payload[1] as u32);
90-
subtask_drop(call);
91-
waitable_set_drop(set);
92-
status = STATUS_RETURNED;
89+
status = payload[1] as u32;
90+
if status == STATUS_RETURNED {
91+
subtask_drop(call);
92+
waitable_set_drop(set);
93+
}
9394
}
9495
}
9596
alloc::dealloc(params, layout);

0 commit comments

Comments
 (0)