Crates.io | futex-queue |
lib.rs | futex-queue |
version | 0.1.1 |
source | src |
created_at | 2021-09-29 13:09:19.380545 |
updated_at | 2021-09-29 13:11:05.898874 |
description | An efficient MPSC queue with timer capability based on Linux futex. Suitable for real-time applications. |
homepage | https://github.com/chemicstry/futex-queue |
repository | https://github.com/chemicstry/futex-queue |
max_upload_size | |
id | 458091 |
size | 30,249 |
An efficient MPSC queue with timer capability based on Linux futex. Suitable for real-time applications.
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.
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