22 * \file wasmtime/async.h
33 *
44 * \brief Wasmtime async functionality
5+ *
6+ * Async functionality in Wasmtime is well documented here:
7+ * https://docs.wasmtime.dev/api/wasmtime/struct.Config.html#method.async_support
8+ *
9+ * All WebAssembly executes synchronously, but an async support enables the Wasm code
10+ * be executed on a seperate stack, so it can be paused and resumed. There are three
11+ * mechanisms for yielding control from wasm to the caller: fuel, epochs, and async host functions.
12+ *
13+ * When WebAssembly is executed, a #wasmtime_call_future_t is returned. This struct represents the
14+ * state of the execution and each call to #wasmtime_call_future_poll will execute the WebAssembly
15+ * code on a seperate stack until the function returns or yields control back to the caller.
16+ *
17+ * It's expected these futures are pulled in a loop until completed, at which point the future
18+ * should be deleted. Functions that return a #wasmtime_call_future_t are special in that all
19+ * parameters to that function should not be modified in any way and must be kept alive until
20+ * the future is deleted. This includes concurrent calls for a single store - another function
21+ * on a store should not be called while there is a #wasmtime_call_future_t alive.
22+ *
23+ * As for asynchronous host calls - the reverse contract is upheld. Wasmtime will keep all parameters
24+ * to the function alive and unmodified until the #wasmtime_func_async_continuation_callback_t returns
25+ * as completed or returns a trap.
26+ *
527 */
628
729#ifndef WASMTIME_ASYNC_H
@@ -90,8 +112,6 @@ typedef bool (*wasmtime_func_async_continuation_callback_t)(
90112
91113/**
92114 * A continuation for the current state of the host function's execution.
93- *
94- * This continutation can be polled via the callback and returns the current state.
95115 */
96116typedef struct wasmtime_async_continuation_t {
97117 wasmtime_func_async_continuation_callback_t callback ;
@@ -123,18 +143,18 @@ typedef wasmtime_async_continuation_t *(*wasmtime_func_async_callback_t)(
123143/**
124144 * \brief The structure representing a asynchronously running function.
125145 *
126- * This structure is always owned by the caller and must be deleted using wasmtime_call_future_delete.
127- *
128- *
146+ * This structure is always owned by the caller and must be deleted using #wasmtime_call_future_delete.
129147 *
148+ * Functions that return this type require that the parameters to the function are unmodified until
149+ * this future is destroyed.
130150 */
131151typedef struct wasmtime_call_future wasmtime_call_future_t ;
132152
133153/**
134154 * \brief Executes WebAssembly in the function.
135155 *
136- * Returns true if the function call has completed, which then wasmtime_call_future_get_results should be called.
137- * After this function returns true, it should *not* be called again for a given future.
156+ * Returns true if the function call has completed. After this function returns true, it should *not* be
157+ * called again for a given future.
138158 *
139159 * This function returns false if execution has yielded either due to being out of fuel
140160 * (see wasmtime_store_out_of_fuel_async_yield), or the epoch has been incremented enough
@@ -152,8 +172,7 @@ WASM_API_EXTERN bool wasmtime_call_future_poll(wasmtime_call_future_t *future);
152172/**
153173 * /brief Frees the underlying memory for a future.
154174 *
155- * All wasmtime_call_future_t are owned by the caller and should be deleted using this function no
156- * matter the result.
175+ * All wasmtime_call_future_t are owned by the caller and should be deleted using this function.
157176 */
158177WASM_API_EXTERN void wasmtime_call_future_delete (wasmtime_call_future_t * future );
159178
@@ -166,11 +185,15 @@ WASM_API_EXTERN void wasmtime_call_future_delete(wasmtime_call_future_t *future)
166185 * The result is a future that is owned by the caller and must be deleted via #wasmtime_call_future_delete.
167186 *
168187 * The `args` and `results` pointers may be `NULL` if the corresponding length is zero.
188+ * The `trap_ret` and `error_ret` pointers may *not* be `NULL`.
169189 *
170190 * Does not take ownership of #wasmtime_val_t arguments or #wasmtime_val_t results,
171- * the arguments and results must be kept alive until the returned #wasmtime_call_future_t is deleted.
191+ * and all parameters to this function must be kept alive and not modified until the
192+ * returned #wasmtime_call_future_t is deleted. This includes the context and store
193+ * parameters. Only a single future can be alive for a given store at a single time
194+ * (meaning only call this function after the previous call's future was deleted).
172195 *
173- * See #wasmtime_call_future_t for for more information.
196+ * See the header documentation for for more information.
174197 *
175198 * For more information see the Rust documentation at
176199 * https://docs.wasmtime.dev/api/wasmtime/struct.Func.html#method.call_async
@@ -183,13 +206,15 @@ WASM_API_EXTERN wasmtime_call_future_t* wasmtime_func_call_async(
183206 wasmtime_val_t * results ,
184207 size_t nresults ,
185208 wasm_trap_t * * trap_ret ,
186- wasmtime_error_t * * wasmtime_error_t );
209+ wasmtime_error_t * * error_ret );
187210
188211/**
189212 * \brief Defines a new async function in this linker.
190213 *
191214 * This function behaves similar to #wasmtime_linker_define_func, except it supports async
192- * callbacks
215+ * callbacks.
216+ *
217+ * The callback `cb` will be invoked on another stack (fiber for Windows).
193218 */
194219WASM_API_EXTERN wasmtime_error_t * wasmtime_linker_define_async_func (
195220 wasmtime_linker_t * linker ,
@@ -208,10 +233,10 @@ WASM_API_EXTERN wasmtime_error_t *wasmtime_linker_define_async_func(
208233 * This is the same as #wasmtime_linker_instantiate but used for async stores
209234 * (which requires functions are called asynchronously). The returning #wasmtime_call_future_t
210235 * must be polled using #wasmtime_call_future_poll, and is owned and must be deleted using #wasmtime_call_future_delete.
211- * The future's results are retrieved using `wasmtime_call_future_get_results after polling has returned true marking
212- * the future as completed.
213236 *
214- * All arguments to this function must outlive the returned future.
237+ * The `trap_ret` and `error_ret` pointers may *not* be `NULL`.
238+ *
239+ * All arguments to this function must outlive the returned future and be unmodified until the future is deleted.
215240 */
216241WASM_API_EXTERN wasmtime_call_future_t * wasmtime_linker_instantiate_async (
217242 const wasmtime_linker_t * linker ,
0 commit comments