naia_server_socket/
executor.rs

1use std::{future::Future, panic::catch_unwind, thread};
2
3use once_cell::sync::Lazy;
4use smol::{block_on, future, Executor, Task};
5
6/// TODO: make description
7pub fn spawn<T: Send + 'static>(future: impl Future<Output = T> + Send + 'static) -> Task<T> {
8    static GLOBAL: Lazy<Executor<'_>> = Lazy::new(|| {
9        for n in 1..=4 {
10            thread::Builder::new()
11                .name(format!("smol-{}", n))
12                .spawn(|| loop {
13                    catch_unwind(|| block_on(GLOBAL.run(future::pending::<()>()))).ok();
14                })
15                .expect("cannot spawn executor thread");
16        }
17
18        Executor::new()
19    });
20
21    GLOBAL.spawn(future)
22}