17 releases

0.1.16 Jul 12, 2024
0.1.10 Apr 12, 2023
0.1.9 Jan 30, 2023
0.1.7 Sep 19, 2022
0.1.0 May 10, 2018

#504 in Concurrency


Used in may_rpc

MIT/Apache

23KB
493 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

~4–29MB
~443K SLoC