|
1 | 1 | #![cfg_attr(loom, allow(unused_imports))]
|
2 | 2 |
|
3 | 3 | use crate::runtime::handle::Handle;
|
4 |
| -#[cfg(tokio_unstable)] |
5 |
| -use crate::runtime::TaskMeta; |
6 | 4 | use crate::runtime::{blocking, driver, Callback, HistogramBuilder, Runtime, TaskCallback};
|
| 5 | +#[cfg(tokio_unstable)] |
| 6 | +use crate::runtime::{LocalOptions, LocalRuntime, TaskMeta}; |
7 | 7 | use crate::util::rand::{RngSeed, RngSeedGenerator};
|
8 | 8 |
|
| 9 | +use crate::runtime::blocking::BlockingPool; |
| 10 | +use crate::runtime::scheduler::CurrentThread; |
9 | 11 | use std::fmt;
|
10 | 12 | use std::io;
|
| 13 | +use std::thread::ThreadId; |
11 | 14 | use std::time::Duration;
|
12 | 15 |
|
13 | 16 | /// Builds Tokio Runtime with custom configuration values.
|
@@ -800,6 +803,37 @@ impl Builder {
|
800 | 803 | }
|
801 | 804 | }
|
802 | 805 |
|
| 806 | + /// Creates the configured `LocalRuntime`. |
| 807 | + /// |
| 808 | + /// The returned `LocalRuntime` instance is ready to spawn tasks. |
| 809 | + /// |
| 810 | + /// # Panics |
| 811 | + /// This will panic if `current_thread` is not the selected runtime flavor. |
| 812 | + /// All other runtime flavors are unsupported by [`LocalRuntime`]. |
| 813 | + /// |
| 814 | + /// [`LocalRuntime`]: [crate::runtime::LocalRuntime] |
| 815 | + /// |
| 816 | + /// # Examples |
| 817 | + /// |
| 818 | + /// ``` |
| 819 | + /// use tokio::runtime::Builder; |
| 820 | + /// |
| 821 | + /// let rt = Builder::new_current_thread().build_local(&mut Default::default()).unwrap(); |
| 822 | + /// |
| 823 | + /// rt.block_on(async { |
| 824 | + /// println!("Hello from the Tokio runtime"); |
| 825 | + /// }); |
| 826 | + /// ``` |
| 827 | + #[allow(unused_variables, unreachable_patterns)] |
| 828 | + #[cfg(tokio_unstable)] |
| 829 | + #[cfg_attr(docsrs, doc(cfg(tokio_unstable)))] |
| 830 | + pub fn build_local(&mut self, options: &LocalOptions) -> io::Result<LocalRuntime> { |
| 831 | + match &self.kind { |
| 832 | + Kind::CurrentThread => self.build_current_thread_local_runtime(), |
| 833 | + _ => panic!("Only current_thread is supported when building a local runtime"), |
| 834 | + } |
| 835 | + } |
| 836 | + |
803 | 837 | fn get_cfg(&self, workers: usize) -> driver::Cfg {
|
804 | 838 | driver::Cfg {
|
805 | 839 | enable_pause_time: match self.kind {
|
@@ -1191,8 +1225,40 @@ impl Builder {
|
1191 | 1225 | }
|
1192 | 1226 |
|
1193 | 1227 | fn build_current_thread_runtime(&mut self) -> io::Result<Runtime> {
|
1194 |
| - use crate::runtime::scheduler::{self, CurrentThread}; |
1195 |
| - use crate::runtime::{runtime::Scheduler, Config}; |
| 1228 | + use crate::runtime::runtime::Scheduler; |
| 1229 | + |
| 1230 | + let (scheduler, handle, blocking_pool) = |
| 1231 | + self.build_current_thread_runtime_components(None)?; |
| 1232 | + |
| 1233 | + Ok(Runtime::from_parts( |
| 1234 | + Scheduler::CurrentThread(scheduler), |
| 1235 | + handle, |
| 1236 | + blocking_pool, |
| 1237 | + )) |
| 1238 | + } |
| 1239 | + |
| 1240 | + #[cfg(tokio_unstable)] |
| 1241 | + fn build_current_thread_local_runtime(&mut self) -> io::Result<LocalRuntime> { |
| 1242 | + use crate::runtime::local_runtime::LocalRuntimeScheduler; |
| 1243 | + |
| 1244 | + let tid = std::thread::current().id(); |
| 1245 | + |
| 1246 | + let (scheduler, handle, blocking_pool) = |
| 1247 | + self.build_current_thread_runtime_components(Some(tid))?; |
| 1248 | + |
| 1249 | + Ok(LocalRuntime::from_parts( |
| 1250 | + LocalRuntimeScheduler::CurrentThread(scheduler), |
| 1251 | + handle, |
| 1252 | + blocking_pool, |
| 1253 | + )) |
| 1254 | + } |
| 1255 | + |
| 1256 | + fn build_current_thread_runtime_components( |
| 1257 | + &mut self, |
| 1258 | + local_tid: Option<ThreadId>, |
| 1259 | + ) -> io::Result<(CurrentThread, Handle, BlockingPool)> { |
| 1260 | + use crate::runtime::scheduler; |
| 1261 | + use crate::runtime::Config; |
1196 | 1262 |
|
1197 | 1263 | let (driver, driver_handle) = driver::Driver::new(self.get_cfg(1))?;
|
1198 | 1264 |
|
@@ -1227,17 +1293,14 @@ impl Builder {
|
1227 | 1293 | seed_generator: seed_generator_1,
|
1228 | 1294 | metrics_poll_count_histogram: self.metrics_poll_count_histogram_builder(),
|
1229 | 1295 | },
|
| 1296 | + local_tid, |
1230 | 1297 | );
|
1231 | 1298 |
|
1232 | 1299 | let handle = Handle {
|
1233 | 1300 | inner: scheduler::Handle::CurrentThread(handle),
|
1234 | 1301 | };
|
1235 | 1302 |
|
1236 |
| - Ok(Runtime::from_parts( |
1237 |
| - Scheduler::CurrentThread(scheduler), |
1238 |
| - handle, |
1239 |
| - blocking_pool, |
1240 |
| - )) |
| 1303 | + Ok((scheduler, handle, blocking_pool)) |
1241 | 1304 | }
|
1242 | 1305 |
|
1243 | 1306 | fn metrics_poll_count_histogram_builder(&self) -> Option<HistogramBuilder> {
|
|
0 commit comments