Hardy Griech's timer bugfix: avoid direct comparison for timer values since they may wrap. Instead, use the CLOCK_LT() that works correctly even for wrapped timers.

This commit is contained in:
adamdunkels 2008-08-15 19:16:09 +00:00
parent 86b835dd2c
commit 9324dc1e7d
2 changed files with 37 additions and 28 deletions

View File

@ -53,14 +53,23 @@
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: clock.h,v 1.6 2008/07/10 17:09:32 adamdunkels Exp $
* $Id: clock.h,v 1.7 2008/08/15 19:16:09 adamdunkels Exp $
*/
#ifndef __CLOCK_H__
#define __CLOCK_H__
#include "contiki-conf.h"
#define CLOCK_LT(a,b) ((signed short)((a)-(b)) < 0)
/**
* Check if a clock time value is less than another clock time value.
*
* This macro checks if a clock time value is less than another clock
* time value. This macro is needed to correctly handle wrap-around of
* clock time values.
*
*/
#define CLOCK_LT(a, b) ((clock_time_t)((a) - (b)) < \
((clock_time_t)(~((clock_time_t)0)) >> 1))
/**
* Initialize the clock library.

View File

@ -12,37 +12,37 @@
/*
* Copyright (c) 2004, Swedish Institute of Computer Science.
* All rights reserved.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
*
* Author: Adam Dunkels <adam@sics.se>
*
* $Id: timer.c,v 1.2 2007/05/27 11:11:28 oliverschmidt Exp $
* $Id: timer.c,v 1.3 2008/08/15 19:16:09 adamdunkels Exp $
*/
#include "contiki-conf.h"
@ -121,7 +121,7 @@ timer_restart(struct timer *t)
int
timer_expired(struct timer *t)
{
return (clock_time_t)(clock_time() - t->start) >= (clock_time_t)t->interval;
return CLOCK_LT(clock_time(), t->start + t->interval);
}
/*---------------------------------------------------------------------------*/