11 releases

0.1.10 Apr 12, 2023
0.1.9 Jan 30, 2023
0.1.7 Sep 19, 2022
0.1.6 Mar 30, 2022
0.1.0 May 10, 2018

#219 in Concurrency

Download history 1/week @ 2024-02-16 31/week @ 2024-02-23 61/week @ 2024-03-01 5/week @ 2024-03-08 3/week @ 2024-03-15

70 downloads per month
Used in may_rpc

MIT/Apache

16KB
326 lines

Build Status Current Crates.io Version Document

coroutine waiter library

This library provide a map associated blocking primitive that waiting for a response produced by another coroutine

Usage

  • the map associated interface is through WaiterMap
fn test_waiter_map() {
    let req_map = Arc::new(WaiterMap::<usize, usize>::new());
    let req_map_1 = req_map.clone();

    let key = 1234;

    // one coroutine wait data send from another coroutine
    // prepare the waiter first
    let waiter = req_map.new_waiter(key);

    // trigger the rsp in another coroutine
    go!(move || req_map_1.set_rsp(&key, 100).ok());

    // this will block until the rsp was set
    let result = waiter.wait_rsp(None).unwrap();
    assert_eq!(result, 100);
}
  • the token associated interface is through TokenWaiter, which not need a map
fn test_token_waiter() {
    for j in 0..100 {
        let result = go!(move || {
            let waiter = TokenWaiter::<usize>::new();
            let waiter = Pin::new(&waiter);
            let id = waiter.id().unwrap();
            // trigger the rsp in another coroutine
            go!(move || TokenWaiter::set_rsp(id, j + 100));
            // this will block until the rsp was set
            assert_eq!(waiter.wait_rsp(None).unwrap(), j + 100);
            // after wait we can get the id again
            let id = waiter.id().unwrap();
            go!(move || TokenWaiter::set_rsp(id, j));
            waiter.wait_rsp(std::time::Duration::from_secs(2)).unwrap()
        })
        .join()
        .unwrap();

        assert_eq!(result, j);
    }
}

Dependencies

~3–34MB
~464K SLoC