|
9 | 9 | use crate::component::{Component, Instance, InstancePre, ResourceType, RuntimeImport}; |
10 | 10 | use crate::module::ModuleRegistry; |
11 | 11 | use crate::runtime::component::ComponentInstanceId; |
| 12 | +#[cfg(feature = "component-model-async")] |
| 13 | +use crate::runtime::component::concurrent::ConcurrentInstanceState; |
12 | 14 | use crate::runtime::vm::instance::{InstanceLayout, OwnedInstance, OwnedVMContext}; |
13 | 15 | use crate::runtime::vm::vmcontext::VMFunctionBody; |
14 | 16 | use crate::runtime::vm::{ |
@@ -45,6 +47,35 @@ pub use self::handle_table::{TransmitLocalState, Waitable}; |
45 | 47 | pub use self::resources::CallContext; |
46 | 48 | pub use self::resources::{CallContexts, ResourceTables, TypedResource, TypedResourceIndex}; |
47 | 49 |
|
| 50 | +/// Represents the state of a (sub-)component instance. |
| 51 | +#[derive(Default)] |
| 52 | +pub struct InstanceState { |
| 53 | + /// Represents the Component Model Async state of a (sub-)component instance. |
| 54 | + #[cfg(feature = "component-model-async")] |
| 55 | + concurrent_state: ConcurrentInstanceState, |
| 56 | + |
| 57 | + /// State of handles (e.g. resources, waitables, etc.) for this instance. |
| 58 | + /// |
| 59 | + /// For resource handles, this is paired with other information to create a |
| 60 | + /// `ResourceTables` and manipulated through that. For other handles, this |
| 61 | + /// is used directly to translate guest handles to host representations and |
| 62 | + /// vice-versa. |
| 63 | + handle_table: HandleTable, |
| 64 | +} |
| 65 | + |
| 66 | +impl InstanceState { |
| 67 | + /// Represents the Component Model Async state of a (sub-)component instance. |
| 68 | + #[cfg(feature = "component-model-async")] |
| 69 | + pub fn concurrent_state(&mut self) -> &mut ConcurrentInstanceState { |
| 70 | + &mut self.concurrent_state |
| 71 | + } |
| 72 | + |
| 73 | + /// State of handles (e.g. resources, waitables, etc.) for this instance. |
| 74 | + pub fn handle_table(&mut self) -> &mut HandleTable { |
| 75 | + &mut self.handle_table |
| 76 | + } |
| 77 | +} |
| 78 | + |
48 | 79 | /// Runtime representation of a component instance and all state necessary for |
49 | 80 | /// the instance itself. |
50 | 81 | /// |
@@ -86,13 +117,9 @@ pub struct ComponentInstance { |
86 | 117 | // borrowing a store mutably at the same time as a contained instance. |
87 | 118 | component: Component, |
88 | 119 |
|
89 | | - /// State of handles (e.g. resources, waitables, etc.) for this component. |
90 | | - /// |
91 | | - /// For resource handles, this is paired with other information to create a |
92 | | - /// `ResourceTables` and manipulated through that. For other handles, this |
93 | | - /// is used directly to translate guest handles to host representations and |
94 | | - /// vice-versa. |
95 | | - instance_handle_tables: PrimaryMap<RuntimeComponentInstanceIndex, HandleTable>, |
| 120 | + /// Contains state specific to each (sub-)component instance within this |
| 121 | + /// top-level instance. |
| 122 | + instance_states: PrimaryMap<RuntimeComponentInstanceIndex, InstanceState>, |
96 | 123 |
|
97 | 124 | /// What all compile-time-identified core instances are mapped to within the |
98 | 125 | /// `Store` that this component belongs to. |
@@ -293,16 +320,15 @@ impl ComponentInstance { |
293 | 320 | ) -> OwnedComponentInstance { |
294 | 321 | let offsets = VMComponentOffsets::new(HostPtr, component.env_component()); |
295 | 322 | let num_instances = component.env_component().num_runtime_component_instances; |
296 | | - let mut instance_handle_tables = |
297 | | - PrimaryMap::with_capacity(num_instances.try_into().unwrap()); |
| 323 | + let mut instance_states = PrimaryMap::with_capacity(num_instances.try_into().unwrap()); |
298 | 324 | for _ in 0..num_instances { |
299 | | - instance_handle_tables.push(HandleTable::default()); |
| 325 | + instance_states.push(InstanceState::default()); |
300 | 326 | } |
301 | 327 |
|
302 | 328 | let mut ret = OwnedInstance::new(ComponentInstance { |
303 | 329 | id, |
304 | 330 | offsets, |
305 | | - instance_handle_tables, |
| 331 | + instance_states, |
306 | 332 | instances: PrimaryMap::with_capacity( |
307 | 333 | component |
308 | 334 | .env_component() |
@@ -825,22 +851,30 @@ impl ComponentInstance { |
825 | 851 | resource_instance == component.defined_resource_instances[idx] |
826 | 852 | } |
827 | 853 |
|
828 | | - /// Returns the runtime state of resources associated with this component. |
| 854 | + /// Returns the runtime state of resources and concurrency associated with |
| 855 | + /// this component. |
829 | 856 | #[inline] |
830 | | - pub fn guest_tables( |
| 857 | + pub fn instance_states( |
831 | 858 | self: Pin<&mut Self>, |
832 | 859 | ) -> ( |
833 | | - &mut PrimaryMap<RuntimeComponentInstanceIndex, HandleTable>, |
| 860 | + &mut PrimaryMap<RuntimeComponentInstanceIndex, InstanceState>, |
834 | 861 | &ComponentTypes, |
835 | 862 | ) { |
836 | 863 | // safety: we've chosen the `pin` guarantee of `self` to not apply to |
837 | 864 | // the map returned. |
838 | 865 | unsafe { |
839 | 866 | let me = self.get_unchecked_mut(); |
840 | | - (&mut me.instance_handle_tables, me.component.types()) |
| 867 | + (&mut me.instance_states, me.component.types()) |
841 | 868 | } |
842 | 869 | } |
843 | 870 |
|
| 871 | + pub fn instance_state( |
| 872 | + self: Pin<&mut Self>, |
| 873 | + instance: RuntimeComponentInstanceIndex, |
| 874 | + ) -> Option<&mut InstanceState> { |
| 875 | + self.instance_states().0.get_mut(instance) |
| 876 | + } |
| 877 | + |
844 | 878 | /// Returns the destructor and instance flags for the specified resource |
845 | 879 | /// table type. |
846 | 880 | /// |
|
0 commit comments