diff --git a/src/runtime2/mod.rs b/src/runtime2/mod.rs index 2acf1503e1dc5a26036832be06d56b3c97f74719..9b741ce49417d1f7933c8622bcac4f8c4c7ffe4a 100644 --- a/src/runtime2/mod.rs +++ b/src/runtime2/mod.rs @@ -21,11 +21,11 @@ use crate::collections::RawVec; use crate::ProtocolDescription; use inbox::Message; -use connector::{ConnectorPDL, ConnectorPublic, ConnectorScheduling, RunDeltaState}; -use scheduler::{Scheduler, ConnectorCtx, ControlMessageHandler}; +use connector::{ConnectorPDL, ConnectorPublic, ConnectorScheduling}; +use scheduler::{Scheduler, ControlMessageHandler}; use native::{Connector, ConnectorApplication, ApplicationInterface}; use crate::runtime2::port::{Port, PortState}; -use crate::runtime2::scheduler::SchedulerCtx; +use crate::runtime2::scheduler::{ComponentCtxFancy, SchedulerCtx}; /// A kind of token that, once obtained, allows mutable access to a connector. /// We're trying to use move semantics as much as possible: the owner of this @@ -78,24 +78,17 @@ pub(crate) enum ConnectorVariant { } impl Connector for ConnectorVariant { - fn handle_message(&mut self, message: Message, ctx: &ConnectorCtx, delta_state: &mut RunDeltaState) { + fn run(&mut self, scheduler_ctx: SchedulerCtx, comp_ctx: &mut ComponentCtxFancy) -> ConnectorScheduling { match self { - ConnectorVariant::UserDefined(c) => c.handle_message(message, ctx, delta_state), - ConnectorVariant::Native(c) => c.handle_message(message, ctx, delta_state), - } - } - - fn run(&mut self, scheduler_ctx: SchedulerCtx, conn_ctx: &ConnectorCtx, delta_state: &mut RunDeltaState) -> ConnectorScheduling { - match self { - ConnectorVariant::UserDefined(c) => c.run(scheduler_ctx, conn_ctx, delta_state), - ConnectorVariant::Native(c) => c.run(scheduler_ctx, conn_ctx, delta_state), + ConnectorVariant::UserDefined(c) => c.run(scheduler_ctx, comp_ctx), + ConnectorVariant::Native(c) => c.run(scheduler_ctx, comp_ctx), } } } pub(crate) struct ScheduledConnector { pub connector: ConnectorVariant, // access by connector - pub context: ConnectorCtx, // mutable access by scheduler, immutable by connector + pub ctx_fancy: ComponentCtxFancy, pub public: ConnectorPublic, // accessible by all schedulers and connectors pub router: ControlMessageHandler, pub shutting_down: bool, @@ -263,7 +256,8 @@ impl RuntimeInner { // --- Creating/retrieving/destroying components - pub(crate) fn create_interface_component(&self, component: ConnectorApplication) -> ConnectorKey { + /// Creates an initially sleeping application connector. + fn create_interface_component(&self, component: ConnectorApplication) -> ConnectorKey { // Initialize as sleeping, as it will be scheduled by the programmer. let mut lock = self.connectors.write().unwrap(); let key = lock.create(ConnectorVariant::Native(Box::new(component)), true); @@ -272,35 +266,17 @@ impl RuntimeInner { return key; } - /// Creates a new PDL component. The caller MUST make sure to schedule the - /// connector. - // TODO: Nicer code, not forcing the caller to schedule, perhaps? - pub(crate) fn create_pdl_component(&self, created_by: &mut ScheduledConnector, connector: ConnectorPDL) -> ConnectorKey { + /// Creates a new PDL component. This function just creates the component. + /// If you create it initially awake, then you must add it to the work + /// queue. Other aspects of correctness (i.e. setting initial ports) are + /// relinquished to the caller! + pub(crate) fn create_pdl_component(&self, connector: ConnectorPDL, initially_sleeping: bool) -> ConnectorKey { // Create as not sleeping, as we'll schedule it immediately let key = { let mut lock = self.connectors.write().unwrap(); - lock.create(ConnectorVariant::UserDefined(connector), false) + lock.create(ConnectorVariant::UserDefined(connector), initially_sleeping) }; - // Transfer the ports - { - let lock = self.connectors.read().unwrap(); - let created = lock.get_private(&key); - - match &created.connector { - ConnectorVariant::UserDefined(connector) => { - - println!("DEBUG: The connector {} owns the ports: {:?}", key.index, connector.ports.owned_ports.iter().map(|v| v.index).collect::>()); - for port_id in connector.ports.owned_ports.iter().copied() { - println!("DEBUG: Transferring port {:?} from {} to {}", port_id, created_by.context.id.0, key.index); - let port = created_by.context.remove_port(port_id); - created.context.add_port(port); - } - }, - ConnectorVariant::Native(_) => unreachable!(), - } - } - self.increment_active_components(); return key; } @@ -428,7 +404,7 @@ impl ConnectorStore { fn create(&mut self, connector: ConnectorVariant, initially_sleeping: bool) -> ConnectorKey { let mut connector = ScheduledConnector { connector, - context: ConnectorCtx::new(), + ctx_fancy: ComponentCtxFancy::new_empty(), public: ConnectorPublic::new(initially_sleeping), router: ControlMessageHandler::new(), shutting_down: false, @@ -441,7 +417,7 @@ impl ConnectorStore { // No free entries, allocate new entry index = self.connectors.len(); key = ConnectorKey{ index: index as u32 }; - connector.context.id = key.downcast(); + connector.ctx_fancy.id = key.downcast(); let connector = Box::into_raw(Box::new(connector)); self.connectors.push(connector); @@ -449,7 +425,7 @@ impl ConnectorStore { // Free spot available index = self.free.pop().unwrap(); key = ConnectorKey{ index: index as u32 }; - connector.context.id = key.downcast(); + connector.ctx_fancy.id = key.downcast(); unsafe { let target = self.connectors.get_mut(index);