Skip to main content

qualia_core_db/bundle/stream/runtime/state/
scope.rs

1//! Preflight -> MOVE -> existing engine -> unconditional restore -> unlock.
2use super::super::TransferRuntime;
3use super::{restore::Restore, RetainedRuntime, RuntimeAccess, RuntimeParents};
4use crate::net::qdnf::errors::QdnfError as E;
5impl<'host> RuntimeParents<'host> {
6    /// One bounded nonblocking poll; all parent locks are released on return.
7    /// No guard/borrow may escape. Never wait for peer or synchronous disk flush.
8    pub fn try_with_runtime<T>(
9        &self,
10        state: &mut RetainedRuntime<'_, '_>,
11        effect: impl for<'b, 'p> FnOnce(RuntimeAccess<'b, 'p>) -> Result<T, E>,
12    ) -> Result<T, E> {
13        self.with_runtime_raw(state, |runtime| {
14            effect(RuntimeAccess::new(
15                runtime,
16                self.ledger,
17                self.identities.ledger,
18            ))
19        })
20    }
21    pub(super) fn with_runtime_raw<T>(
22        &self,
23        state: &mut RetainedRuntime<'_, '_>,
24        effect: impl for<'p> FnOnce(&mut TransferRuntime<'p>) -> Result<T, E>,
25    ) -> Result<T, E> {
26        self.association(state)?;
27        if state.data.as_ref().ok_or(E::Incomplete)?.closed {
28            return Err(E::Closed);
29        }
30        let mut loans = self.loans()?;
31        state
32            .data
33            .as_ref()
34            .ok_or(E::Incomplete)?
35            .preflight(&loans)?;
36        // ALL fallible affinity/handle checks precede taking the single owner.
37        // Catch INSIDE guard lifetime: restore runs on unwind; actual mutex
38        // guards then drop normally before we resume the original panic payload.
39        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
40            let data = state.data.take().ok_or(E::Incomplete)?;
41            let runtime = data.attach(
42                &mut loans.ledger,
43                &mut loans.leases,
44                &mut loans.operations,
45                &mut loans.scheduler,
46                self.governor,
47            );
48            let mut restore = Restore::new(&mut state.data, runtime);
49            RuntimeAccess::new(restore.runtime(), self.ledger, self.identities.ledger)
50                .observe_cancellation()?;
51            effect(restore.runtime())
52        }));
53        drop(loans);
54        match result {
55            Ok(result) => result,
56            Err(payload) => std::panic::resume_unwind(payload),
57        }
58    }
59}
60impl RetainedRuntime<'_, '_> {
61    pub fn live_transfers(&self) -> Result<usize, E> {
62        Ok(self.data.as_ref().ok_or(E::Incomplete)?.live)
63    }
64    pub fn pending_schedule_records(&self) -> Result<usize, E> {
65        Ok(self.data.as_ref().ok_or(E::Incomplete)?.pending_records)
66    }
67    pub fn charged_window_bytes(&self) -> Result<u64, E> {
68        Ok(self.data.as_ref().ok_or(E::Incomplete)?.used_bytes)
69    }
70    /// Logical moved state footprint, not an assertion of generated memcpy count.
71    pub fn move_bytes_per_poll() -> usize {
72        2 * core::mem::size_of::<super::RuntimeData>()
73    }
74}