diff --git a/src/runtime2/runtime.rs b/src/runtime2/runtime.rs index 9b4c2ad4eda55e0eaf97a0b220c8417f253acc8e..8265a8c8b88e2c94d7279d3a96f3ec595a8e1b65 100644 --- a/src/runtime2/runtime.rs +++ b/src/runtime2/runtime.rs @@ -3,10 +3,9 @@ use std::sync::atomic::{AtomicU32, AtomicBool, Ordering}; use std::collections::VecDeque; use crate::protocol::*; -use crate::runtime2::component::wake_up_if_sleeping; use super::communication::Message; -use super::component::{CompCtx, CompPDL}; +use super::component::{wake_up_if_sleeping, CompPDL, CompCtx}; use super::store::{ComponentStore, ComponentReservation, QueueDynMpsc, QueueDynProducer}; use super::scheduler::*; @@ -107,12 +106,17 @@ impl CompHandle { debug_assert!(old_count > 0); // because we should never be able to retrieve a handle when the component is (being) destroyed } - /// Returns true if the component should be destroyed - pub(crate) fn decrement_users(&mut self) -> bool { + /// Returns the `CompKey` to the component if it should be destroyed + pub(crate) fn decrement_users(&mut self) -> Option { debug_assert!(!self.decremented, "illegal to 'decrement_users' twice"); dbg_code!(self.decremented = true); let old_count = self.num_handles.fetch_sub(1, Ordering::AcqRel); - return old_count == 1; + let new_count = old_count - 1; + if new_count == 0 { + return Some(unsafe{ self.id.upgrade() }); + } + + return None; } } @@ -132,6 +136,7 @@ impl std::ops::Deref for CompHandle { type Target = CompPublic; fn deref(&self) -> &Self::Target { + debug_assert!(!self.decremented); // cannot access if control is relinquished return unsafe{ &*self.target }; } }