1use std::error::Error;
2use std::future::Future;
3
4#[cfg(all(feature = "runtime-monoio", feature = "runtime-tokio"))]
6compile_error!("Can't compile Ferron with both main runtimes enabled");
7#[cfg(not(any(feature = "runtime-monoio", feature = "runtime-tokio")))]
8compile_error!("Can't compile Ferron with no main runtimes enabled");
9
10#[cfg(feature = "runtime-monoio")]
12pub fn new_runtime(future: impl Future, enable_uring: bool) -> Result<(), Box<dyn Error + Send + Sync>> {
13 if enable_uring && monoio::utils::detect_uring() {
14 #[cfg(target_os = "linux")]
15 let mut rt = monoio::RuntimeBuilder::<monoio::IoUringDriver>::new()
16 .enable_all()
17 .attach_thread_pool(Box::new(BlockingThreadPool))
18 .build()?;
19 #[cfg(not(target_os = "linux"))]
20 let mut rt = monoio::RuntimeBuilder::<monoio::LegacyDriver>::new()
21 .enable_all()
22 .attach_thread_pool(Box::new(BlockingThreadPool))
23 .build()?;
24 rt.block_on(future);
25 } else {
26 let mut rt = monoio::RuntimeBuilder::<monoio::LegacyDriver>::new()
27 .enable_all()
28 .build()?;
29 rt.block_on(future);
30 }
31 Ok(())
32}
33
34#[cfg(feature = "runtime-tokio")]
36pub fn new_runtime(future: impl Future, _enable_uring: bool) -> Result<(), Box<dyn Error + Send + Sync>> {
37 let rt = tokio::runtime::Builder::new_current_thread().enable_all().build()?;
38 rt.block_on(async move {
39 let local_set = tokio::task::LocalSet::new();
40 local_set.run_until(future).await;
41 });
42 Ok(())
43}
44
45pub use ferron_common::runtime::*;
46
47#[cfg(feature = "runtime-monoio")]
49struct BlockingThreadPool;
50
51#[cfg(feature = "runtime-monoio")]
52impl monoio::blocking::ThreadPool for BlockingThreadPool {
53 #[inline]
54 fn schedule_task(&self, task: monoio::blocking::BlockingTask) {
55 blocking::unblock(move || task.run()).detach();
56 }
57}