Skip to content

Commit 9f56a47

Browse files
Avoid setting an alarm in the past (#1954)
* Avoid setting an alarm in the past Fixes #1953 * Call ta_time_us_64 when needed Remove local "now" variable.
1 parent d08f36c commit 9f56a47

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

src/common/pico_time/time.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ static void alarm_pool_irq_handler(void) {
142142
uint timer_num = ta_timer_num(timer);
143143
alarm_pool_t *pool = pools[timer_num][timer_alarm_num];
144144
assert(pool->timer_alarm_num == timer_alarm_num);
145-
int64_t now = (int64_t) ta_time_us_64(timer);
146145
int64_t earliest_target;
147146
// 1. clear force bits if we were forced (do this outside the loop, as forcing is hopefully rare)
148147
ta_clear_force_irq(timer, timer_alarm_num);
@@ -159,7 +158,7 @@ static void alarm_pool_irq_handler(void) {
159158
if (earliest_index >= 0) {
160159
alarm_pool_entry_t *earliest_entry = &pool->entries[earliest_index];
161160
earliest_target = earliest_entry->target;
162-
if ((now - earliest_target) >= 0) {
161+
if (((int64_t)ta_time_us_64(timer) - earliest_target) >= 0) {
163162
// time to call the callback now (or in the past)
164163
// note that an entry->target of < 0 means the entry has been canceled (not this is set
165164
// by this function, in response to the entry having been queued by the cancel_alarm API
@@ -259,15 +258,14 @@ static void alarm_pool_irq_handler(void) {
259258
index = next;
260259
}
261260
}
262-
now = (int64_t) ta_time_us_64(timer);
263261
earliest_index = pool->ordered_head;
264262
if (earliest_index < 0) break;
265263
// need to wait
266264
alarm_pool_entry_t *earliest_entry = &pool->entries[earliest_index];
267265
earliest_target = earliest_entry->target;
268266
ta_set_timeout(timer, timer_alarm_num, earliest_target);
269267
// check we haven't now past the target time; if not we don't want to loop again
270-
} while ((earliest_target - now) <= 0);
268+
} while ((earliest_target - (int64_t)ta_time_us_64(timer)) <= 0);
271269
}
272270

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

0 commit comments

Comments
 (0)