use super::PriorityError;
use std::cell::RefCell;
thread_local! {
static POSTURE: RefCell<Option<Result<(), PriorityError>>> = const { RefCell::new(None) };
}
pub(super) fn lower_current_thread() -> Result<(), PriorityError> {
let result = platform_lower();
POSTURE.with(|posture| *posture.borrow_mut() = Some(result.clone()));
result
}
pub(super) fn check_current_thread() -> Result<(), PriorityError> {
POSTURE.with(|posture| {
posture
.borrow()
.clone()
.unwrap_or(Err(PriorityError::Unexpected {
operation: "background worker entry was not initialized",
value: 0,
}))
})
}
#[cfg(any(target_os = "linux", windows))]
fn os_error(operation: &'static str) -> PriorityError {
PriorityError::Os {
operation,
code: std::io::Error::last_os_error().raw_os_error(),
}
}
#[cfg(target_os = "linux")]
fn platform_lower() -> Result<(), PriorityError> {
fn read() -> Result<i32, PriorityError> {
let (value, error) = unsafe {
*libc::__errno_location() = 0;
let value = libc::getpriority(libc::PRIO_PROCESS, 0);
(value, *libc::__errno_location())
};
if error != 0 {
return Err(PriorityError::Os {
operation: "getpriority",
code: Some(error),
});
}
if !(-20..=19).contains(&value) {
return Err(PriorityError::Unexpected {
operation: "getpriority",
value,
});
}
Ok(value)
}
let current = read()?;
let desired = if current == 19 { current } else { current + 1 };
let target = current.max(desired);
if unsafe { libc::setpriority(libc::PRIO_PROCESS, 0, target) } != 0 {
return Err(os_error("setpriority"));
}
let observed = read()?;
if observed < target {
return Err(PriorityError::Unexpected {
operation: "getpriority after lowering",
value: observed,
});
}
Ok(())
}
#[cfg(target_os = "macos")]
fn platform_lower() -> Result<(), PriorityError> {
#[repr(u32)]
enum QosClass {
Background = 0x09,
}
unsafe extern "C" {
fn pthread_set_qos_class_self_np(
class: QosClass,
priority: std::ffi::c_int,
) -> std::ffi::c_int;
}
let code = unsafe { pthread_set_qos_class_self_np(QosClass::Background, 0) };
if code != 0 {
return Err(PriorityError::Os {
operation: "pthread_set_qos_class_self_np",
code: Some(code),
});
}
Ok(())
}
#[cfg(windows)]
fn platform_lower() -> Result<(), PriorityError> {
use std::ffi::c_void;
#[link(name = "kernel32")]
unsafe extern "system" {
fn GetCurrentThread() -> *mut c_void;
fn GetThreadPriority(thread: *mut c_void) -> i32;
fn SetThreadPriority(thread: *mut c_void, priority: i32) -> i32;
}
fn recognized(value: i32) -> bool {
matches!(value, -15 | -2 | -1 | 0 | 1 | 2 | 15)
}
let thread = unsafe { GetCurrentThread() };
let current = unsafe { GetThreadPriority(thread) };
if !recognized(current) {
let diagnostic_os_error = std::io::Error::last_os_error().raw_os_error();
tracing::warn!(
returned = current,
?diagnostic_os_error,
"unrecognized GetThreadPriority result; OS error is diagnostic only"
);
return Err(PriorityError::Unexpected {
operation: "GetThreadPriority",
value: current,
});
}
let target = current.min(-1);
if unsafe { SetThreadPriority(thread, target) } == 0 {
return Err(os_error("SetThreadPriority"));
}
let observed = unsafe { GetThreadPriority(thread) };
if !recognized(observed) || observed > target {
let diagnostic_os_error = std::io::Error::last_os_error().raw_os_error();
tracing::warn!(
returned = observed,
?diagnostic_os_error,
"unexpected GetThreadPriority verification; OS error is diagnostic only"
);
return Err(PriorityError::Unexpected {
operation: "GetThreadPriority after lowering",
value: observed,
});
}
Ok(())
}
#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
fn platform_lower() -> Result<(), PriorityError> {
Err(PriorityError::Unsupported {
target: std::env::consts::OS,
})
}
#[cfg(test)]
pub(super) fn refuse_current_thread_for_test() {
POSTURE.with(|posture| {
*posture.borrow_mut() = Some(Err(PriorityError::Unexpected {
operation: "test posture refusal",
value: -999,
}))
});
}