#sync #native #wait-object

sync-wait-object

Synchronous Wait Object (abstraction over Mutex & CondVar)

2 unstable releases

0.2.0 Jan 22, 2023
0.1.0 Jan 20, 2023

#187 in Windows APIs

Download history 4/week @ 2024-05-27 5/week @ 2024-06-03 2/week @ 2024-06-17 8/week @ 2024-07-01 22/week @ 2024-07-29 16/week @ 2024-08-05 11/week @ 2024-08-12 15/week @ 2024-08-19 19/week @ 2024-08-26 32/week @ 2024-09-02

78 downloads per month
Used in native-timer

MIT license

20KB
266 lines

Wait Object based on Mutex and Condvar

Provide an abstraction over Condvar + Mutex usage, as provided by the Rust document in Condvar.

The library provides three main types: WaitEvent, ManualResetEvent, and AutoResetEvent. WaitEvent is the core abstraction mentioned. ManualResetEvent and AutoResetEvent are just a specialization for bool type.

When compiling with Windows platform, the lib also provides windows module for native implementation of ManualResetEvent and AutoResetEvent.

Example of the abstraction provided:

use sync_wait_object::WaitEvent;
use std::thread;

let wait3 = WaitEvent::new_init(0);
let mut wait_handle = wait3.clone();

thread::spawn(move || {
    for i in 1..=3 {
        wait_handle.set_state(i).unwrap();
    }
});

let timeout = std::time::Duration::from_secs(1);
let r#final = *wait3.wait(Some(timeout), |i| *i == 3).unwrap();
let current = *wait3.value().unwrap();
assert_eq!(r#final, 3);
assert_eq!(current, 3);

The second is to wait and then reset the value to a desired state.

use sync_wait_object::WaitEvent;
use std::thread;

let wait3 = WaitEvent::new_init(0);
let mut wait_handle = wait3.clone();

thread::spawn(move || {
    for i in 1..=3 {
        wait_handle.set_state(i).unwrap();
    }
});

let timeout = std::time::Duration::from_secs(1);
let r#final = wait3.wait_reset(Some(timeout), || 1, |i| *i == 3).unwrap();
let current = *wait3.value().unwrap();
assert_eq!(r#final, 3);
assert_eq!(current, 1);

Dependencies

~0–38MB
~524K SLoC