• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 //! # ylong_runtime
15 //! A runtime for writing IO-bounded and CPU-bounded applications.
16 
17 #[cfg(all(
18     feature = "ffrt",
19     any(feature = "current_thread_runtime", feature = "multi_instance_runtime")
20 ))]
21 compile_error!("Feature ffrt can not be enabled with feature current_thread_runtime or feature multi_instance_runtime");
22 
23 #[cfg(all(feature = "ffrt", not(target_os = "linux")))]
24 compile_error!("Feature ffrt only works on linux currently");
25 
26 #[cfg(all(feature = "ffrt", feature = "metrics"))]
27 compile_error!("Feature ffrt can not be enabled with feature metrics");
28 
29 extern crate core;
30 
31 use std::future::Future;
32 #[macro_use]
33 pub(crate) mod macros;
34 
35 use crate::error::ScheduleError;
36 use crate::task::{JoinHandle, Task, TaskBuilder};
37 
38 pub mod builder;
39 pub mod error;
40 pub mod executor;
41 
42 #[cfg(feature = "ffrt")]
43 pub(crate) mod ffrt;
44 #[cfg(feature = "fs")]
45 pub mod fs;
46 pub mod futures;
47 pub mod io;
48 pub mod iter;
49 #[cfg(feature = "macros")]
50 mod select;
51 #[cfg(feature = "macros")]
52 pub use ylong_runtime_macros::tuple_form;
53 pub(crate) mod spawn;
54 #[cfg(feature = "sync")]
55 pub mod sync;
56 pub mod task;
57 
58 cfg_time! {
59     pub mod time;
60 }
61 
62 pub mod util;
63 cfg_metrics!(
64     mod metrics;
65     pub use metrics::Metrics;
66 );
67 
68 cfg_net! {
69     pub mod net;
70 }
71 
72 /// Using the default task setting, spawns a task onto the global runtime.
spawn<T, R>(task: T) -> JoinHandle<R> where T: Future<Output = R>, T: Send + 'static, R: Send + 'static,73 pub fn spawn<T, R>(task: T) -> JoinHandle<R>
74 where
75     T: Future<Output = R>,
76     T: Send + 'static,
77     R: Send + 'static,
78 {
79     TaskBuilder::new().spawn(task)
80 }
81 
82 /// Using the default task setting, spawns a blocking task.
spawn_blocking<T, R>(task: T) -> JoinHandle<R> where T: FnOnce() -> R, T: Send + 'static, R: Send + 'static,83 pub fn spawn_blocking<T, R>(task: T) -> JoinHandle<R>
84 where
85     T: FnOnce() -> R,
86     T: Send + 'static,
87     R: Send + 'static,
88 {
89     TaskBuilder::new().spawn_blocking(task)
90 }
91 
92 /// Blocks the current thread until the `Future` passed in is completed.
block_on<T>(task: T) -> T::Output where T: Future,93 pub fn block_on<T>(task: T) -> T::Output
94 where
95     T: Future,
96 {
97     let rt = executor::global_default_async();
98     rt.block_on(task)
99 }
100