|
| 1 | +use std::cell::RefCell; |
| 2 | +use std::future::Future; |
| 3 | +use std::task::{Context, Poll}; |
| 4 | + |
| 5 | +static GLOBAL_EXECUTOR: once_cell::sync::Lazy<multitask::Executor> = once_cell::sync::Lazy::new(multitask::Executor::new); |
| 6 | + |
| 7 | +struct Executor { |
| 8 | + local_executor: multitask::LocalExecutor, |
| 9 | + parker: async_io::parking::Parker, |
| 10 | +} |
| 11 | + |
| 12 | +thread_local! { |
| 13 | + static EXECUTOR: RefCell<Executor> = RefCell::new({ |
| 14 | + let (parker, unparker) = async_io::parking::pair(); |
| 15 | + let local_executor = multitask::LocalExecutor::new(move || unparker.unpark()); |
| 16 | + Executor { local_executor, parker } |
| 17 | + }); |
| 18 | +} |
| 19 | + |
| 20 | +pub(crate) fn spawn<F, T>(future: F) -> multitask::Task<T> |
| 21 | +where |
| 22 | + F: Future<Output = T> + Send + 'static, |
| 23 | + T: Send + 'static, |
| 24 | +{ |
| 25 | + GLOBAL_EXECUTOR.spawn(future) |
| 26 | +} |
| 27 | + |
| 28 | +#[cfg(feature = "unstable")] |
| 29 | +pub(crate) fn local<F, T>(future: F) -> multitask::Task<T> |
| 30 | +where |
| 31 | + F: Future<Output = T> + 'static, |
| 32 | + T: 'static, |
| 33 | +{ |
| 34 | + EXECUTOR.with(|executor| executor.borrow().local_executor.spawn(future)) |
| 35 | +} |
| 36 | + |
| 37 | +pub(crate) fn run<F, T>(future: F) -> T |
| 38 | +where |
| 39 | + F: Future<Output = T>, |
| 40 | +{ |
| 41 | + enter(|| EXECUTOR.with(|executor| { |
| 42 | + let executor = executor.borrow(); |
| 43 | + let unparker = executor.parker.unparker(); |
| 44 | + let global_ticker = GLOBAL_EXECUTOR.ticker(move || unparker.unpark()); |
| 45 | + let unparker = executor.parker.unparker(); |
| 46 | + let waker = async_task::waker_fn(move || unparker.unpark()); |
| 47 | + let cx = &mut Context::from_waker(&waker); |
| 48 | + pin_utils::pin_mut!(future); |
| 49 | + loop { |
| 50 | + if let Poll::Ready(res) = future.as_mut().poll(cx) { |
| 51 | + return res; |
| 52 | + } |
| 53 | + if let Ok(false) = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| executor.local_executor.tick() || global_ticker.tick())) { |
| 54 | + executor.parker.park(); |
| 55 | + } |
| 56 | + } |
| 57 | + })) |
| 58 | +} |
| 59 | + |
| 60 | +/// Enters the tokio context if the `tokio` feature is enabled. |
| 61 | +fn enter<T>(f: impl FnOnce() -> T) -> T { |
| 62 | + #[cfg(not(feature = "tokio02"))] |
| 63 | + return f(); |
| 64 | + |
| 65 | + #[cfg(feature = "tokio02")] |
| 66 | + { |
| 67 | + use std::cell::Cell; |
| 68 | + use tokio::runtime::Runtime; |
| 69 | + |
| 70 | + thread_local! { |
| 71 | + /// The level of nested `enter` calls we are in, to ensure that the outermost always |
| 72 | + /// has a runtime spawned. |
| 73 | + static NESTING: Cell<usize> = Cell::new(0); |
| 74 | + } |
| 75 | + |
| 76 | + /// The global tokio runtime. |
| 77 | + static RT: once_cell::sync::Lazy<Runtime> = once_cell::sync::Lazy::new(|| Runtime::new().expect("cannot initialize tokio")); |
| 78 | + |
| 79 | + NESTING.with(|nesting| { |
| 80 | + let res = if nesting.get() == 0 { |
| 81 | + nesting.replace(1); |
| 82 | + RT.enter(f) |
| 83 | + } else { |
| 84 | + nesting.replace(nesting.get() + 1); |
| 85 | + f() |
| 86 | + }; |
| 87 | + nesting.replace(nesting.get() - 1); |
| 88 | + res |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
0 commit comments