From 030a86615cb7fc6ca2fda1b977dba29bb4102723 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Sun, 4 Aug 2019 21:51:19 +0200 Subject: [PATCH] embedded: streamline timer management using btstack_time_delta --- platform/embedded/btstack_run_loop_embedded.c | 50 ++++--------------- 1 file changed, 11 insertions(+), 39 deletions(-) diff --git a/platform/embedded/btstack_run_loop_embedded.c b/platform/embedded/btstack_run_loop_embedded.c index 19a1452c1..1e145ee1d 100644 --- a/platform/embedded/btstack_run_loop_embedded.c +++ b/platform/embedded/btstack_run_loop_embedded.c @@ -58,6 +58,7 @@ #include "btstack_run_loop.h" #include "btstack_run_loop_embedded.h" #include "btstack_linked_list.h" +#include "btstack_util.h" #include "hal_tick.h" #include "hal_cpu.h" @@ -119,53 +120,24 @@ static void btstack_run_loop_embedded_set_timer(btstack_timer_source_t *ts, uint #endif } -// under the assumption that a tick value is +/- 2^30 away from now, calculate the upper bits of the tick value -static int btstack_run_loop_embedded_reconstruct_higher_bits(uint32_t now, uint32_t ticks){ - int32_t delta = ticks - now; - if (delta >= 0){ - if (ticks >= now) { - return 0; - } else { - return 1; - } - } else { - if (ticks < now) { - return 0; - } else { - return -1; - } - } -} - /** * Add timer to run_loop (keep list sorted) */ static void btstack_run_loop_embedded_add_timer(btstack_timer_source_t *ts){ #ifdef TIMER_SUPPORT - -#ifdef HAVE_EMBEDDED_TICK - uint32_t now = system_ticks; -#endif -#ifdef HAVE_EMBEDDED_TIME_MS - uint32_t now = hal_time_ms(); -#endif - - uint32_t new_low = ts->timeout; - int new_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, new_low); - btstack_linked_item_t *it; for (it = (btstack_linked_item_t *) &timers; it->next ; it = it->next){ // don't add timer that's already in there - if ((btstack_timer_source_t *) it->next == ts){ + btstack_timer_source_t * next = (btstack_timer_source_t *) it->next; + if (next == ts){ log_error( "btstack_run_loop_timer_add error: timer to add already in list!"); return; } - uint32_t next_low = ((btstack_timer_source_t *) it->next)->timeout; - int next_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, next_low); - if (new_high < next_high || ((new_high == next_high) && (new_low < next_low))){ - break; - } + // exit if new timeout before list timeout + int32_t delta = btstack_time_delta(ts->timeout, next->timeout); + if (delta < 0) break; } + ts->item.next = it->next; it->next = (btstack_linked_item_t *) ts; #endif @@ -229,10 +201,10 @@ void btstack_run_loop_embedded_execute_once(void) { // process timers while (timers) { - btstack_timer_source_t *ts = (btstack_timer_source_t *) timers; - uint32_t timeout_low = ts->timeout; - int timeout_high = btstack_run_loop_embedded_reconstruct_higher_bits(now, timeout_low); - if (timeout_high > 0 || ((timeout_high == 0) && (timeout_low > now))) break; + btstack_timer_source_t * ts = (btstack_timer_source_t *) timers; + int32_t delta = btstack_time_delta(ts->timeout, now); + if (delta > 0) break; + btstack_run_loop_embedded_remove_timer(ts); ts->process(ts); }