Skip to content

chemicstry/futex-queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Credits

This work was done as a part of my Thesis at University of Twente.

About

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

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages