use core::arch::asm;
use core::iter::FusedIterator;
use core::mem::size_of;
use core::sync::atomic::{AtomicU32, Ordering};
use crate::MAX_CPUS;
use super::int::lapic::LAPIC;
static ONLINE_MASK: AtomicU32 = AtomicU32::new(0);
const _: () = assert!(MAX_CPUS <= size_of::<AtomicU32>() * 8);
pub fn halt() {
unsafe { asm!("hlt", options(nomem, nostack)) };
}
pub fn init(core: usize) {
ONLINE_MASK.fetch_or(1 << core, Ordering::AcqRel);
}
pub fn id() -> usize {
LAPIC.id() as _
}
pub fn online() -> u32 {
ONLINE_MASK.load(Ordering::Acquire)
}
pub fn iter() -> impl Iterator<Item = usize> {
let mask = ONLINE_MASK.load(Ordering::Acquire);
(0..MAX_CPUS).filter(move |i| mask & (1 << i) != 0)
}