2023-10-30 04:35:06 +00:00
|
|
|
use std::fmt::{Display, Formatter};
|
|
|
|
use std::future::Future;
|
|
|
|
use std::io;
|
|
|
|
|
2022-06-27 15:15:43 +00:00
|
|
|
use tokio::runtime;
|
2023-10-30 04:35:06 +00:00
|
|
|
use tokio::runtime::Runtime;
|
|
|
|
use tokio::task::JoinHandle;
|
|
|
|
|
|
|
|
pub struct AFPluginRuntime {
|
|
|
|
inner: Runtime,
|
2024-01-05 16:37:26 +00:00
|
|
|
#[cfg(feature = "wasm_build")]
|
2023-10-30 04:35:06 +00:00
|
|
|
local: tokio::task::LocalSet,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for AFPluginRuntime {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
if cfg!(feature = "single_thread") {
|
|
|
|
write!(f, "Runtime(single_thread)")
|
|
|
|
} else {
|
|
|
|
write!(f, "Runtime(multi_thread)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AFPluginRuntime {
|
|
|
|
pub fn new() -> io::Result<Self> {
|
|
|
|
let inner = default_tokio_runtime()?;
|
|
|
|
Ok(Self {
|
|
|
|
inner,
|
2024-01-05 16:37:26 +00:00
|
|
|
#[cfg(feature = "wasm_build")]
|
2023-10-30 04:35:06 +00:00
|
|
|
local: tokio::task::LocalSet::new(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-05 16:37:26 +00:00
|
|
|
#[cfg(feature = "wasm_build")]
|
2023-10-30 04:35:06 +00:00
|
|
|
#[track_caller]
|
|
|
|
pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
|
|
|
|
where
|
|
|
|
F: Future + 'static,
|
|
|
|
{
|
|
|
|
self.local.spawn_local(future)
|
|
|
|
}
|
|
|
|
|
2024-01-05 16:37:26 +00:00
|
|
|
#[cfg(not(feature = "wasm_build"))]
|
2023-10-30 04:35:06 +00:00
|
|
|
#[track_caller]
|
|
|
|
pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
|
|
|
|
where
|
|
|
|
F: Future + Send + 'static,
|
|
|
|
<F as Future>::Output: Send + 'static,
|
|
|
|
{
|
|
|
|
self.inner.spawn(future)
|
|
|
|
}
|
2022-06-27 15:15:43 +00:00
|
|
|
|
2024-01-05 16:37:26 +00:00
|
|
|
#[cfg(feature = "wasm_build")]
|
2023-10-30 04:35:06 +00:00
|
|
|
pub async fn run_until<F>(&self, future: F) -> F::Output
|
|
|
|
where
|
|
|
|
F: Future,
|
|
|
|
{
|
|
|
|
self.local.run_until(future).await
|
|
|
|
}
|
|
|
|
|
2024-01-05 16:37:26 +00:00
|
|
|
#[cfg(not(feature = "wasm_build"))]
|
2023-10-30 04:35:06 +00:00
|
|
|
pub async fn run_until<F>(&self, future: F) -> F::Output
|
|
|
|
where
|
|
|
|
F: Future,
|
|
|
|
{
|
|
|
|
future.await
|
|
|
|
}
|
|
|
|
|
2024-01-05 16:37:26 +00:00
|
|
|
#[cfg(feature = "wasm_build")]
|
2023-10-30 04:35:06 +00:00
|
|
|
#[track_caller]
|
|
|
|
pub fn block_on<F>(&self, f: F) -> F::Output
|
|
|
|
where
|
|
|
|
F: Future,
|
|
|
|
{
|
|
|
|
self.local.block_on(&self.inner, f)
|
|
|
|
}
|
|
|
|
|
2024-01-05 16:37:26 +00:00
|
|
|
#[cfg(not(feature = "wasm_build"))]
|
2023-10-30 04:35:06 +00:00
|
|
|
#[track_caller]
|
|
|
|
pub fn block_on<F>(&self, f: F) -> F::Output
|
|
|
|
where
|
|
|
|
F: Future,
|
|
|
|
{
|
|
|
|
self.inner.block_on(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-05 16:37:26 +00:00
|
|
|
#[cfg(feature = "wasm_build")]
|
2023-10-30 04:35:06 +00:00
|
|
|
pub fn default_tokio_runtime() -> io::Result<Runtime> {
|
|
|
|
runtime::Builder::new_current_thread()
|
|
|
|
.thread_name("dispatch-rt-st")
|
|
|
|
.build()
|
|
|
|
}
|
2022-06-27 15:15:43 +00:00
|
|
|
|
2024-01-05 16:37:26 +00:00
|
|
|
#[cfg(not(feature = "wasm_build"))]
|
2023-10-30 04:35:06 +00:00
|
|
|
pub fn default_tokio_runtime() -> io::Result<Runtime> {
|
2023-02-13 01:29:49 +00:00
|
|
|
runtime::Builder::new_multi_thread()
|
2023-10-30 04:35:06 +00:00
|
|
|
.thread_name("dispatch-rt-mt")
|
2023-02-13 01:29:49 +00:00
|
|
|
.enable_io()
|
|
|
|
.enable_time()
|
|
|
|
.on_thread_start(move || {
|
|
|
|
tracing::trace!(
|
|
|
|
"{:?} thread started: thread_id= {}",
|
2023-10-30 04:35:06 +00:00
|
|
|
std::thread::current(),
|
2023-02-13 01:29:49 +00:00
|
|
|
thread_id::get()
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.on_thread_stop(move || {
|
|
|
|
tracing::trace!(
|
|
|
|
"{:?} thread stopping: thread_id= {}",
|
2023-10-30 04:35:06 +00:00
|
|
|
std::thread::current(),
|
2023-02-13 01:29:49 +00:00
|
|
|
thread_id::get(),
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.build()
|
2022-06-27 15:15:43 +00:00
|
|
|
}
|