use cpu::{CpuAffinityError, current_thread_id, for_each_online_cpu, set_current_thread_affinity};

fn main() -> Result<(), CpuAffinityError> {
    let cpu = first_online_cpu()?;
    let thread_id = current_thread_id()?;

    set_current_thread_affinity([cpu])?;
    println!("pinned current thread {thread_id} to cpu {cpu}");

    Ok(())
}

fn first_online_cpu() -> Result<usize, CpuAffinityError> {
    let mut first = None;
    for_each_online_cpu(|cpu| {
        first.get_or_insert(cpu);
    })?;
    first.ok_or(CpuAffinityError::EmptyCpuList)
}
