Skip to content

Commit 52a449e

Browse files
committed
call InstanceFlags::set_may_enter where appropriate
There's still more work to do to fully implement (and test) the reentrance rules for concurrent tasks, but this is a start. Signed-off-by: Joel Dice <joel.dice@fermyon.com>
1 parent 53f076f commit 52a449e

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

crates/wasmtime/src/runtime/component/concurrent.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use {
33
component::func::{self, Func, Lower as _, LowerContext, Options},
44
vm::{
55
component::{
6-
CallContext, ComponentInstance, ResourceTables, VMComponentContext, WaitableState,
6+
CallContext, ComponentInstance, InstanceFlags, ResourceTables, VMComponentContext,
7+
WaitableState,
78
},
89
mpk::{self, ProtectionMask},
910
AsyncWasmCallState, PreviousAsyncWasmCallState, SendSyncPtr, VMFuncRef,
@@ -771,14 +772,25 @@ fn maybe_send_event<'a, T>(
771772

772773
maybe_push_call_context(&mut store, guest_task)?;
773774

775+
let mut flags = unsafe {
776+
(*store
777+
.concurrent_state()
778+
.component_instance
779+
.unwrap()
780+
.as_ptr())
781+
.instance_flags(callback.instance)
782+
};
783+
774784
let params = &mut [
775785
ValRaw::u32(callback.context),
776786
ValRaw::u32(event as u32),
777787
ValRaw::u32(handle),
778788
ValRaw::u32(result),
779789
];
780790
unsafe {
791+
flags.set_may_enter(false);
781792
crate::Func::call_unchecked_raw(&mut store, callback.function.as_non_null(), params)?;
793+
flags.set_may_enter(true);
782794
}
783795

784796
maybe_pop_call_context(&mut store, guest_task)?;
@@ -1733,6 +1745,7 @@ fn make_call<T>(
17331745
callee: SendSyncPtr<VMFuncRef>,
17341746
param_count: usize,
17351747
result_count: usize,
1748+
flags: Option<InstanceFlags>,
17361749
) -> impl FnOnce(
17371750
StoreContextMut<T>,
17381751
) -> Result<([MaybeUninit<ValRaw>; MAX_FLAT_PARAMS], StoreContextMut<T>)>
@@ -1753,11 +1766,17 @@ fn make_call<T>(
17531766
let mut cx = unsafe { StoreContextMut::<T>(&mut *cx.cast()) };
17541767

17551768
unsafe {
1769+
if let Some(mut flags) = flags {
1770+
flags.set_may_enter(false);
1771+
}
17561772
crate::Func::call_unchecked_raw(
17571773
&mut cx,
17581774
callee.as_non_null(),
17591775
&mut storage[..param_count.max(result_count)] as *mut [MaybeUninit<ValRaw>] as _,
17601776
)?;
1777+
if let Some(mut flags) = flags {
1778+
flags.set_may_enter(true);
1779+
}
17611780
}
17621781

17631782
Ok((storage, cx))
@@ -1863,9 +1882,7 @@ fn do_start_call<'a, T>(
18631882
})?;
18641883
let mut cx = unsafe { StoreContextMut::<T>(&mut *cx.cast()) };
18651884

1866-
unsafe {
1867-
flags.set_needs_post_return(false);
1868-
}
1885+
unsafe { flags.set_needs_post_return(false) }
18691886

18701887
if let Some(func) = post_return {
18711888
let arg = match result_count {
@@ -1882,9 +1899,7 @@ fn do_start_call<'a, T>(
18821899
}
18831900
}
18841901

1885-
unsafe {
1886-
flags.set_may_enter(true);
1887-
}
1902+
unsafe { flags.set_may_enter(true) }
18881903

18891904
let (calls, host_table, _) = cx.0.component_resource_state();
18901905
ResourceTables {
@@ -2003,7 +2018,17 @@ pub(crate) extern "C" fn async_exit<T>(
20032018
let result_count = usize::try_from(result_count).unwrap();
20042019
assert!(result_count <= MAX_FLAT_RESULTS);
20052020

2006-
let call = make_call(guest_task, callee, param_count, result_count);
2021+
let call = make_call(
2022+
guest_task,
2023+
callee,
2024+
param_count,
2025+
result_count,
2026+
if callback.is_null() {
2027+
None
2028+
} else {
2029+
Some((*instance).instance_flags(callee_instance))
2030+
},
2031+
);
20072032

20082033
let (guest_context, new_cx) = do_start_call(
20092034
cx,
@@ -2093,17 +2118,22 @@ pub(crate) fn start_call<'a, T: Send, LowerParams: Copy, R: 'static>(
20932118

20942119
log::trace!("starting call {}", guest_task.rep());
20952120

2121+
let instance = store.0[instance.0].as_ref().unwrap().instance_ptr();
2122+
20962123
let call = make_call(
20972124
guest_task,
20982125
SendSyncPtr::new(callee),
20992126
mem::size_of::<LowerParams>() / mem::size_of::<ValRaw>(),
21002127
1,
2128+
if callback.is_none() {
2129+
None
2130+
} else {
2131+
Some(unsafe { (*instance).instance_flags(component_instance) })
2132+
},
21012133
);
21022134

21032135
store.concurrent_state().guest_task = Some(guest_task);
21042136

2105-
let instance = store.0[instance.0].as_ref().unwrap().instance_ptr();
2106-
21072137
store = do_start_call(
21082138
store,
21092139
instance,

0 commit comments

Comments
 (0)