2022-02-08 03:36:56 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::task::{Wake, Waker};
|
|
|
|
|
2022-01-30 02:26:35 +00:00
|
|
|
use safe_executor::Executor;
|
2022-01-29 21:50:49 +00:00
|
|
|
|
2022-02-11 04:25:38 +00:00
|
|
|
fn spawn(executor: &Executor) {
|
2022-01-29 22:10:04 +00:00
|
|
|
println!("Spawning futures");
|
2022-01-29 21:50:49 +00:00
|
|
|
|
2022-02-11 04:25:38 +00:00
|
|
|
let task1 = executor.spawn(async move {
|
2022-01-29 22:10:04 +00:00
|
|
|
println!("Henlo from first spawn");
|
2022-01-29 21:50:49 +00:00
|
|
|
"A"
|
|
|
|
});
|
|
|
|
|
2022-02-11 04:25:38 +00:00
|
|
|
let task2 = executor.spawn(async move {
|
2022-01-29 22:10:04 +00:00
|
|
|
println!("Henlo from second spawn");
|
2022-01-29 21:50:49 +00:00
|
|
|
"B"
|
|
|
|
});
|
|
|
|
|
2022-02-11 04:25:38 +00:00
|
|
|
let run2m = executor.clone();
|
2022-01-29 21:50:49 +00:00
|
|
|
|
2022-02-11 04:25:38 +00:00
|
|
|
executor.spawn(async move {
|
2022-01-29 21:50:49 +00:00
|
|
|
let res1 = task1.await;
|
|
|
|
let res2 = task2.await;
|
2022-01-29 22:10:04 +00:00
|
|
|
println!("Henlo from third spawn, {:?}, {:?}", res1, res2);
|
2022-01-29 21:50:49 +00:00
|
|
|
|
|
|
|
let res3 = run2m
|
|
|
|
.spawn(async move {
|
2022-01-29 22:10:04 +00:00
|
|
|
println!("Henlo from inner spawn");
|
2022-01-29 21:50:49 +00:00
|
|
|
"D"
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
2022-01-29 22:10:04 +00:00
|
|
|
println!("Henlo again from third spawn, {:?}", res3);
|
2022-01-29 21:50:49 +00:00
|
|
|
});
|
2022-01-29 22:10:04 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 03:36:56 +00:00
|
|
|
struct DummyWaker;
|
|
|
|
impl Wake for DummyWaker {
|
|
|
|
fn wake(self: std::sync::Arc<Self>) {}
|
|
|
|
fn wake_by_ref(self: &Arc<Self>) {}
|
|
|
|
}
|
|
|
|
|
2022-01-29 22:10:04 +00:00
|
|
|
fn main() {
|
2022-02-11 04:25:38 +00:00
|
|
|
let executor = Executor::new();
|
|
|
|
let runner = executor.clone().into_runner();
|
2022-01-29 22:10:04 +00:00
|
|
|
|
2022-01-29 22:15:23 +00:00
|
|
|
// This creates 3 new tasks
|
2022-02-11 04:25:38 +00:00
|
|
|
spawn(&executor);
|
2022-01-29 22:10:04 +00:00
|
|
|
|
2022-02-08 03:36:56 +00:00
|
|
|
let waker: Waker = Arc::new(DummyWaker).into();
|
|
|
|
|
2022-02-11 04:25:38 +00:00
|
|
|
while runner.any_woken() {
|
2022-01-29 22:10:04 +00:00
|
|
|
println!("Ticking");
|
2022-02-11 04:25:38 +00:00
|
|
|
runner.tick(&waker);
|
2022-01-29 22:10:04 +00:00
|
|
|
}
|
2022-01-29 22:15:23 +00:00
|
|
|
|
|
|
|
// This reclaims the first 3 tasks
|
2022-01-29 22:10:04 +00:00
|
|
|
println!("Pruning");
|
2022-02-11 04:25:38 +00:00
|
|
|
runner.prune();
|
2022-01-29 21:50:49 +00:00
|
|
|
|
2022-01-29 22:15:23 +00:00
|
|
|
// This creates 3 new tasks
|
2022-02-11 04:25:38 +00:00
|
|
|
spawn(&executor);
|
|
|
|
while runner.any_woken() {
|
2022-01-29 21:50:49 +00:00
|
|
|
println!("Ticking");
|
2022-02-11 04:25:38 +00:00
|
|
|
runner.tick(&waker);
|
2022-01-29 21:50:49 +00:00
|
|
|
}
|
2022-01-29 22:10:04 +00:00
|
|
|
|
2022-01-29 22:15:23 +00:00
|
|
|
// This re-uses the 3 tasks created prior
|
2022-02-11 04:25:38 +00:00
|
|
|
spawn(&executor);
|
2022-01-29 22:15:23 +00:00
|
|
|
|
|
|
|
// This doesn't reclaim any tasks, since we've spawned 3 more futures
|
2022-01-29 22:10:04 +00:00
|
|
|
println!("Pruning");
|
2022-02-11 04:25:38 +00:00
|
|
|
runner.prune();
|
2022-01-29 22:10:04 +00:00
|
|
|
|
2022-02-11 04:25:38 +00:00
|
|
|
while runner.any_woken() {
|
2022-01-29 22:10:04 +00:00
|
|
|
println!("Ticking");
|
2022-02-11 04:25:38 +00:00
|
|
|
runner.tick(&waker);
|
2022-01-29 22:15:23 +00:00
|
|
|
|
|
|
|
// This reclaims tasks as their futures resolve on each tick
|
2022-01-29 22:10:04 +00:00
|
|
|
println!("Pruning");
|
2022-02-11 04:25:38 +00:00
|
|
|
runner.prune();
|
2022-01-29 22:10:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
println!("Hewwo Mr Obama");
|
2022-01-29 21:50:49 +00:00
|
|
|
}
|