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
16pub fn try_spawn<F, T>(
17    scheduler: Scheduler,
18    param: impl IntoSchedParam,
19    f: F,
20) -> thread::JoinHandle<T>
21where
22    F: FnOnce(crate::sched::Result<()>) -> T,
23    F: Send + 'static,
24    T: Send + 'static,
25{
26    let param = param.into_sched_param();
27    thread::spawn(move || {
28        let set_result = scheduler
29            .with_priority(param.priority)
30            .and_then(|ps| ps.set_current());
31        f(set_result)
32    })
33}