Commit 421136d
generalize async fiber abstraction (#11114)
* generalize async fiber abstraction
As part of the work implementing the new Component Model async ABI in the
`wasip3-prototyping` repo, I've generalized the `FiberFuture` abstraction in
`wasmtime::runtime::store::async_` to support fibers which can either retain
exclusive access to the store across suspend points or release it. The latter
allows the store to be used by the `component-model-async` event loop and/or
other fibers to run before the original fiber resumes, which is the key to
allowing multiple fibers to run concurrently, passing control of the store back
and forth.
In the case of Pulley, the above generalization means we also need to give each
fiber its own `Interpreter` so that multiple concurrent fibers don't clobber
each other's state.
Concretely, this moves a lot of the code out of `async_.rs` and into a new
`fiber.rs` submodule which will be shared with the `component-model-async`
implementation.
This also pulls in a new `StoreToken<T>` utility which has been useful in
`wasip3-prototyping` to safely convert from a `&mut dyn VMStore` to a
`StoreContextMut<'a, T>` when we previously witnessed a conversion in the other
direction.
Note that I've added a `'static` bound to the `VMStore` trait, which simplifies
use of `&mut dyn VMStore`, avoiding thorny lifetime issues.
Signed-off-by: Joel Dice <[email protected]>
* address review feedback
Signed-off-by: Joel Dice <[email protected]>
* fix miri-flagged stacked borrow violation
As part of my earlier effort to unify the fiber abstractions in the `wasmtime`
crate, I changed a `*mut StoreFiber` field to a `&mut StoreFiber`, not realizing
that it resulted in a mutable alias at runtime and thus undefined behavior.
Miri caught it, fortunately.
Signed-off-by: Joel Dice <[email protected]>
* remove unneeded `Send` bounds
Signed-off-by: Joel Dice <[email protected]>
* address more review feedback
Main changes:
- Make `resume_fiber[_raw]` take a `&mut StoreOpaque` parameter to make its unsafe internals easier to reason about, safety-wise.
- Panic if `StoreFiber::drop` is called on an in-progress fiber without having called `StoreFiber::dispose` to gracefully end it first.
- (Re)introduce `FiberFuture`, which closes over a `&mut StoreOpaque` and uses it to call `StoreFiber::dispose` on drop.
This will require a few more changes to make it usable by `concurrent.rs`, but
I'll save those changes for a later PR.
Signed-off-by: Joel Dice <[email protected]>
* address more review feedback
Signed-off-by: Joel Dice <[email protected]>
* update `impl Send For StoreFiber` comment
Signed-off-by: Joel Dice <[email protected]>
* Remove currently-extraneous `Result<()>` from fibers
May be needed for concurrent bits, but for now not necessary.
* Use safe pointers instead of raw pointers
It's predicted Miri won't like this, but for now in-repo it's ok with it.
* Fold more responsibility into `resume_fiber_raw`
Remove the need for the function entirely and replace it with
`resume_fiber`.
* Remove channels from async fibers
Can use stack-based closures/results to transmit the result instead of
needing a channel.
* Fold `on_fiber_raw` directly into `on_fiber`
The `on_fiber` function is small enough it should be possible to do so.
* Don't use `Option` in `FiberFuture`
Leave the fiber non-optional at-rest so it's always available for the
destructor.
* Fold `suspend` functions together
Small shims, not otherwise public at this time, so remove a layer of
indirection.
* Move stack limit management to `FiberResumeState`
Helps remove some raw pointers that are held for a long time within
`AsyncCx`
* add some doc comments to `fiber.rs`
Signed-off-by: Joel Dice <[email protected]>
* update `fiber.rs` and friends to match CM async requirements
This adds a `resolve_or_release` function, which `Instance::resume_fiber` will
use when current `concurrent.rs` stub is replaced by a real implementation.
Signed-off-by: Joel Dice <[email protected]>
* fix non-component-model-async build warnings
Signed-off-by: Joel Dice <[email protected]>
* make `resume_fiber` private in `fiber.rs`
Signed-off-by: Joel Dice <[email protected]>
* Shrink `PollContext` state
Move management of the async guard range elsewhere to the normal
save/restore area.
* Refactor `AsyncCx`, reduce `unsafe`
* Remove the `AsyncCx` type from Wasmtime as it's inherently `unsafe` to
use, instead bundle operations directly on a `Store*` reference.
* Don't retain pointers-to-pointers within the roughly-equivalent
`BlockingContext` created in this PR. Instead when a blocking context
is created "take" the metadata from the store to assert exclusive
ownership of the pointers.
* Refactor how `&mut Context<'_>` is passed around, namely thread it
through fiber parameters to model resumption as registering a new
context to poll with.
* Remove `PollContext` in favor of directly storing a pointer as it's
now mostly an empty structure.
* Minor refactorings to make things more future-refactorable and/or
clear in a few places.
* Refactor management of the "current suspend" and "current future
context" pointers. These are now null'd out on resumption and asserted
null on suspension.
* Remove the need for a generic `Reset` structure in the fiber bits as
it's a pretty dangerous structure to have in general.
The end result of this refactoring is that all usage of `block_on` is
now safe and additionally many of the internals of the implementation
are safer than they were before
* Adjust some lint attributes
* Make manipulation of `AsyncState` safe
No need for raw pointers with recent refactorings.
* Fix dead code warning
* More dead code warnings
* Cut down on raw pointers in fiber.rs
* Move executor save/restore to normal fiber state save/restore
* Bikeshed a method name
* update comment in make_fiber
Signed-off-by: Joel Dice <[email protected]>
* fix machports build
Signed-off-by: Joel Dice <[email protected]>
---------
Signed-off-by: Joel Dice <[email protected]>
Co-authored-by: Alex Crichton <[email protected]>1 parent a6d1244 commit 421136d
File tree
19 files changed
+1211
-696
lines changed- benches
- crates/wasmtime/src
- runtime
- component
- func
- func
- store
- vm
- sys/unix
19 files changed
+1211
-696
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
134 | 134 | | |
135 | 135 | | |
136 | 136 | | |
137 | | - | |
138 | | - | |
| 137 | + | |
| 138 | + | |
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
| |||
628 | 628 | | |
629 | 629 | | |
630 | 630 | | |
631 | | - | |
| 631 | + | |
| 632 | + | |
632 | 633 | | |
633 | 634 | | |
634 | 635 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
| 38 | + | |
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
547 | 547 | | |
548 | 548 | | |
549 | 549 | | |
550 | | - | |
| 550 | + | |
551 | 551 | | |
552 | 552 | | |
553 | 553 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
179 | 179 | | |
180 | 180 | | |
181 | 181 | | |
182 | | - | |
| 182 | + | |
183 | 183 | | |
184 | 184 | | |
185 | 185 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
441 | 441 | | |
442 | 442 | | |
443 | 443 | | |
444 | | - | |
445 | | - | |
446 | | - | |
447 | | - | |
| 444 | + | |
| 445 | + | |
448 | 446 | | |
449 | 447 | | |
450 | 448 | | |
| |||
603 | 601 | | |
604 | 602 | | |
605 | 603 | | |
606 | | - | |
607 | | - | |
608 | | - | |
609 | | - | |
| 604 | + | |
| 605 | + | |
610 | 606 | | |
611 | | - | |
| 607 | + | |
612 | 608 | | |
613 | 609 | | |
614 | 610 | | |
| |||
676 | 672 | | |
677 | 673 | | |
678 | 674 | | |
679 | | - | |
680 | | - | |
681 | | - | |
682 | | - | |
683 | | - | |
684 | | - | |
| 675 | + | |
| 676 | + | |
685 | 677 | | |
686 | 678 | | |
687 | 679 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
31 | 38 | | |
32 | 39 | | |
33 | 40 | | |
| |||
0 commit comments