Extract platform-specific QoS code into module

This commit is contained in:
Luna Razzaghipour 2023-05-21 02:27:49 +10:00
parent bb02ae7532
commit a416248296
No known key found for this signature in database

View File

@ -207,16 +207,25 @@ pub enum QoSClass {
UserInteractive, UserInteractive,
} }
#[cfg(target_vendor = "apple")] pub const IS_QOS_AVAILABLE: bool = imp::IS_QOS_AVAILABLE;
pub const IS_QOS_AVAILABLE: bool = true;
#[cfg(not(target_vendor = "apple"))] pub fn set_current_thread_qos_class(class: QoSClass) {
pub const IS_QOS_AVAILABLE: bool = false; imp::set_current_thread_qos_class(class)
}
pub fn get_current_thread_qos_class() -> Option<QoSClass> {
imp::get_current_thread_qos_class()
}
// All Apple platforms use XNU as their kernel // All Apple platforms use XNU as their kernel
// and thus have the concept of QoS. // and thus have the concept of QoS.
#[cfg(target_vendor = "apple")] #[cfg(target_vendor = "apple")]
pub fn set_current_thread_qos_class(class: QoSClass) { mod imp {
use super::QoSClass;
pub(super) const IS_QOS_AVAILABLE: bool = true;
pub(super) fn set_current_thread_qos_class(class: QoSClass) {
let c = match class { let c = match class {
QoSClass::UserInteractive => libc::qos_class_t::QOS_CLASS_USER_INTERACTIVE, QoSClass::UserInteractive => libc::qos_class_t::QOS_CLASS_USER_INTERACTIVE,
QoSClass::UserInitiated => libc::qos_class_t::QOS_CLASS_USER_INITIATED, QoSClass::UserInitiated => libc::qos_class_t::QOS_CLASS_USER_INITIATED,
@ -248,7 +257,9 @@ pub fn set_current_thread_qos_class(class: QoSClass) {
// to `pthread_set_qos_class_self_np`. // to `pthread_set_qos_class_self_np`.
// //
// This is impossible, so again panic. // This is impossible, so again panic.
unreachable!("invalid qos_class_t value was passed to pthread_set_qos_class_self_np") unreachable!(
"invalid qos_class_t value was passed to pthread_set_qos_class_self_np"
)
} }
_ => { _ => {
@ -257,15 +268,9 @@ pub fn set_current_thread_qos_class(class: QoSClass) {
unreachable!("`pthread_set_qos_class_self_np` returned unexpected error {errno}") unreachable!("`pthread_set_qos_class_self_np` returned unexpected error {errno}")
} }
} }
} }
#[cfg(not(target_vendor = "apple"))] pub(super) fn get_current_thread_qos_class() -> Option<QoSClass> {
pub fn set_current_thread_qos_class(class: QoSClass) {
// FIXME: Windows has QoS APIs, we should use them!
}
#[cfg(target_vendor = "apple")]
pub fn get_current_thread_qos_class() -> Option<QoSClass> {
let current_thread = unsafe { libc::pthread_self() }; let current_thread = unsafe { libc::pthread_self() };
let mut qos_class_raw = libc::qos_class_t::QOS_CLASS_UNSPECIFIED; let mut qos_class_raw = libc::qos_class_t::QOS_CLASS_UNSPECIFIED;
let code = unsafe { let code = unsafe {
@ -303,9 +308,19 @@ pub fn get_current_thread_qos_class() -> Option<QoSClass> {
panic!("tried to get QoS of thread which has opted out of QoS") panic!("tried to get QoS of thread which has opted out of QoS")
} }
} }
}
} }
// FIXME: Windows has QoS APIs, we should use them!
#[cfg(not(target_vendor = "apple"))] #[cfg(not(target_vendor = "apple"))]
pub fn get_current_thread_qos_class() -> Option<QoSClass> { mod imp {
use super::QoSClass;
pub(super) const IS_QOS_AVAILABLE: bool = false;
pub(super) fn set_current_thread_qos_class(_: QoSClass) {}
pub(super) fn get_current_thread_qos_class() -> Option<QoSClass> {
None None
}
} }