Skip to content

Commit

Permalink
Avoid setting an alarm in the past (#1954)
Browse files Browse the repository at this point in the history
* Avoid setting an alarm in the past

Fixes #1953

* Call ta_time_us_64 when needed

Remove local "now" variable.
  • Loading branch information
peterharperuk authored Sep 26, 2024
1 parent d08f36c commit 9f56a47
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/common/pico_time/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ static void alarm_pool_irq_handler(void) {
uint timer_num = ta_timer_num(timer);
alarm_pool_t *pool = pools[timer_num][timer_alarm_num];
assert(pool->timer_alarm_num == timer_alarm_num);
int64_t now = (int64_t) ta_time_us_64(timer);
int64_t earliest_target;
// 1. clear force bits if we were forced (do this outside the loop, as forcing is hopefully rare)
ta_clear_force_irq(timer, timer_alarm_num);
Expand All @@ -159,7 +158,7 @@ static void alarm_pool_irq_handler(void) {
if (earliest_index >= 0) {
alarm_pool_entry_t *earliest_entry = &pool->entries[earliest_index];
earliest_target = earliest_entry->target;
if ((now - earliest_target) >= 0) {
if (((int64_t)ta_time_us_64(timer) - earliest_target) >= 0) {
// time to call the callback now (or in the past)
// note that an entry->target of < 0 means the entry has been canceled (not this is set
// by this function, in response to the entry having been queued by the cancel_alarm API
Expand Down Expand Up @@ -259,15 +258,14 @@ static void alarm_pool_irq_handler(void) {
index = next;
}
}
now = (int64_t) ta_time_us_64(timer);
earliest_index = pool->ordered_head;
if (earliest_index < 0) break;
// need to wait
alarm_pool_entry_t *earliest_entry = &pool->entries[earliest_index];
earliest_target = earliest_entry->target;
ta_set_timeout(timer, timer_alarm_num, earliest_target);
// check we haven't now past the target time; if not we don't want to loop again
} while ((earliest_target - now) <= 0);
} while ((earliest_target - (int64_t)ta_time_us_64(timer)) <= 0);
}

void alarm_pool_post_alloc_init(alarm_pool_t *pool, alarm_pool_timer_t *timer, uint hardware_alarm_num, uint max_timers) {
Expand Down

0 comments on commit 9f56a47

Please sign in to comment.