diff --git a/src/runtime2/component/component_context.rs b/src/runtime2/component/component_context.rs index 94dff5badfa7677ddbe3d4b2425ddff7366afc0a..4ee51581b3f46d0b8c43f278b58a8b3fc7e391cb 100644 --- a/src/runtime2/component/component_context.rs +++ b/src/runtime2/component/component_context.rs @@ -196,7 +196,7 @@ impl CompCtx { return Channel{ putter_id, getter_id }; } - /// Adds a new port. Make sure to call `add_peer` afterwards. + /// Adds a new port. Make sure to call `change_peer` afterwards. pub(crate) fn add_port(&mut self, peer_comp_id: CompId, peer_port_id: PortId, kind: PortKind, state: PortState) -> LocalPortHandle { let self_id = PortId(self.take_port_id()); self.ports.push(Port{ @@ -209,7 +209,28 @@ impl CompCtx { return LocalPortHandle(self_id); } - /// Removes a port. Make sure you called `remove_peer` first. + /// Adds a self-reference. Called by the runtime/scheduler + pub(crate) fn add_self_reference(&mut self, self_handle: CompHandle) { + debug_assert_eq!(self.id, self_handle.id()); + debug_assert!(self.get_peer_index_by_id(self.id).is_none()); + self.peers.push(Peer{ + id: self.id, + num_associated_ports: 0, + handle: self_handle + }); + } + + /// Removes a self-reference. Called by the runtime/scheduler + pub(crate) fn remove_self_reference(&mut self) -> Option { + let self_index = self.get_peer_index_by_id(self.id).unwrap(); + let peer = &mut self.peers[self_index]; + let maybe_comp_key = peer.handle.decrement_users(); + self.peers.remove(self_index); + + return maybe_comp_key; + } + + /// Removes a port. Make sure you called `change_peer` first. pub(crate) fn remove_port(&mut self, port_handle: LocalPortHandle) -> Port { let port_index = self.must_get_port_index(port_handle); let port = self.ports.remove(port_index);