There were many bugs in the rtimer code, particularly one that made only one rtimer at a time work. We have a new rtimer system that we're testing, but in the meantime, I rewrote the rtimer code to explicitly support only one rtimer. Makes the code significantly simpler (and fixes a bug that was very hard to track down).

This commit is contained in:
adamdunkels 2009-12-08 23:55:17 +00:00
parent f9260c4fae
commit eff2203310
1 changed files with 18 additions and 85 deletions

View File

@ -42,7 +42,7 @@
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: rtimer.c,v 1.5 2007/10/23 20:33:19 adamdunkels Exp $
* @(#)$Id: rtimer.c,v 1.6 2009/12/08 23:55:17 adamdunkels Exp $
*/
#include "sys/rtimer.h"
@ -51,7 +51,7 @@
#ifdef RTIMER_CONF_NUM
#define LIST_SIZE RTIMER_CONF_NUM
#else
#define LIST_SIZE 4
#define LIST_SIZE 8
#endif
static struct rtimer *rtimers[LIST_SIZE];
@ -65,6 +65,8 @@ static u8_t next, firstempty;
#define PRINTF(...)
#endif
static struct rtimer *next_rtimer;
/*---------------------------------------------------------------------------*/
void
rtimer_init(void)
@ -79,108 +81,39 @@ rtimer_set(struct rtimer *rtimer, rtimer_clock_t time,
rtimer_clock_t duration,
rtimer_callback_t func, void *ptr)
{
int i;
int first = 0;
PRINTF("rtimer_set time %d\n", time);
if(next_rtimer == NULL) {
first = 1;
}
rtimer->func = func;
rtimer->ptr = ptr;
/* Check if rtimer queue is full. */
if(firstempty == (next - 1) % LIST_SIZE) {
PRINTF("rtimer_set: next %d firstempty %d full\n", next, firstempty);
return RTIMER_ERR_FULL;
}
/* Check if it is possible to run this rtimer at the requested
time. */
for(i = next; i != firstempty;
i = (i + 1) % LIST_SIZE) {
if(rtimers[i] == rtimer) {
/* Check if timer is already scheduled. If so, we do not
schedule it again. */
return RTIMER_ERR_ALREADY_SCHEDULED;
}
/* XXX: should check a range of time not just the same precise
moment. */
if(rtimers[i]->time == time) {
PRINTF("rtimer_set: next %d firstempty %d time %d == %d\n",
next, firstempty, rtimers[i]->time, time);
return RTIMER_ERR_TIME;
}
}
/* Put the rtimer at the end of the rtimer list. */
rtimer->time = time;
rtimers[firstempty] = rtimer;
PRINTF("rtimer_post: putting rtimer %p as %d\n", rtimer, firstempty);
next_rtimer = rtimer;
firstempty = (firstempty + 1) % LIST_SIZE;
/* PRINTF("rtimer_post: next %d firstempty %d scheduling soon\n",
next, firstempty);*/
/* Check if this is the first rtimer on the list. If so, we need to
run the rtimer_arch_schedule() function to get the ball rolling. */
if(firstempty == (next + 1) % LIST_SIZE) {
PRINTF("rtimer_set scheduling %d %p (%d)\n",
next, rtimers[next], rtimers[next]->time);
rtimer_arch_schedule(rtimers[next]->time);
if(first == 1) {
rtimer_arch_schedule(time);
}
return RTIMER_OK;
}
/*---------------------------------------------------------------------------*/
void
rtimer_run_next(void)
{
int i, n;
struct rtimer *t;
/* Do not run timer if list is empty. */
if(next == firstempty) {
if(next_rtimer == NULL) {
return;
}
t = rtimers[next];
/* Increase the pointer to the next rtimer. */
next = (next + 1) % LIST_SIZE;
/* Run the rtimer. */
PRINTF("rtimer_run_next running %p\n", t);
t = next_rtimer;
next_rtimer = NULL;
t->func(t, t->ptr);
if(next == firstempty) {
PRINTF("rtimer_run_next: empty rtimer list\n");
/* The list is empty, no more rtimers to schedule. */
return;
if(next_rtimer != NULL) {
rtimer_arch_schedule(next_rtimer->time);
}
/* Find the next rtimer to run. */
n = next;
for(i = next; i != firstempty; i = (i + 1) % LIST_SIZE) {
PRINTF("rtimer_run_next checking %p (%d) against %p (%d)\n",
rtimers[i], rtimers[i]->time,
rtimers[n], rtimers[n]->time);
if(RTIMER_CLOCK_LT(rtimers[i]->time, rtimers[n]->time)) {
n = i;
}
}
PRINTF("rtimer_run_next next rtimer is %d %p (%d)\n",
n, rtimers[n], rtimers[n]->time);
/* Put the next rtimer first in the rtimer list. */
t = rtimers[next];
rtimers[next] = rtimers[n];
rtimers[n] = t;
PRINTF("rtimer_run_next scheduling %d %p (%d)\n",
next, rtimers[next], rtimers[next]->time);
rtimer_arch_schedule(rtimers[next]->time);
return;
}
/*---------------------------------------------------------------------------*/