-
Notifications
You must be signed in to change notification settings - Fork 269
Implement a C ABI for async import/export communication #1254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexcrichton
merged 6 commits into
bytecodealliance:main
from
alexcrichton:async-libc-interface
Apr 3, 2025
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b38aa77
Implement a C ABI for async import/export communication
alexcrichton 181912b
Refactor with separate objects
alexcrichton 29295f7
Update crates/guest-rust/rt/src/async_support/waitable.rs
alexcrichton 04f4253
Update wasi-sdk in CI
alexcrichton c0216a1
Review comments
alexcrichton bd50ee2
Get crate compiling on native
alexcrichton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| //! Definition of the "C ABI" of how imported functions interact with exported | ||
| //! tasks. | ||
| //! | ||
| //! Ok this crate is written in Rust, why in the world does this exist? This | ||
| //! comment is intended to explain this rationale but the tl;dr; is we want | ||
| //! this to work: | ||
| //! | ||
| //! * Within a single component ... | ||
| //! * One rust crate uses `wit-bindgen 0.A.0` to generate an exported function. | ||
| //! * One rust crate uses `wit-bindgen 0.B.0` to bind an imported function. | ||
| //! * The two crates are connected in the application with | ||
| //! `std::future::Future`. | ||
| //! | ||
| //! Without this module this situation won't work because 0.A.0 has no | ||
| //! knowledge of 0.B.0 meaning that when 0.B.0 decides to block it won't know | ||
| //! where to register its `waitable` within a `waitable-set`. | ||
| //! | ||
| //! To solve this problem the long-term intention is that something will live | ||
| //! in `wasi-libc` itself, but in the meantime it's living "somewhere" within | ||
| //! `wit-bindgen 0.*.0`. Specifically all `wit-bindgen` versions will all | ||
|
alexcrichton marked this conversation as resolved.
Outdated
|
||
| //! reference, via C linkage, a single function which is used to manipulate a | ||
| //! single pointer in linear memory. This pointer is a `wasip3_task` structure | ||
| //! which has all the various fields to use it. | ||
| //! | ||
| //! The `wasip3_task_set` symbol is itself defined in C inside of the | ||
| //! `src/wit_bindgen_cabi.c` file at this time, specifically because it's | ||
| //! annotated with `__weak__` meaning that any definition of it suffices. This | ||
| //! isn't possible to define in stable Rust (specifically `__weak__`). | ||
| //! | ||
| //! Once `wasip3_task_set` is defined everything then operates via indirection, | ||
| //! aka based off the returned pointer. The intention is that exported functions | ||
| //! will set this (it's sort of like an executor) and then imported functions | ||
| //! will all use this as the source of registering waitables. In the end that | ||
| //! means that it's possible to share types with `std::future::Future` that | ||
| //! are backed at the ABI level with this "channel". | ||
| //! | ||
| //! In the future it's hoped that this can move into `wasi-libc` itself, or if | ||
| //! `wasi-libc` provides something else that would be prioritized over this. | ||
| //! For now this is basically an affordance that we're going to be frequently | ||
| //! releaseing new major versions of `wit-bindgen` and we don't want to force | ||
| //! applications to all be using the exact same version of the bindings | ||
| //! generator and async bindings. | ||
| //! | ||
| //! Additionally for now this file is serving as documentation of this | ||
| //! interface. | ||
|
|
||
| use core::ffi::c_void; | ||
|
|
||
| extern "C" { | ||
| /// Sets the global task pointer to `ptr` provided. Returns the previous | ||
| /// value. | ||
| /// | ||
| /// This function acts as both a dual getter and a setter. To get the | ||
|
alexcrichton marked this conversation as resolved.
Outdated
|
||
| /// current task pointer a dummy `ptr` can be provided (e.g. NULL) and then | ||
| /// it's passed back when you're done working with it. When setting the | ||
| /// current task pointer it's recommended to call this and then call it | ||
| /// again with the previous value when the tasks's work is done. | ||
| /// | ||
| /// For executors they need to ensure that the `ptr` passed in lives for | ||
| /// the entire lifetime of the component model task. | ||
|
alexcrichton marked this conversation as resolved.
|
||
| pub fn wasip3_task_set(ptr: *mut wasip3_task) -> *mut wasip3_task; | ||
| } | ||
|
|
||
| /// The first version of `wasip3_task` which implies the existence of the | ||
| /// fields `ptr`, `waitable_register`, and `waitable_unregister`. | ||
| pub const WASIP3_TASK_V1: u32 = 1; | ||
|
|
||
| /// Indirect "vtable" used to connect imported functions and exported tasks. | ||
| /// Executors (e.g. exported functions) define and manage this while imports | ||
| /// use it. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self. Why am I making comments when I can't even visual what is meant by exporting a task? |
||
| #[repr(C)] | ||
| pub struct wasip3_task { | ||
| /// Currently `WASIP3_TASK_V1`. Indicates what fields are present next | ||
| /// depending on the version here. | ||
| pub version: u32, | ||
|
|
||
| /// Private pointer owned by the `wasip3_task` itself, passed to callbacks | ||
| /// below as the first argument. | ||
| pub ptr: *mut c_void, | ||
|
|
||
| /// Register a new `waitable` for this exported task. | ||
| /// | ||
| /// This exported task will add `waitable` to its `waitable-set`. When it | ||
| /// becomes ready then `callback` will be invoked with the ready code as | ||
| /// well as the `callback_ptr` provided. | ||
| /// | ||
| /// If `waitable` was previously registered with this task then the | ||
| /// previuos `callback_ptr` is returned. Otherwise `NULL` is returned. | ||
|
alexcrichton marked this conversation as resolved.
Outdated
|
||
| /// | ||
| /// It's the caller's responsibility to ensure that `callback_ptr` is valid | ||
| /// until `callback` is invoked, `waitable_unregister` is invoked, or | ||
| /// `waitable_register` is called again to overwrite the value. | ||
| pub waitable_register: unsafe extern "C" fn( | ||
| ptr: *mut c_void, | ||
| waitable: u32, | ||
| callback: unsafe extern "C" fn(callback_ptr: *mut c_void, code: u32), | ||
| callback_ptr: *mut c_void, | ||
| ) -> *mut c_void, | ||
|
|
||
| /// Removes the `waitable` from this task's `waitable-set`. | ||
| /// | ||
| /// Returns the `callback_ptr` passed to `waitable_register` if present, or | ||
| /// `NULL` if it's not present. | ||
| pub waitable_unregister: unsafe extern "C" fn(ptr: *mut c_void, waitable: u32) -> *mut c_void, | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.