#linux #thread

futex-queue

An efficient MPSC queue with timer capability based on Linux futex. Suitable for real-time applications.

2 releases

0.1.1 Sep 29, 2021
0.1.0 Sep 29, 2021

#438 in #concurrency


Used in linux-rtic

Apache-2.0 OR MIT

12KB
180 lines

futex-queue

License Cargo Documentation

An efficient MPSC queue with timer capability based on Linux futex. Suitable for real-time applications.

How it Works

Queue is based on Linux futex syscall to wait for both immediate and scheduled items in a single syscall. This alleviates the need for separate timer thread, which would involve multiple context switches.

Immediate items are sent via regular futex atomic variable waking mechanism. Scheduled items use FUTEX_WAIT_BITSET operation with absolute timestamp of the earliest item in the queue as a timeout for the syscall.

Example

let (tx, mut rx) = FutexQueue::<u32, 4>::new();
let now = Instant::now();

let thread = thread::spawn(move || {
    loop {
        let item = rx.recv();
        println!("{} ms: Received: {}", now.elapsed().as_millis(), item.value());
    }
});

tx.send(1).unwrap();
tx.send_scheduled(2, now + Duration::from_secs(1)).unwrap();
tx.send(3).unwrap();
tx.send_scheduled(4, now + Duration::from_secs(2)).unwrap();

thread.join().unwrap();

Output:

0 ms: Received: 3
0 ms: Received: 1
1000 ms: Received: 2
2000 ms: Received: 4

Dependencies

~710KB
~14K SLoC