use crate::{
compat::timer_compat::TIMERS,
preempt::arch_specific::task_create,
timer::{systimer_count, yield_task},
};
pub(crate) fn init_tasks() {
crate::preempt::allocate_main_task();
task_create(timer_task, core::ptr::null_mut(), 8192);
}
pub(crate) extern "C" fn timer_task(_param: *mut esp_wifi_sys::c_types::c_void) {
loop {
let current_timestamp = systimer_count();
let to_run = critical_section::with(|cs| unsafe {
let mut timers = TIMERS.borrow_ref_mut(cs);
let to_run = timers.find_next_due(current_timestamp);
if let Some(to_run) = to_run {
to_run.active = to_run.periodic;
if to_run.periodic {
to_run.started = current_timestamp;
}
Some(to_run.callback)
} else {
None
}
});
if let Some(to_run) = to_run {
trace!("trigger timer....");
to_run.call();
trace!("timer callback called");
} else {
yield_task();
}
}
}