#![doc = include_str!("../README.md")]
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Qos {
High,
Low,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct QosError(i32);
impl QosError {
#[must_use]
pub fn errno(self) -> i32 {
self.0
}
}
impl fmt::Display for QosError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "thread QoS change rejected (errno {})", self.0)
}
}
impl std::error::Error for QosError {}
pub fn set_current_thread(qos: Qos) -> Result<(), QosError> {
imp::set(qos)
}
pub fn with_qos<T>(qos: Qos, f: impl FnOnce() -> T) -> T {
let _restore = Restore(imp::save());
let _ = imp::set(qos);
f()
}
struct Restore(imp::Saved);
impl Drop for Restore {
fn drop(&mut self) {
imp::restore(&self.0);
}
}
pub fn boost_process_io() -> Result<(), QosError> {
#[cfg(target_vendor = "apple")]
{
imp::boost_process_io()
}
#[cfg(not(target_vendor = "apple"))]
{
imp::boost_process_io();
Ok(())
}
}
#[cfg(target_vendor = "apple")]
mod imp {
use libc::qos_class_t::{QOS_CLASS_DEFAULT, QOS_CLASS_USER_INITIATED, QOS_CLASS_UTILITY};
use super::{Qos, QosError};
pub(super) struct Saved {
class: libc::qos_class_t,
relative_priority: libc::c_int,
}
fn class_of(qos: Qos) -> libc::qos_class_t {
match qos {
Qos::High => QOS_CLASS_USER_INITIATED,
Qos::Low => QOS_CLASS_UTILITY,
}
}
pub(super) fn set(qos: Qos) -> Result<(), QosError> {
let rc = unsafe { libc::pthread_set_qos_class_self_np(class_of(qos), 0) };
if rc == 0 { Ok(()) } else { Err(QosError(rc)) }
}
pub(super) fn save() -> Saved {
let mut class = QOS_CLASS_DEFAULT;
let mut relative_priority: libc::c_int = 0;
unsafe {
libc::pthread_get_qos_class_np(
libc::pthread_self(),
&raw mut class,
&raw mut relative_priority,
);
}
Saved {
class,
relative_priority,
}
}
pub(super) fn restore(saved: &Saved) {
unsafe {
libc::pthread_set_qos_class_self_np(saved.class, saved.relative_priority);
}
}
const IOPOL_TYPE_DISK: libc::c_int = 0;
const IOPOL_SCOPE_PROCESS: libc::c_int = 1;
const IOPOL_IMPORTANT: libc::c_int = 1;
unsafe extern "C" {
fn setiopolicy_np(
iotype: libc::c_int,
scope: libc::c_int,
policy: libc::c_int,
) -> libc::c_int;
}
pub(super) fn boost_process_io() -> Result<(), QosError> {
let rc = unsafe { setiopolicy_np(IOPOL_TYPE_DISK, IOPOL_SCOPE_PROCESS, IOPOL_IMPORTANT) };
if rc == 0 {
Ok(())
} else {
Err(QosError(
std::io::Error::last_os_error().raw_os_error().unwrap_or(-1),
))
}
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
mod imp {
use super::{Qos, QosError};
pub(super) struct Saved {
nice: libc::c_int,
}
fn nice_of(qos: Qos) -> libc::c_int {
match qos {
Qos::High => 0,
Qos::Low => 10,
}
}
pub(super) fn set(qos: Qos) -> Result<(), QosError> {
let rc = unsafe { libc::setpriority(libc::PRIO_PROCESS, 0, nice_of(qos)) };
if rc == 0 {
Ok(())
} else {
Err(QosError(
std::io::Error::last_os_error().raw_os_error().unwrap_or(-1),
))
}
}
pub(super) fn save() -> Saved {
let nice = unsafe { libc::getpriority(libc::PRIO_PROCESS, 0) };
Saved { nice }
}
pub(super) fn restore(saved: &Saved) {
unsafe {
libc::setpriority(libc::PRIO_PROCESS, 0, saved.nice);
}
}
pub(super) fn boost_process_io() {
}
}
#[cfg(not(any(target_vendor = "apple", target_os = "linux", target_os = "android")))]
mod imp {
use super::{Qos, QosError};
pub(super) struct Saved;
pub(super) fn set(_qos: Qos) -> Result<(), QosError> {
Ok(())
}
pub(super) fn save() -> Saved {
Saved
}
pub(super) fn restore(_saved: &Saved) {}
pub(super) fn boost_process_io() {}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn low_and_high_apply_without_error() {
assert!(set_current_thread(Qos::Low).is_ok());
let _ = set_current_thread(Qos::High);
}
#[test]
fn boost_process_io_does_not_error_on_supported_platforms() {
assert!(boost_process_io().is_ok());
}
#[test]
fn with_qos_returns_closure_value_and_runs_it() {
let mut ran = false;
let out = with_qos(Qos::Low, || {
ran = true;
7 + 35
});
assert!(ran);
assert_eq!(out, 42);
}
#[test]
fn with_qos_nests() {
let out = with_qos(Qos::High, || with_qos(Qos::Low, || "inner").len());
assert_eq!(out, 5);
}
#[test]
fn with_qos_restores_after_panic() {
let result = std::panic::catch_unwind(|| {
with_qos(Qos::Low, || panic!("boom"));
});
assert!(result.is_err());
assert!(set_current_thread(Qos::Low).is_ok());
}
#[test]
fn qos_error_reports_errno() {
let err = QosError(13);
assert_eq!(err.errno(), 13);
assert!(err.to_string().contains("13"));
}
}