preempt_rt/
thread.rs

1use crate::sched::{IntoSchedParam, Scheduler};
2use std::thread;
3
4pub fn spawn<F, T>(scheduler: Scheduler, param: impl IntoSchedParam, f: F) -> thread::JoinHandle<T>
5where
6    F: FnOnce() -> T,
7    F: Send + 'static,
8    T: Send + 'static,
9{
10    try_spawn(scheduler, param, move |set_result| {
11        set_result.expect("failed to set scheduler");
12        f()
13    })
14}
15
16#[cfg(target_os = "linux")]
17pub fn try_spawn<F, T>(
18    scheduler: Scheduler,
19    param: impl IntoSchedParam,
20    f: F,
21) -> thread::JoinHandle<T>
22where
23    F: FnOnce(crate::sched::RtResult<()>) -> T,
24    F: Send + 'static,
25    T: Send + 'static,
26{
27    let param = param.into_sched_param();
28    thread::spawn(move || {
29        let set_result = scheduler
30            .with_priority(param.priority)
31            .and_then(|ps| ps.set_current());
32        f(set_result)
33    })
34}
35
36#[cfg(all(feature = "non-linux-stubs", target_os = "macos"))]
37pub fn try_spawn<F, T>(
38    _scheduler: Scheduler,
39    _param: impl IntoSchedParam,
40    f: F,
41) -> thread::JoinHandle<T>
42where
43    F: FnOnce(crate::sched::RtResult<()>) -> T,
44    F: Send + 'static,
45    T: Send + 'static,
46{
47    thread::spawn(move || f(Err(crate::sched::PreemptRtError::NonLinuxPlatform("macos"))))
48}