1#![no_std]
2
3extern crate alloc;
4
5embedded_i18n::i18n!("locales");
6
7#[cfg(any(
8 test,
9 feature = "std",
10 target_os = "linux",
11 target_os = "macos",
12 target_os = "windows",
13 target_arch = "wasm32"
14))]
15extern crate std;
16
17#[cfg(all(feature = "profiling", target_os = "linux"))]
18pub mod profiling;
19
20#[cfg(any(test, feature = "test_harness"))]
21pub mod test_executor;
22
23#[cfg(feature = "crash")]
24pub mod active_task;
25
26pub mod metrics;
27
28mod environment_variable;
29mod error;
30mod join_handle;
31#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
32mod macrotask;
33mod manager;
34mod signal;
35mod task;
36#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
37mod time_driver;
38
39use core::{future::poll_fn, task::Poll, time::Duration};
40use embassy_time::{Instant, Timer};
41
42pub use embassy_executor;
43pub use environment_variable::*;
44pub use error::*;
45pub use join_handle::*;
46pub use manager::*;
47pub use signal::*;
48pub use task::*;
49pub use task_macros::{run, test};
50
51pub fn deadline_after_nanoseconds(nanoseconds: u64) -> Instant {
59 const fn gcd(a: u64, b: u64) -> u64 {
60 if b == 0 { a } else { gcd(b, a % b) }
61 }
62 const DIVISOR: u64 = gcd(embassy_time::TICK_HZ, 1_000_000_000);
63 const TICKS: u64 = embassy_time::TICK_HZ / DIVISOR;
64 const NANOSECONDS: u64 = 1_000_000_000 / DIVISOR;
65 const _: () = assert!(NANOSECONDS.checked_mul(TICKS).is_some());
67
68 let ticks = if nanoseconds == 0 {
69 0
70 } else {
71 let whole = (nanoseconds / NANOSECONDS).saturating_mul(TICKS);
72 let part = (nanoseconds % NANOSECONDS * TICKS).div_ceil(NANOSECONDS);
73 whole.saturating_add(part).saturating_add(1)
74 };
75 Instant::from_ticks(Instant::now().as_ticks().saturating_add(ticks))
76}
77
78pub async fn sleep(duration: impl Into<Duration>) {
80 let nanoseconds = u64::try_from(duration.into().as_nanos()).unwrap_or(u64::MAX);
81
82 Timer::at(deadline_after_nanoseconds(nanoseconds)).await
83}
84
85#[cfg(target_arch = "wasm32")]
87const THROTTLED_YIELD: Duration = Duration::from_millis(10);
88
89pub async fn yield_now() {
110 #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
111 macrotask::yield_now().await;
112
113 #[cfg(all(target_arch = "wasm32", not(target_os = "unknown")))]
116 sleep(THROTTLED_YIELD).await;
117
118 #[cfg(not(target_arch = "wasm32"))]
119 embassy_futures::yield_now().await;
120}
121
122pub async fn yield_throttled() {
134 #[cfg(target_arch = "wasm32")]
135 sleep(THROTTLED_YIELD).await;
136
137 #[cfg(not(target_arch = "wasm32"))]
138 yield_now().await;
139}
140
141pub async fn suspend(function: impl FnOnce(&mut core::task::Context<'_>)) {
144 let mut function_opt = Some(function);
145
146 poll_fn(|context| {
147 if let Some(f) = function_opt.take() {
148 f(context);
149 Poll::Pending
150 } else {
151 Poll::Ready(())
152 }
153 })
154 .await;
155}
156
157pub fn block_on<F: core::future::Future>(future: F) -> F::Output {
158 embassy_futures::block_on(future)
159}
160
161#[cfg(test)]
162mod tests {
163 use super::{Instant, deadline_after_nanoseconds};
164
165 #[test]
166 fn deadline_rounds_up_and_saturates() {
167 let now = Instant::now().as_ticks();
168 assert!(deadline_after_nanoseconds(0).as_ticks() >= now);
169 assert!(deadline_after_nanoseconds(1).as_ticks() > now);
171 assert!(
173 deadline_after_nanoseconds(1_000_000_000).as_ticks() >= now + embassy_time::TICK_HZ
174 );
175 let far = deadline_after_nanoseconds(u64::MAX).as_ticks();
177 assert!(far > deadline_after_nanoseconds(u64::MAX / 2).as_ticks());
178 assert!(far > now + embassy_time::TICK_HZ);
179 }
180}