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

Commit 3a644f0

Browse files
committed
Refactor how block_on is called and used
1 parent f09013d commit 3a644f0

2 files changed

Lines changed: 41 additions & 117 deletions

File tree

crates/wasmtime/src/runtime/store.rs

Lines changed: 16 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,33 +1126,9 @@ impl<T> StoreInner<T> {
11261126
CallHookInner::Sync(hook) => hook((&mut *self).as_context_mut(), s),
11271127

11281128
#[cfg(all(feature = "async", feature = "call-hook"))]
1129-
CallHookInner::Async(handler) => unsafe {
1130-
#[cfg(feature = "component-model-async")]
1131-
{
1132-
let async_cx = crate::component::concurrent::AsyncCx::try_new(&mut self.inner)
1133-
.ok_or_else(|| anyhow!("couldn't grab async_cx for call hook"))?;
1134-
1135-
async_cx
1136-
.block_on(
1137-
handler
1138-
.handle_call_event((&mut *self).as_context_mut(), s)
1139-
.as_mut(),
1140-
None,
1141-
)?
1142-
.0
1143-
}
1144-
#[cfg(not(feature = "component-model-async"))]
1145-
{
1146-
self.inner
1147-
.async_cx()
1148-
.ok_or_else(|| anyhow!("couldn't grab async_cx for call hook"))?
1149-
.block_on(
1150-
handler
1151-
.handle_call_event((&mut *self).as_context_mut(), s)
1152-
.as_mut(),
1153-
)?
1154-
}
1155-
},
1129+
CallHookInner::Async(handler) => {
1130+
self.block_on(|store| handler.handle_call_event(StoreContextMut(store), s))?
1131+
}
11561132

11571133
CallHookInner::ForceTypeParameterToBeUsed { uninhabited, .. } => {
11581134
let _ = s;
@@ -2047,36 +2023,12 @@ unsafe impl<T> crate::runtime::vm::VMStore for StoreInner<T> {
20472023
limiter(&mut self.data).memory_growing(current, desired, maximum)
20482024
}
20492025
#[cfg(feature = "async")]
2050-
Some(ResourceLimiterInner::Async(ref mut limiter)) => unsafe {
2051-
#[cfg(feature = "component-model-async")]
2052-
{
2053-
_ = limiter;
2054-
let async_cx =
2055-
crate::component::concurrent::AsyncCx::new(&mut (&mut *self).inner);
2056-
let Some(ResourceLimiterInner::Async(ref mut limiter)) = self.limiter else {
2057-
unreachable!();
2058-
};
2059-
async_cx
2060-
.block_on(
2061-
limiter(&mut self.data)
2062-
.memory_growing(current, desired, maximum)
2063-
.as_mut(),
2064-
None,
2065-
)?
2066-
.0
2067-
}
2068-
#[cfg(not(feature = "component-model-async"))]
2069-
{
2070-
self.inner
2071-
.async_cx()
2072-
.expect("ResourceLimiterAsync requires async Store")
2073-
.block_on(
2074-
limiter(&mut self.data)
2075-
.memory_growing(current, desired, maximum)
2076-
.as_mut(),
2077-
)?
2078-
}
2079-
},
2026+
Some(ResourceLimiterInner::Async(_)) => self.block_on(|store| {
2027+
let Some(ResourceLimiterInner::Async(limiter)) = &mut store.limiter else {
2028+
unreachable!();
2029+
};
2030+
limiter(&mut store.data).memory_growing(current, desired, maximum)
2031+
})?,
20802032
None => Ok(true),
20812033
}
20822034
}
@@ -2103,50 +2055,17 @@ unsafe impl<T> crate::runtime::vm::VMStore for StoreInner<T> {
21032055
desired: usize,
21042056
maximum: Option<usize>,
21052057
) -> Result<bool, anyhow::Error> {
2106-
// Need to borrow async_cx before the mut borrow of the limiter.
2107-
// self.async_cx() panicks when used with a non-async store, so
2108-
// wrap this in an option.
2109-
#[cfg(all(feature = "async", not(feature = "component-model-async")))]
2110-
let async_cx = if self.async_support()
2111-
&& matches!(self.limiter, Some(ResourceLimiterInner::Async(_)))
2112-
{
2113-
Some(self.async_cx().unwrap())
2114-
} else {
2115-
None
2116-
};
2117-
21182058
match self.limiter {
21192059
Some(ResourceLimiterInner::Sync(ref mut limiter)) => {
21202060
limiter(&mut self.data).table_growing(current, desired, maximum)
21212061
}
21222062
#[cfg(feature = "async")]
2123-
Some(ResourceLimiterInner::Async(ref mut limiter)) => unsafe {
2124-
#[cfg(feature = "component-model-async")]
2125-
{
2126-
_ = limiter;
2127-
let async_cx =
2128-
crate::component::concurrent::AsyncCx::new(&mut (&mut *self).inner);
2129-
let Some(ResourceLimiterInner::Async(ref mut limiter)) = self.limiter else {
2130-
unreachable!();
2131-
};
2132-
async_cx
2133-
.block_on(
2134-
limiter(&mut self.data)
2135-
.table_growing(current, desired, maximum)
2136-
.as_mut(),
2137-
None,
2138-
)?
2139-
.0
2140-
}
2141-
#[cfg(not(feature = "component-model-async"))]
2142-
{
2143-
async_cx
2144-
.expect("ResourceLimiterAsync requires async Store")
2145-
.block_on(
2146-
limiter(&mut self.data).table_growing(current, desired, maximum),
2147-
)?
2148-
}
2149-
},
2063+
Some(ResourceLimiterInner::Async(_)) => self.block_on(|store| {
2064+
let Some(ResourceLimiterInner::Async(limiter)) = &mut store.limiter else {
2065+
unreachable!();
2066+
};
2067+
limiter(&mut store.data).table_growing(current, desired, maximum)
2068+
})?,
21502069
None => Ok(true),
21512070
}
21522071
}
@@ -2213,11 +2132,7 @@ unsafe impl<T> crate::runtime::vm::VMStore for StoreInner<T> {
22132132
// to clean up this fiber. Do so by raising a trap which will
22142133
// abort all wasm and get caught on the other side to clean
22152134
// things up.
2216-
unsafe {
2217-
self.async_cx()
2218-
.expect("attempted to pull async context during shutdown")
2219-
.block_on(future)?
2220-
}
2135+
self.block_on(|_| future)?;
22212136
delta
22222137
}
22232138
};

crates/wasmtime/src/runtime/store/async_.rs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -203,29 +203,14 @@ impl<T> StoreInner<T> {
203203
/// This only works on async futures and stores, and assumes that we're
204204
/// executing on a fiber. This will yield execution back to the caller once.
205205
pub fn async_yield_impl(&mut self) -> Result<()> {
206-
use crate::runtime::vm::Yield;
207-
208-
let mut future = Yield::new();
209-
210206
// When control returns, we have a `Result<()>` passed
211207
// in from the host fiber. If this finished successfully then
212208
// we were resumed normally via a `poll`, so keep going. If
213209
// the future was dropped while we were yielded, then we need
214210
// to clean up this fiber. Do so by raising a trap which will
215211
// abort all wasm and get caught on the other side to clean
216212
// things up.
217-
#[cfg(feature = "component-model-async")]
218-
unsafe {
219-
let async_cx = crate::component::concurrent::AsyncCx::new(&mut (&mut *self).inner);
220-
async_cx.block_on(Pin::new_unchecked(&mut future), None)?.0;
221-
Ok(())
222-
}
223-
#[cfg(not(feature = "component-model-async"))]
224-
unsafe {
225-
self.async_cx()
226-
.expect("attempted to pull async context during shutdown")
227-
.block_on(Pin::new_unchecked(&mut future))
228-
}
213+
self.block_on(|_| crate::runtime::vm::Yield::new())
229214
}
230215

231216
#[cfg(target_has_atomic = "64")]
@@ -237,6 +222,30 @@ impl<T> StoreInner<T> {
237222
self.epoch_deadline_behavior =
238223
Some(Box::new(move |_store| Ok(UpdateDeadline::Yield(delta))));
239224
}
225+
226+
pub(crate) fn block_on<'a, F: Future + Send>(
227+
&'a mut self,
228+
mk_future: impl FnOnce(&'a mut Self) -> F,
229+
) -> Result<F::Output> {
230+
#[cfg(feature = "component-model-async")]
231+
{
232+
let async_cx = crate::component::concurrent::AsyncCx::try_new(self)
233+
.expect("couldn't create AsyncCx to block on async operation");
234+
let future = mk_future(self);
235+
let mut future = core::pin::pin!(future);
236+
unsafe { Ok(async_cx.block_on(future.as_mut(), None)?.0) }
237+
}
238+
#[cfg(not(feature = "component-model-async"))]
239+
{
240+
let cx = self
241+
.inner
242+
.async_cx()
243+
.expect("async_cx is not present to block on async operation");
244+
let future = mk_future(self);
245+
let mut future = core::pin::pin!(future);
246+
unsafe { cx.block_on(future.as_mut()) }
247+
}
248+
}
240249
}
241250

242251
#[doc(hidden)]

0 commit comments

Comments
 (0)