Crate hybridfutex

source ·
Expand description

HybridFutex

Crates.io Documentation MIT licensed

HybridFutex is a Rust library that provides a synchronization primitive that allows threads to wait for a notification from another thread. It is designed to be low-overhead and scalable and supports both synchronous and asynchronous waiting and notification.

Features

  • Support Notify many operations on any target.
  • Support for synchronous and asynchronous waiting.
  • Built-in fairness and scalability for high-contention scenarios.
  • Efficient kernel-assisted blocking using park API.
  • Low-latency waiting and notification on both Windows and Unix platforms.
  • Cross-platform compatibility with no external dependencies.
  • Simple and easy-to-use API.

Usage

To use HybridFutex, simply add it as a dependency in your Cargo.toml file:

[dependencies]
hybrid-futex = "0.1"

Then, you can use HybridFutex in your Rust code as follows:

use std::sync::Arc;
use std::thread;
use std::time::Duration;
use hybridfutex::HybridFutex;

let wait_queue = Arc::new(HybridFutex::new());
let wait_queue_clone = wait_queue.clone();

// Spawn a thread that waits for a notification from another thread
let handle = thread::spawn(move || {
    println!("Thread 1 is waiting");
    wait_queue_clone.wait_sync();
    println!("Thread 1 is notified");
});

// Wait for a short time before notifying the other thread
thread::sleep(Duration::from_millis(100));

// Notify the other thread
wait_queue.notify_one();

// Wait for the other thread to finish
handle.join().unwrap();

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • This library is inspired by the futex system-call in Linux.

Structs

  • A HybridFutex is a synchronization primitive that allows threads to wait for a notification from another thread. The HybridFutex maintains a counter that represents the number of waiters, and a queue of waiters. The counter is incremented when a thread calls wait_sync or wait_async methods, and decremented when a thread calls notify_one or notify_many methods. A thread calling wait_sync or wait_async is blocked until it is notified by another thread calling notify_one or notify_many.
  • A future representing a thread that is waiting for a notification from another thread using a HybridFutex synchronization primitive.