rust: Add support for inter-task wakeups #1440
Merged
+507
−137
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.
This commit fixes a preexisting issue in the async support in the rust
bindings where inter-component-model-task wakeups/communication did not
work. Previously
wit-bindgenwould end up either panicking or wedgingitself if this situation arose, effectively not handling it at all.
Inter-task wakeups are supported in the component model with unit
streams and futures (e.g.
futureandstreamwith no type parameter).For these types the component model allows the same instance to both
read/write from the stream or future. The support here is implemented
with
streamas a task might receive many wake up notifications overits lifetime.
Implementing this is a bit tricky, so the highlights of the
implementation are:
This relies on new "magic function imports" recognized by
wit-componentspecifically related to unit-streams and unit-futures.This is a very new feature of
wit-componentand thus hasn't had timeto propagate throughout the ecosystem yet. Due to the niche nature of
needing cross-task wakeups this commit disables cross-task wakeups by
default and adds support for it behind a Cargo feature. The thinking
is that if anyone runs into this there's now a clear panic/error
message indicating that the feature can be enabled. Otherwise the hope
is that no one runs into this (as it's relatively niche) and when the
time is right we can enable this by default.
All tasks started by wit-bindgen will need this new inter-task-wakeup
stream for the reason that we don't actually know what will wake a
task up when it blocks. To avoid adding a stream allocation on the
fast path, the allocation of the inter-task stream is deferred until
a task blocks for the first time.
Right now a read of the inter-task stream starts when a task blocks.
The read is cancelled when the task wakes up. A future optimization
would be to keep the read-in-progress and only restart it if
necessary. For now that's deferred, however.
Closes #1431