Cancellable timers: sorted_list::remove, timer cancel, replace done flag with timer cancellation

This commit is contained in:
Ian Gulliver
2026-04-11 08:28:32 +09:00
parent 3a3c5873c3
commit e486f6501a
5 changed files with 48 additions and 18 deletions

View File

@@ -12,18 +12,27 @@ inline bool operator<(const timer_entry& a, const timer_entry& b) {
return absolute_time_diff_us(b.when, a.when) < 0;
}
using timer_handle = sorted_list<timer_entry, 16>::node*;
struct timer_queue {
sorted_list<timer_entry, 16> queue;
alarm_id_t alarm = -1;
volatile bool irq_pending = false;
void schedule(absolute_time_t when, std::function<void()> fn) {
queue.insert({when, std::move(fn)});
timer_handle schedule(absolute_time_t when, std::function<void()> fn) {
auto* n = queue.insert({when, std::move(fn)});
arm();
return n;
}
void schedule_ms(uint32_t ms, std::function<void()> fn) {
schedule(make_timeout_time_ms(ms), std::move(fn));
timer_handle schedule_ms(uint32_t ms, std::function<void()> fn) {
return schedule(make_timeout_time_ms(ms), std::move(fn));
}
bool cancel(timer_handle h) {
bool removed = queue.remove(h);
if (removed) arm();
return removed;
}
void run() {