Bugfix: should not invoke timer if timer list is empty.

This commit is contained in:
adamdunkels 2007-05-17 00:23:58 +00:00
parent 5b3923141f
commit c4b212898b

View file

@ -28,13 +28,13 @@
*
* This file is part of the Contiki operating system.
*
* @(#)$Id: rtimer.c,v 1.2 2007/03/28 20:28:22 adamdunkels Exp $
* @(#)$Id: rtimer.c,v 1.3 2007/05/17 00:23:58 adamdunkels Exp $
*/
#include "sys/rtimer.h"
#include "contiki.h"
#define LIST_SIZE 16
#define LIST_SIZE 4
static struct rtimer *rtimers[LIST_SIZE];
static u8_t next, firstempty;
@ -78,6 +78,12 @@ rtimer_set(struct rtimer *rtimer, rtimer_clock_t time, rtimer_clock_t duration,
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) {
@ -89,7 +95,7 @@ rtimer_set(struct rtimer *rtimer, rtimer_clock_t time, rtimer_clock_t duration,
/* Put the rtimer at the end of the rtimer list. */
rtimer->time = time;
rtimers[firstempty] = rtimer;
PRINTF("rtimer_post: putting rtimer %s as %d\n", rtimer->name, firstempty);
PRINTF("rtimer_post: putting rtimer %p as %d\n", rtimer, firstempty);
firstempty = (firstempty + 1) % LIST_SIZE;
@ -100,8 +106,8 @@ rtimer_set(struct rtimer *rtimer, rtimer_clock_t time, rtimer_clock_t duration,
run the rtimer_arch_schedule() function to get the ball rolling. */
if(firstempty == (next + 1) % LIST_SIZE) {
PRINTF("rtimer_set scheduling %d %s (%d)\n",
next, rtimers[next]->name, rtimers[next]->time);
PRINTF("rtimer_set scheduling %d %p (%d)\n",
next, rtimers[next], rtimers[next]->time);
rtimer_arch_schedule(rtimers[next]->time);
}
@ -114,13 +120,18 @@ rtimer_run_next(void)
int i, n;
struct rtimer *t;
/* Do not run timer if list is empty. */
if(next == firstempty) {
return;
}
t = rtimers[next];
/* Increase the pointer to the next rtimer. */
next = (next + 1) % LIST_SIZE;
/* Run the rtimer. */
PRINTF("rtimer_run_next running %s\n", t->name);
PRINTF("rtimer_run_next running %p\n", t);
t->func(t, t->ptr);
if(next == firstempty) {
@ -132,24 +143,24 @@ rtimer_run_next(void)
/* Find the next rtimer to run. */
n = next;
for(i = next; i != firstempty; i = (i + 1) % LIST_SIZE) {
PRINTF("rtimer_run_next checking %s (%d) against %s (%d)\n",
rtimers[i]->name, rtimers[i]->time,
rtimers[n]->name, rtimers[n]->time);
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 %s (%d)\n",
n, rtimers[n]->name, rtimers[n]->time);
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 %s (%d)\n",
next, rtimers[next]->name, rtimers[next]->time);
PRINTF("rtimer_run_next scheduling %d %p (%d)\n",
next, rtimers[next], rtimers[next]->time);
rtimer_arch_schedule(rtimers[next]->time);
}