//! A CPU online owner retained with the previously accepted local Queue. use crate::runtime::percpu_queue::{self as rq,Backend,Class,Disposition,Handle,Queue,Selection}; #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Binding{pub issuer:u64,pub epoch:u64,pub cpu:u32,pub generation:u64,pub online:bool} #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Execution{pub cpu:u32,pub masked:bool,pub in_irq:bool} #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Error{Configuration,Cpu,State,Stale,Queue(rq::Error)} /// # Safety /// This non-cloneable owner contains the exact lifecycle Online token for the /// binding's validated CPU and generation. It excludes a second local queue and /// offline admission while retained. Execution reads genuine CPU/IRQ state. /// Its destructor never releases CPU, task or stack resources. A returned owner /// and backend must remain owned until all backend task/context/stack pins have /// been released and the lifecycle owner has observed actual quiescence. pub unsafe trait OnlineOwner{fn binding(&self)->Binding;fn execution(&self)->Execution;} pub struct Local{owner:P,binding:Binding,queue:Queue} impl Local{ pub fn new(owner:P,backend:B)->Result{ let binding=owner.binding();if binding.issuer==0||binding.epoch==0||binding.generation==0||!binding.online{return Err((Error::Configuration,owner,backend))} let e=owner.execution();let b=backend.execution();if e.cpu!=binding.cpu||b.cpu!=binding.cpu{return Err((Error::Cpu,owner,backend))}if !e.masked||e.in_irq{return Err((Error::State,owner,backend))} match Queue::new(backend){Ok(queue)=>Ok(Self{owner,binding,queue}),Err((error,backend))=>Err((Error::Queue(error),owner,backend))} } fn guard(&self)->Result<(),Error>{if self.owner.binding()!=self.binding{return Err(Error::Stale)}let e=self.owner.execution();if e.cpu!=self.binding.cpu{return Err(Error::Cpu)}if !e.masked||e.in_irq{return Err(Error::State)}Ok(())} pub fn binding(&self)->Binding{self.binding} pub fn audit(&self)->Result<(),Error>{self.guard()?;self.queue.audit().map_err(Error::Queue)} pub fn enqueue(&mut self,member:B::Member,class:Class)->Result{if let Err(e)=self.guard(){return Err((e,member))}self.queue.enqueue(member,class).map_err(|(e,m)|(Error::Queue(e),m))} pub fn requeue(&mut self,h:Handle)->Result<(),Error>{self.guard()?;self.queue.requeue(h).map_err(Error::Queue)} pub fn request_priority(&mut self,h:Handle,class:u8)->Result<(),Error>{self.guard()?;self.queue.request_priority(h,class).map_err(Error::Queue)} pub fn select(&mut self,disposition:Disposition)->Result,Error>{self.guard()?;self.queue.select(disposition).map_err(Error::Queue)} pub fn renew(&mut self)->Result{self.guard()?;self.queue.renew().map_err(Error::Queue)} pub fn charge(&mut self,tick:B::Tick)->Result{if let Err(e)=self.guard(){return Err((e,tick))}self.queue.charge(tick).map_err(|(e,t)|(Error::Queue(e),t))} pub fn withdraw(&mut self,h:Handle)->Result{self.guard()?;self.queue.withdraw(h).map_err(Error::Queue)} pub fn record(&self,h:Handle)->Result{self.guard()?;self.queue.record(h).map_err(Error::Queue)} pub fn snapshot(&self)->Result{self.guard()?;self.queue.audit().map_err(Error::Queue)?;Ok(self.queue.snapshot())} pub fn ready_order(&self)->Result<[Option;N],Error>{self.guard()?;self.queue.audit().map_err(Error::Queue)?;Ok(self.queue.ready_order())} pub fn with_backend_observation(&self,f:impl FnOnce(&B)->R)->Result{self.guard()?;Ok(self.queue.with_backend_observation(f))} /// Returns both ownership chains together; this does not prove CPU quiescence. /// The caller must still tear down retained backend thread/context resources. pub fn into_parts(self)->Result<(P,B),(Error,Self)>{if let Err(e)=self.guard(){return Err((e,self))}let Self{owner,binding,queue}=self;match queue.into_backend(){Ok(backend)=>Ok((owner,backend)),Err((error,queue))=>Err((Error::Queue(error),Self{owner,binding,queue}))}} }