Crates.io | may_waiter |
lib.rs | may_waiter |
version | 0.1.16 |
source | src |
created_at | 2018-05-10 02:00:26.947623 |
updated_at | 2024-07-12 06:11:43.394625 |
description | Simple coroutine communication blocker |
homepage | https://github.com/Xudong-Huang/may_waiter.git |
repository | https://github.com/Xudong-Huang/may_waiter.git |
max_upload_size | |
id | 64621 |
size | 35,349 |
This library provide a map associated blocking primitive that waiting for a response produced by another coroutine
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);
}
TokenWaiter
, which not need a mapfn 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);
}
}