Skip to main content

tembra_core/host/orbs/
gate.rs

1//! Admit retained roots while blocking incompatible new work.
2
3use super::*;
4
5impl Host {
6    /// Stops first access by old roots while allowing already admitted roots to reenter.
7    /// Returns true only after their complete roots and owned tasks physically settle. Ending or
8    /// cancelling a root does not release its lease while a callback is still running.
9    pub fn gate(&self, ticket: Id, objects: &[Id]) -> Result<bool> {
10        let mut inner = self.inner.borrow_mut();
11        let Inner {
12            pending,
13            objects: records,
14            object_roots,
15            ..
16        } = &mut *inner;
17        let pending = pending
18            .as_mut()
19            .filter(|p| p.reload.id == ticket)
20            .ok_or_else(|| conflict(Reason::ReloadTicket))?;
21        if objects.iter().any(|id| !pending.objects.contains(id)) {
22            return Err(Error::invalid(Reason::GateObject, "object is not affected"));
23        }
24        for id in objects {
25            records
26                .get_mut(id)
27                .ok_or_else(|| gone(Reason::Object))?
28                .gate
29                .get_or_insert_with(|| object_roots.members(id).collect());
30        }
31        pending.gated.extend(objects);
32        Ok(objects.iter().all(|id| !inner.gate_object_busy(*id)))
33    }
34
35    /// Drains an exclusive orb-owned resource, including hook invocations without an object.
36    pub fn gate_orbs(&self, ticket: Id, owners: &[Id]) -> Result<bool> {
37        let mut inner = self.inner.borrow_mut();
38        let Inner {
39            pending, orb_roots, ..
40        } = &mut *inner;
41        let pending = pending
42            .as_mut()
43            .filter(|pending| pending.reload.id == ticket)
44            .ok_or_else(|| conflict(Reason::ReloadTicket))?;
45        if owners.iter().any(|owner| !pending.owners.contains(owner)) {
46            return Err(Error::invalid(Reason::GateOrb, "orb is not affected"));
47        }
48        for owner in owners {
49            pending
50                .gated_orbs
51                .entry(*owner)
52                .or_insert_with(|| orb_roots.members(owner).collect());
53        }
54        Ok(owners.iter().all(|owner| !inner.orb_busy(*owner)))
55    }
56}