use std::marker::PhantomData;
use std::time::Duration;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DspScheduling {
Workgroup,
TimeConstraint,
Qos,
#[default]
None,
}
impl DspScheduling {
pub const fn as_str(self) -> &'static str {
match self {
DspScheduling::Workgroup => "workgroup",
DspScheduling::TimeConstraint => "time_constraint",
DspScheduling::Qos => "qos",
DspScheduling::None => "none",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(
not(any(test, target_os = "macos", target_os = "ios")),
allow(dead_code)
)]
pub(crate) struct RtParams {
pub(crate) period: Duration,
pub(crate) computation: Duration,
pub(crate) constraint: Duration,
}
pub(crate) const RT_PARAMS: RtParams = RtParams {
period: Duration::from_millis(10),
computation: crate::stage::P99_BUDGET,
constraint: crate::stage::HARD_BUDGET,
};
pub(crate) const WAKE_FROM_IO: bool = cfg!(any(target_os = "macos", target_os = "ios"));
pub(crate) trait SchedOs {
type Workgroup;
type Token;
fn set_qos(&mut self) -> bool;
fn set_time_constraint(&mut self, params: RtParams) -> bool;
fn join(&mut self, wg: &Self::Workgroup) -> Option<Self::Token>;
fn leave(&mut self, wg: &Self::Workgroup, token: Self::Token);
}
pub(crate) struct DspSched<O: SchedOs> {
os: O,
base: DspScheduling,
joined: Option<(Box<O::Workgroup>, O::Token)>,
_not_send: PhantomData<*const ()>,
}
impl<O: SchedOs> DspSched<O> {
pub(crate) fn promote(mut os: O, params: RtParams) -> Self {
let qos = os.set_qos();
let time_constraint = os.set_time_constraint(params);
let base = if time_constraint {
DspScheduling::TimeConstraint
} else if qos {
DspScheduling::Qos
} else {
DspScheduling::None
};
DspSched {
os,
base,
joined: None,
_not_send: PhantomData,
}
}
pub(crate) fn mode(&self) -> DspScheduling {
if self.joined.is_some() {
DspScheduling::Workgroup
} else {
self.base
}
}
pub(crate) fn set_workgroup(
&mut self,
wg: Option<Box<O::Workgroup>>,
mut discard: impl FnMut(Box<O::Workgroup>),
) {
if let Some((old, token)) = self.joined.take() {
self.os.leave(&old, token);
discard(old);
}
let Some(wg) = wg else {
return;
};
if self.base != DspScheduling::TimeConstraint {
discard(wg);
return;
}
match self.os.join(&wg) {
Some(token) => self.joined = Some((wg, token)),
None => discard(wg),
}
}
}
impl<O: SchedOs> Drop for DspSched<O> {
fn drop(&mut self) {
if let Some((wg, token)) = self.joined.take() {
self.os.leave(&wg, token);
}
}
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub(crate) mod apple;
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub(crate) use apple::{AppleOs as PlatformOs, Workgroup};
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
pub(crate) struct PlatformOs;
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
pub(crate) enum Workgroup {}
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
impl PlatformOs {
pub(crate) fn new() -> Self {
PlatformOs
}
}
#[cfg(not(any(target_os = "macos", target_os = "ios")))]
impl SchedOs for PlatformOs {
type Workgroup = Workgroup;
type Token = ();
fn set_qos(&mut self) -> bool {
false
}
fn set_time_constraint(&mut self, _: RtParams) -> bool {
false
}
fn join(&mut self, wg: &Workgroup) -> Option<()> {
match *wg {}
}
fn leave(&mut self, wg: &Workgroup, _: ()) {
match *wg {}
}
}
pub(crate) fn promote_current() -> DspSched<PlatformOs> {
DspSched::promote(PlatformOs::new(), RT_PARAMS)
}
#[cfg(test)]
mod tests;