// No ownership-layout change. The actual ThreadTable produces the nonclone // wait; the ProcessTable verifies the entire retained resource observation. /// # Safety /// sleep runs on the actual current process stack. It changes only that /// retained thread from Running to Sleeping and clears current identity. /// The returned Wake is the real single-use ThreadTable sleep episode owner. /// Before errors preserve all state and return no token. Once state changes, /// errors are After and retain any created token. No callback changes SP, /// CPU, root, interrupt masking, or any frame/space/stack owner. pub unsafe trait ProcessSleepBackend: ProcessWakeBackend { unsafe fn sleep(&mut self, bag: &mut Self::Bag) -> Result)>; } pub enum ProcessSleepFailure { Before(Error), Quarantined(Error, Option), } impl Table { /// # Safety /// The caller has actually landed on the current retained process stack. /// All mutable loans end before the assembly departure. After success it /// returns to the bootstrap stack immediately, without executing work as /// the sleeping process. An uncertain error retains the table and token; /// it must never authorize freeing or reusing the current stack. pub unsafe fn sleep_current_process(&mut self) -> Result> { let prepare = (|| { let execution = self.gate()?; let pid = execution.current.ok_or(Error::Context)?; if execution.kernel_bootstrap { return Err(Error::Context); } let index = self.public_index(pid)?; if self.entries[index].phase != Phase::Published { return Err(Error::State); } let before = self.record(pid)?.resources; if before.state != threads::State::Running { return Err(Error::State); } if self.backend().execution() != execution { return Err(Error::Context); } Ok((index, before, execution)) })(); let (index, before, execution) = match prepare { Ok(value) => value, Err(error) => return Err(ProcessSleepFailure::Before(error)), }; let result = unsafe { self.backend.as_mut().expect("owned backend").sleep( self.entries[index].bag.as_mut().expect("validated bag")) }; let after = self.backend().observe(self.entries[index].bag.as_ref().expect("retained bag")); let context = self.backend().execution(); match result { Ok(token) => { let mut expected = before; expected.state = threads::State::Sleeping; let mut departed = execution; departed.current = None; if context != departed || after != Ok(expected) { self.entries[index].phase = Phase::Quarantined; return Err(ProcessSleepFailure::Quarantined( if context == departed { Error::Resource } else { Error::Context }, Some(token))); } Ok(token) }, Err((failure, token)) => { if failure.boundary == Boundary::Before && token.is_none() && context == execution && after == Ok(before) { Err(ProcessSleepFailure::Before(failure.error)) } else { self.entries[index].phase = Phase::Quarantined; Err(ProcessSleepFailure::Quarantined( if context == execution { failure.error } else { Error::Context }, token)) } }, } } }