pub(super) struct ThreadOwners { table: Table, hardware: Rc>, next: u64, } impl ThreadOwners { pub(super) fn new() -> Self { let (table, hardware) = setup(); Self { table, hardware, next: 0, } } pub(super) fn create(&mut self) -> ThreadId { let id = spawn(&mut self.table, self.next); self.next += 1; id } pub(super) fn record(&self, id: ThreadId) -> ThreadRecord { self.table.record(id).unwrap() } pub(super) fn activate(&mut self, id: ThreadId) { enter(&mut self.table, &self.hardware, id); } pub(super) fn yield_current(&mut self, id: ThreadId) { self.table.yield_current(id).unwrap(); self.hardware.borrow_mut().sp = 0; } pub(super) fn exit(&mut self, id: ThreadId) { if self.record(id).state == State::Running { self.table.exit_current(id).unwrap(); self.hardware.borrow_mut().sp = 0; } else { self.table.cancel(id).unwrap(); } } pub(super) fn reap(&mut self, id: ThreadId) { if self.record(id).state != State::Dead { self.exit(id); } self.table.reap(id).unwrap(); } pub(super) fn live(&self) -> usize { self.table.snapshot().live } }