futex-queue

Crates.iofutex-queue
lib.rsfutex-queue
version0.1.1
sourcesrc
created_at2021-09-29 13:09:19.380545
updated_at2021-09-29 13:11:05.898874
descriptionAn efficient MPSC queue with timer capability based on Linux futex. Suitable for real-time applications.
homepagehttps://github.com/chemicstry/futex-queue
repositoryhttps://github.com/chemicstry/futex-queue
max_upload_size
id458091
size30,249
Jurgis (chemicstry)

documentation

https://docs.rs/futex-queue

README

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
Commit count: 6

cargo fmt