diff --git a/src/runtime2/global_store.rs b/src/runtime2/global_store.rs index 3b62801458e6cdc055fd273866a2f921da1e3ed0..4e7b9f621ed03fe587d80c4186d72249417ca598 100644 --- a/src/runtime2/global_store.rs +++ b/src/runtime2/global_store.rs @@ -1,18 +1,16 @@ use std::ptr; -use std::sync::{Arc, Barrier, RwLock, RwLockReadGuard}; +use std::sync::{Arc, RwLock}; use std::sync::atomic::{AtomicBool, AtomicU32}; use crate::collections::{MpmcQueue, RawVec}; use super::connector::{ConnectorPDL, ConnectorPublic}; -use super::port::{PortIdLocal, Port, PortKind, PortOwnership, Channel}; -use super::inbox::PublicInbox; use super::scheduler::Router; use crate::ProtocolDescription; use crate::runtime2::connector::{ConnectorScheduling, RunDeltaState}; -use crate::runtime2::inbox::{DataMessage, MessageContents, SyncMessage}; -use crate::runtime2::native::Connector; +use crate::runtime2::inbox::MessageContents; +use crate::runtime2::native::{Connector, ConnectorApplication}; use crate::runtime2::scheduler::ConnectorCtx; /// A kind of token that, once obtained, allows mutable access to a connector. @@ -40,7 +38,7 @@ impl ConnectorKey { /// A kind of token that allows shared access to a connector. Multiple threads /// may hold this -#[derive(Copy, Clone)] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] pub(crate) struct ConnectorId(pub u32); impl ConnectorId { @@ -121,7 +119,7 @@ impl ConnectorStore { unsafe { let connector = lock.connectors.get(connector_id.0 as usize); debug_assert!(!connector.is_null()); - return &*connector.public; + return &(**connector).public; } } @@ -133,16 +131,56 @@ impl ConnectorStore { unsafe { let connector = lock.connectors.get_mut(key.index as usize); debug_assert!(!connector.is_null()); - return *connector as &mut _; + return &mut (**connector); } } + pub(crate) fn create_interface(&self, connector: ConnectorApplication) -> ConnectorKey { + // Connector interface does not own any initial ports, and cannot be + // created by another connector + let key = self.create_connector_raw(ConnectorVariant::Native(Box::new(connector))); + return key; + } + /// Create a new connector, returning the key that can be used to retrieve /// and/or queue it. The caller must make sure that the constructed /// connector's code is initialized with the same ports as the ports in the /// `initial_ports` array. - pub(crate) fn create(&self, created_by: &mut ScheduledConnector, connector: ConnectorVariant, initial_ports: Vec) -> ConnectorKey { + pub(crate) fn create_pdl(&self, created_by: &mut ScheduledConnector, connector: ConnectorPDL) -> ConnectorKey { + let key = self.create_connector_raw(ConnectorVariant::UserDefined(connector)); + let new_connector = self.get_mut(&key); + + // Transferring ownership of ports (and crashing if there is a + // programmer's mistake in port management) + match &new_connector.connector { + ConnectorVariant::UserDefined(connector) => { + for port_id in &connector.ports.owned_ports { + let mut port = created_by.context.remove_port(*port_id); + new_connector.context.add_port(port); + } + }, + ConnectorVariant::Native(_) => unreachable!(), + } + + return key; + } + + pub(crate) fn destroy(&self, key: ConnectorKey) { + let lock = self.inner.write().unwrap(); + + unsafe { + let connector = lock.connectors.get_mut(key.index as usize); + ptr::drop_in_place(*connector); + // Note: but not deallocating! + } + + lock.free.push(key.index as usize); + } + + /// Creates a connector but does not set its initial ports + fn create_connector_raw(&self, connector: ConnectorVariant) -> ConnectorKey { // Creation of the connector in the global store, requires a lock + let index; { let lock = self.inner.write().unwrap(); let connector = ScheduledConnector { @@ -152,7 +190,6 @@ impl ConnectorStore { router: Router::new(), }; - let index; if lock.free.is_empty() { let connector = Box::into_raw(Box::new(connector)); @@ -172,37 +209,14 @@ impl ConnectorStore { } } - // Setting of new connector's ID + // Generate key and retrieve the connector to set its ID let key = ConnectorKey{ index: index as u32 }; let new_connector = self.get_mut(&key); new_connector.context.id = key.downcast(); - // Transferring ownership of ports (and crashing if there is a - // programmer's mistake in port management) - match &new_connector.connector { - ConnectorVariant::UserDefined(connector) => { - for port_id in &connector.ports.owned_ports { - let mut port = created_by.context.remove_port(*port_id); - new_connector.context.add_port(port); - } - }, - ConnectorVariant::Native(_) => {}, // no initial ports (yet!) - } - + // Return the connector key return key; } - - pub(crate) fn destroy(&self, key: ConnectorKey) { - let lock = self.inner.write().unwrap(); - - unsafe { - let connector = lock.connectors.get_mut(key.index as usize); - ptr::drop_in_place(*connector); - // Note: but not deallocating! - } - - lock.free.push(key.index as usize); - } } impl Drop for ConnectorStore {