Skip to main content

leodos_libcfs/runtime/
time.rs

1//! Asynchronous timer utilities.
2
3use crate::cfe::time::SysTime;
4use crate::cfe::duration::Duration;
5use core::future::Future;
6use core::pin::Pin;
7use core::task::{Context, Poll};
8
9/// Waits for a specified amount of time to pass.
10///
11/// This function is an async version of `std::thread::sleep`. It creates a future
12/// that will complete after the given duration has elapsed, without blocking the
13/// cFS task.
14pub fn sleep(duration: Duration) -> Sleep {
15    Sleep {
16        deadline: SysTime::now() + SysTime::from(duration),
17    }
18}
19
20/// A future that completes at a specified time.
21#[must_use = "futures do nothing unless you `.await` or poll them"]
22pub struct Sleep {
23    deadline: SysTime,
24}
25
26impl Future for Sleep {
27    type Output = ();
28
29    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
30        // Since our runtime polls continuously, we just need to check the current
31        // mission time against our deadline on each poll.
32        if SysTime::now() >= self.deadline {
33            Poll::Ready(())
34        } else {
35            Poll::Pending
36        }
37    }
38}