syd 3.58.0

rock-solid application kernel
Documentation
//
// Syd: rock-solid application kernel
// src/cpu.rs: Get CPU count using sched_getaffinity(2)
//
// Copyright (c) 2026 Ali Polatel <alip@chesswob.org>
//
// SPDX-License-Identifier: GPL-3.0

//! Get CPU count using sched_getaffinity(2)

use nix::{
    errno::Errno,
    sched::{sched_getaffinity, CpuSet},
    unistd::Pid,
};

/// Number of logical CPUs calling process may run on.
pub fn get() -> Result<usize, Errno> {
    let set = sched_getaffinity(Pid::from_raw(0))?;

    let mut count = 0usize;
    for cpu in 0..CpuSet::count() {
        if set.is_set(cpu)? {
            count = count.saturating_add(1);
        }
    }

    if count == 0 {
        return Err(Errno::ENODATA);
    }
    Ok(count)
}