pistonite_cu/co/
runtime.rs1use std::sync::LazyLock;
2
3use tokio::runtime::{Builder, Runtime};
4
5use crate::co::Handle;
6
7#[cfg(not(feature = "coroutine-heavy"))]
9static RUNTIME: LazyLock<Runtime> = LazyLock::new(|| {
10 Builder::new_current_thread()
11 .enable_all()
12 .build()
13 .expect("cannot create current-thread tokio runtime")
14});
15
16static BACKGROUND_RUNTIME: LazyLock<Runtime> = LazyLock::new(|| {
18 #[cfg(feature = "coroutine-heavy")]
19 {
20 Builder::new_multi_thread()
21 .enable_all()
22 .build()
23 .expect("cannot create heavy background tokio runtime")
24 }
25 #[cfg(not(feature = "coroutine-heavy"))]
26 {
27 Builder::new_multi_thread()
28 .worker_threads(1)
29 .enable_all()
30 .build()
31 .expect("cannot create background tokio runtime")
32 }
33});
34
35pub(crate) fn foreground() -> &'static Runtime {
37 #[cfg(not(feature = "coroutine-heavy"))]
41 {
42 &RUNTIME
43 }
44 #[cfg(feature = "coroutine-heavy")]
45 {
46 &BACKGROUND_RUNTIME
47 }
48}
49
50pub(crate) fn background() -> &'static Runtime {
51 &BACKGROUND_RUNTIME
52}
53
54#[inline(always)]
65#[cfg(not(feature = "coroutine-heavy"))]
66pub fn block<F>(future: F) -> F::Output
67where
68 F: Future,
69{
70 RUNTIME.block_on(future)
71}
72
73#[inline(always)]
75pub fn spawn<F>(future: F) -> Handle<F::Output>
76where
77 F: Future + Send + 'static,
78 F::Output: Send + 'static,
79{
80 Handle(BACKGROUND_RUNTIME.spawn(future))
81}
82
83#[inline(always)]
88#[cfg(feature = "coroutine-heavy")]
89pub fn spawn_blocking<F, R>(func: F) -> Handle<F::Output>
90where
91 F: FnOnce() -> R + Send + 'static,
92 R: Send + 'static,
93{
94 Handle(BACKGROUND_RUNTIME.spawn_blocking(func))
95}
96
97#[inline(always)]
99pub fn run<F>(future: F) -> F::Output
100where
101 F: Future,
102{
103 BACKGROUND_RUNTIME.block_on(future)
104}