2021-11-22 16:28:30 +00:00
|
|
|
use super::*;
|
|
|
|
use crate::intf::*;
|
|
|
|
use core::sync::atomic::{AtomicU64, Ordering};
|
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
|
|
|
|
cfg_if! {
|
|
|
|
if #[cfg(target_arch = "wasm32")] {
|
|
|
|
type TickTaskRoutine =
|
2022-06-13 00:58:02 +00:00
|
|
|
dyn Fn(StopToken, u64, u64) -> PinBoxFuture<Result<(), String>> + 'static;
|
2021-11-22 16:28:30 +00:00
|
|
|
} else {
|
|
|
|
type TickTaskRoutine =
|
2022-06-13 00:58:02 +00:00
|
|
|
dyn Fn(StopToken, u64, u64) -> SendPinBoxFuture<Result<(), String>> + Send + Sync + 'static;
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-04 04:58:26 +00:00
|
|
|
/// Runs a single-future background processing task, attempting to run it once every 'tick period' microseconds.
|
|
|
|
/// If the prior tick is still running, it will allow it to finish, and do another tick when the timer comes around again.
|
|
|
|
/// One should attempt to make tasks short-lived things that run in less than the tick period if you want things to happen with regular periodicity.
|
2021-11-22 16:28:30 +00:00
|
|
|
pub struct TickTask {
|
|
|
|
last_timestamp_us: AtomicU64,
|
|
|
|
tick_period_us: u64,
|
|
|
|
routine: OnceCell<Box<TickTaskRoutine>>,
|
2022-06-13 00:58:02 +00:00
|
|
|
stop_source: AsyncMutex<Option<StopSource>>,
|
|
|
|
single_future: MustJoinSingleFuture<Result<(), String>>,
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TickTask {
|
|
|
|
pub fn new_us(tick_period_us: u64) -> Self {
|
|
|
|
Self {
|
|
|
|
last_timestamp_us: AtomicU64::new(0),
|
2021-11-27 17:44:21 +00:00
|
|
|
tick_period_us,
|
2021-11-22 16:28:30 +00:00
|
|
|
routine: OnceCell::new(),
|
2022-06-13 00:58:02 +00:00
|
|
|
stop_source: AsyncMutex::new(None),
|
|
|
|
single_future: MustJoinSingleFuture::new(),
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 14:53:01 +00:00
|
|
|
pub fn new_ms(tick_period_ms: u32) -> Self {
|
|
|
|
Self {
|
|
|
|
last_timestamp_us: AtomicU64::new(0),
|
|
|
|
tick_period_us: (tick_period_ms as u64) * 1000u64,
|
|
|
|
routine: OnceCell::new(),
|
2022-06-13 00:58:02 +00:00
|
|
|
stop_source: AsyncMutex::new(None),
|
|
|
|
single_future: MustJoinSingleFuture::new(),
|
2022-01-27 14:53:01 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-22 16:28:30 +00:00
|
|
|
pub fn new(tick_period_sec: u32) -> Self {
|
|
|
|
Self {
|
|
|
|
last_timestamp_us: AtomicU64::new(0),
|
|
|
|
tick_period_us: (tick_period_sec as u64) * 1000000u64,
|
|
|
|
routine: OnceCell::new(),
|
2022-06-13 00:58:02 +00:00
|
|
|
stop_source: AsyncMutex::new(None),
|
|
|
|
single_future: MustJoinSingleFuture::new(),
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg_if! {
|
|
|
|
if #[cfg(target_arch = "wasm32")] {
|
|
|
|
pub fn set_routine(
|
|
|
|
&self,
|
2022-06-13 00:58:02 +00:00
|
|
|
routine: impl Fn(StopToken, u64, u64) -> PinBoxFuture<Result<(), String>> + 'static,
|
2021-11-22 16:28:30 +00:00
|
|
|
) {
|
|
|
|
self.routine.set(Box::new(routine)).map_err(drop).unwrap();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
pub fn set_routine(
|
|
|
|
&self,
|
2022-06-13 00:58:02 +00:00
|
|
|
routine: impl Fn(StopToken, u64, u64) -> SendPinBoxFuture<Result<(), String>> + Send + Sync + 'static,
|
2021-11-22 16:28:30 +00:00
|
|
|
) {
|
|
|
|
self.routine.set(Box::new(routine)).map_err(drop).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 00:58:02 +00:00
|
|
|
pub async fn stop(&self) -> Result<(), String> {
|
|
|
|
// drop the stop source if we have one
|
|
|
|
let opt_stop_source = &mut *self.stop_source.lock().await;
|
|
|
|
if opt_stop_source.is_none() {
|
|
|
|
// already stopped, just return
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
*opt_stop_source = None;
|
|
|
|
|
|
|
|
// wait for completion of the tick task
|
|
|
|
match self.single_future.join().await {
|
2021-11-22 16:28:30 +00:00
|
|
|
Ok(Some(Err(err))) => Err(err),
|
|
|
|
_ => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn tick(&self) -> Result<(), String> {
|
|
|
|
let now = get_timestamp();
|
|
|
|
let last_timestamp_us = self.last_timestamp_us.load(Ordering::Acquire);
|
|
|
|
|
|
|
|
if last_timestamp_us == 0u64 || (now - last_timestamp_us) >= self.tick_period_us {
|
|
|
|
// Run the singlefuture
|
2022-06-13 00:58:02 +00:00
|
|
|
let opt_stop_source = &mut *self.stop_source.lock().await;
|
|
|
|
let stop_source = StopSource::new();
|
2021-11-22 16:28:30 +00:00
|
|
|
match self
|
|
|
|
.single_future
|
2022-06-13 00:58:02 +00:00
|
|
|
.single_spawn(self.routine.get().unwrap()(
|
|
|
|
stop_source.token(),
|
|
|
|
last_timestamp_us,
|
|
|
|
now,
|
|
|
|
))
|
2021-11-22 16:28:30 +00:00
|
|
|
.await
|
|
|
|
{
|
2022-06-13 00:58:02 +00:00
|
|
|
// Single future ran this tick
|
|
|
|
Ok(Some(ret)) => {
|
|
|
|
// Set new timer
|
2021-11-22 16:28:30 +00:00
|
|
|
self.last_timestamp_us.store(now, Ordering::Release);
|
2022-06-13 00:58:02 +00:00
|
|
|
// Save new stopper
|
|
|
|
*opt_stop_source = Some(stop_source);
|
|
|
|
ret
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
2022-06-13 00:58:02 +00:00
|
|
|
// Single future did not run this tick
|
2021-11-22 16:28:30 +00:00
|
|
|
Ok(None) | Err(()) => {
|
|
|
|
// If the execution didn't happen this time because it was already running
|
|
|
|
// then we should try again the next tick and not reset the timestamp so we try as soon as possible
|
2022-06-13 00:58:02 +00:00
|
|
|
Ok(())
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-13 00:58:02 +00:00
|
|
|
} else {
|
|
|
|
// It's not time yet
|
|
|
|
Ok(())
|
2021-11-22 16:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|