#linux-kernel #thread-priority #libc #scheduler #preempt

preempt-rt

A lightweight Rust library for using the kernel's PREEMPT_RT scheduling functionality

9 unstable releases (3 breaking)

Uses new Rust 2024

0.4.3 Oct 29, 2025
0.4.2 Jul 31, 2025
0.3.1 Jul 28, 2025
0.2.1 Jul 23, 2025
0.1.1 Jul 23, 2025

#654 in Unix APIs

MIT license

21KB
318 lines

preempt-rt

preempt-rt is a lightweight wrapper around the PREEMPT_RT libc functions, providing a rust-like interface to the underlying libc functions.

There are some simple helper functions that can be used directly:

fn main() {
    let scheduler = get_scheduler(Pid::current_thread()).expect("could not get scheduler");
    set_scheduler(Scheduler::SCHED_FIFO, 50).expect("could not set scheduler to fifo with priority 50");
}

There are also lightweight wrappers for spawning threads with a given scheduler and priority. Because the FIFO, RR, and DEADLINE schedulers will only work on a Linux kernel compiled with PREEMPT_RT, a fallible option is provided too (useful when testing code on a non-rt host).

fn main() {
    preempt_rt::thread::spawn(Scheduler::SCHED_FIFO, 50, move || {
        println!("hello from an rt thread");
    });

    preempt_rt::thread::try_spawn(Scheduler::SCHED_FIFO, 50, move |sched_result| {
        match sched_result {
            Ok(()) => println!("hello from an rt thread"),
            Err(err) => eprintln!("failed to set thread scheduler: {err}")
        }
    });
}

Although SCHED_DEADLINE is included in the Scheduler enum, support for setting the deadline parameters is not currently implemented in this library.

Dependencies

~165–610KB
~14K SLoC