//! Bump forge's main-thread priority via rtkit (negative nice, NOT SCHED_RR —
//! RT scheduling gets SIGKILL'd by RLIMIT_RTTIME on render bursts).

use tracing::{info, warn};

const REQUESTED_NICENESS: i32 = -15;

pub fn boost_main_priority() {
    let tid = unsafe { libc::gettid() } as u64;
    match request(tid) {
        Ok(()) => info!("rtkit: main thread boosted to nice {REQUESTED_NICENESS}"),
        Err(e) => warn!("rtkit priority boost failed: {e}"),
    }
}

fn request(tid: u64) -> Result<(), Box<dyn std::error::Error>> {
    let conn = zbus::blocking::Connection::system()?;
    conn.call_method(
        Some("org.freedesktop.RealtimeKit1"),
        "/org/freedesktop/RealtimeKit1",
        Some("org.freedesktop.RealtimeKit1"),
        "MakeThreadHighPriority",
        &(tid, REQUESTED_NICENESS),
    )?;
    Ok(())
}
