diff --git a/os/rt/include/chlists.h b/os/rt/include/chlists.h index d02fbad6b..4a374f57b 100644 --- a/os/rt/include/chlists.h +++ b/os/rt/include/chlists.h @@ -522,6 +522,43 @@ static inline void ch_dlist_insert_before(ch_delta_list_t *dlhp, dlhp->prev = dlp; } +/** + * @brief Inserts an element in a delta list. + * + * @param[in] dlhp pointer to the delta list header element + * @param[in] dlep element to be inserted before the header element + * @param[in] delta delta of the element to be inserted + * + * @notapi + */ +static inline void ch_dlist_insert(ch_delta_list_t *dlhp, + ch_delta_list_t *dlep, + sysinterval_t delta) { + ch_delta_list_t *dlp; + + /* The delta list is scanned in order to find the correct position for + this element. */ + dlp = dlhp->next; + while (dlp->delta < delta) { + /* Debug assert if the element is already in the list.*/ + chDbgAssert(dlp != dlep, "element already in list"); + + delta -= dlp->delta; + dlp = dlp->next; + } + + /* The timer is inserted in the delta list.*/ + ch_dlist_insert_before(dlp, dlep, delta); + + /* Adjusting delta for the following element.*/ + dlp->delta -= delta; + + /* Special case when the inserted element is in last position in the list, + the value in the header must be restored, just doing it is faster than + checking then doing.*/ + dlhp->delta = (sysinterval_t)-1; +} + /** * @brief Dequeues an element from the delta list. * diff --git a/os/rt/src/chvt.c b/os/rt/src/chvt.c index 23bdf157b..4d8a65c09 100644 --- a/os/rt/src/chvt.c +++ b/os/rt/src/chvt.c @@ -61,39 +61,6 @@ static inline bool is_timer(ch_delta_list_t *dlhp, ch_delta_list_t *dlp) { return (bool)(dlp != dlhp); } - -#if 0 -/** - * @brief Delta list compression. - * - * @param[in] vtlp pointer to the delta list to be compressed - * @param[in] deltanow interval to be compacted starting from "lasttime" - * - * @notapi - */ -static void vt_list_compress(virtual_timers_list_t *vtlp, - sysinterval_t deltanow) { - ch_delta_list_t *dlp = vtlp->dlist.next; - - vtlp->lasttime = chTimeAddX(vtlp->lasttime, deltanow); - - /* The loop is bounded because the delta list header has the delta field - set to (sysinterval_t)-1 which is larger than all deltas.*/ - while (dlp->delta < deltanow) { - deltanow -= dlp->delta; - dlp->delta = (sysinterval_t)0; - dlp = dlp->next; - } - - /* Adjusting next timer in the list, if any.*/ - if (is_timer(&vtlp->dlist, dlp)) { - - chDbgAssert(deltanow <= dlp->delta, "invalid delta"); - - dlp->delta -= deltanow; - } -} -#endif #endif /** @@ -103,7 +70,6 @@ static void vt_enqueue(virtual_timers_list_t *vtlp, virtual_timer_t *vtp, systime_t now, sysinterval_t delay) { - ch_delta_list_t *dlp; sysinterval_t delta; #if CH_CFG_ST_TIMEDELTA > 0 @@ -148,7 +114,6 @@ static void vt_enqueue(virtual_timers_list_t *vtlp, delta is shortened to make it fit the numeric range, the timer will be triggered "deltanow" cycles earlier.*/ if (delta < deltanow) { -// vt_list_compress(vtlp, deltanow); delta = delay; } @@ -161,7 +126,8 @@ static void vt_enqueue(virtual_timers_list_t *vtlp, and next deadline.*/ deadline_delta = delta; #if CH_CFG_INTERVALS_SIZE > CH_CFG_ST_RESOLUTION - /* The delta could be too large for the physical timer to handle.*/ + /* The delta could be too large for the physical timer to handle + this can happen when: sizeof (systime_t) < sizeof (sysinterval_t).*/ if (deadline_delta > (sysinterval_t)TIME_MAX_SYSTIME) { deadline_delta = (sysinterval_t)TIME_MAX_SYSTIME; } @@ -176,26 +142,7 @@ static void vt_enqueue(virtual_timers_list_t *vtlp, delta = delay; #endif /* CH_CFG_ST_TIMEDELTA == 0 */ - /* The delta list is scanned in order to find the correct position for - this timer. */ - dlp = vtlp->dlist.next; - while (dlp->delta < delta) { - /* Debug assert if the timer is already in the list.*/ - chDbgAssert(dlp != &vtp->dlist, "timer already armed"); - - delta -= dlp->delta; - dlp = dlp->next; - } - - /* The timer is inserted in the delta list.*/ - ch_dlist_insert_before(dlp, &vtp->dlist, delta); - - /* Calculate new delta for the following entry.*/ - dlp->delta -= delta; - - /* Special case when the timer is in last position in the list, the - value in the header must be restored.*/ - vtlp->dlist.delta = (sysinterval_t)-1; + ch_dlist_insert(&vtlp->dlist, &vtp->dlist, delta); } /*===========================================================================*/