1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

evtimer: refactor adding and deleting events

This commit is contained in:
Kees Bakker 2019-01-06 21:29:14 +01:00 committed by Kees Bakker
parent 6320fdebe9
commit 629e96ea80

View File

@ -31,64 +31,63 @@
#define ENABLE_DEBUG (0)
#include "debug.h"
/* XXX this function is intentionally non-static, since the optimizer can't
* handle the pointer hack in this function */
void evtimer_add_event_to_list(evtimer_t *evtimer, evtimer_event_t *event)
static void _add_event_to_list(evtimer_t *evtimer, evtimer_event_t *event)
{
DEBUG("evtimer: new event offset %" PRIu32 " ms\n", event->offset);
/* we want list->next to point to the first list element. thus we take the
* *address* of evtimer->events, then cast it from (evtimer_event_t **) to
* (evtimer_event_t*). After that, list->next actually equals
* evtimer->events. */
evtimer_event_t *list = (evtimer_event_t *)&evtimer->events;
evtimer_event_t **list = &evtimer->events;
while (list->next) {
evtimer_event_t *list_entry = list->next;
while (*list) {
/* Stop when new event time is nearer then next */
if (event->offset < list_entry->offset) {
if (event->offset < (*list)->offset) {
DEBUG("evtimer: next %" PRIu32 " < %" PRIu32 " ms\n",
event->offset, list_entry->offset);
event->offset, (*list)->offset);
break;
}
/* Set event offset relative to previous event */
event->offset -= list_entry->offset;
list = list->next;
event->offset -= (*list)->offset;
list = &(*list)->next;
}
DEBUG("evtimer: new event relativ offset %" PRIu32 " ms\n", event->offset);
/* Set found next bigger event after new event */
event->next = list->next;
if (list->next) {
event->next = *list;
if (*list) {
/* Set offset following event relative to new event */
evtimer_event_t *next_entry = list->next;
DEBUG("evtimer: recalculate offset for %" PRIu32 " ms\n",
next_entry->offset);
(*list)->offset);
next_entry->offset -= event->offset;
(*list)->offset -= event->offset;
DEBUG("evtimer: resulting new event offset %" PRIu32 " ms\n",
next_entry->offset);
(*list)->offset);
}
list->next = event;
*list = event;
}
static void _del_event_from_list(evtimer_t *evtimer, evtimer_event_t *event)
{
evtimer_event_t *list = (evtimer_event_t *) &evtimer->events;
evtimer_event_t **list = &evtimer->events;
while (list->next) {
evtimer_event_t *list_entry = list->next;
if (list_entry == event) {
list->next = event->next;
if (list->next) {
list_entry = list->next;
list_entry->offset += event->offset;
}
/* Find the entry to delete from the list */
while (*list) {
if (*list == event) {
break;
}
list = list->next;
list = &(*list)->next;
}
if (*list) {
/* This is the entry we want to remove */
*list = (*list)->next;
/* If the deleted entry was _not_ the last one
* then update 'offset' of the entry that
* followed.
*/
if (*list) {
(*list)->offset += event->offset;
}
}
}
@ -145,7 +144,7 @@ void evtimer_add(evtimer_t *evtimer, evtimer_event_t *event)
DEBUG("evtimer_add(): adding event with offset %" PRIu32 "\n", event->offset);
_update_head_offset(evtimer);
evtimer_add_event_to_list(evtimer, event);
_add_event_to_list(evtimer, event);
if (evtimer->events == event) {
_set_timer(&evtimer->timer, event->offset);
}