Cotton code
102 lines · 3.8 KB · RustRaw
//! WHICH BAND THE CALLING THREAD RUNS IN: the QoS class it asked for and the//! base Mach priority the kernel granted.//!//! The two differ when the process is clamped from outside: on 12 Oct 2026//! every thread of the serving app ran at priority 20, the utility band, and//! its reads ran 10–23x slower than the same statement at the default class//! whenever the Mac was building (`cotton-ffi`'s `shell/serving_class.rs` has//! the measurements). A slow hold's line names its thread's band//! ([`crate::held::slow_hold_line`]), so a hold slow for that reason says so./// What a thread asked for and what the kernel gave it.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub struct ThreadClass {    /// The QoS class the thread requested: `user-initiated`, `utility`, …    pub class: &'static str,    /// The thread's base Mach priority — 31 is the default band, 37 and up a    /// person's own work, 20 utility, 4 background. `None` off Apple.    pub priority: Option<i32>,}/// The base priority at and above which a thread is NOT held below the/// default band. Utility is 20, background 4; default and up are 31+.pub const DEFAULT_BAND: i32 = 31;impl ThreadClass {    /// Whether the kernel holds this thread below the default band.    pub fn below_default(&self) -> bool {        self.priority.is_some_and(|priority| priority < DEFAULT_BAND)    }    /// `user-initiated at priority 37`, or the class alone where no priority    /// can be read.    pub fn said(&self) -> String {        match self.priority {            Some(priority) => format!("{} at priority {priority}", self.class),            None => self.class.to_string(),        }    }}/// The calling thread's class and granted priority.pub fn this_thread() -> ThreadClass {    #[cfg(any(target_os = "ios", target_os = "macos"))]    {        let mut class = libc::qos_class_t::QOS_CLASS_UNSPECIFIED;        let mut relative: libc::c_int = 0;        // SAFETY: both out-pointers are live locals.        unsafe {            libc::pthread_get_qos_class_np(libc::pthread_self(), &mut class, &mut relative);        }        ThreadClass {            class: class_name(class as u32),            priority: base_priority(),        }    }    #[cfg(not(any(target_os = "ios", target_os = "macos")))]    ThreadClass {        class: "unspecified",        priority: None,    }}#[cfg(any(target_os = "ios", target_os = "macos"))]fn base_priority() -> Option<i32> {    // SAFETY: a plain-old-data struct the kernel fills.    let mut info: libc::thread_extended_info = unsafe { std::mem::zeroed() };    let mut count = libc::THREAD_EXTENDED_INFO_COUNT;    // SAFETY: `mach_thread_self` hands back a send right this function owns    // and gives back below; `info` is a live local of the flavour's size.    #[allow(deprecated)]    let thread = unsafe { libc::mach_thread_self() };    let read = unsafe {        libc::thread_info(            thread,            libc::THREAD_EXTENDED_INFO as libc::thread_flavor_t,            (&mut info as *mut libc::thread_extended_info).cast(),            &mut count,        )    };    extern "C" {        // `<mach/mach_port.h>`; the `libc` crate does not bind it.        fn mach_port_deallocate(task: libc::mach_port_t, name: libc::mach_port_t) -> libc::kern_return_t;    }    // SAFETY: the right `mach_thread_self` gave this function.    #[allow(deprecated)]    unsafe {        mach_port_deallocate(libc::mach_task_self(), thread);    }    (read == 0).then_some(info.pth_priority)}/// The name of a `qos_class_t` value.pub fn class_name(class: u32) -> &'static str {    match class {        0x21 => "user-interactive",        0x19 => "user-initiated",        0x15 => "default",        0x11 => "utility",        0x09 => "background",        _ => "unspecified",    }}