# uip/timer.c

Go to the documentation of this file.

    00001 /**
    00002  * \addtogroup timer
    00003  * @{
    00004  */
    00005
    00006 /**
    00007  * \file
    00008  * Timer library implementation.
    00009  * \author
    00010  * Adam Dunkels <adam@sics.se>
    00011  */
    00012
    00013 /*
    00014  * Copyright (c) 2004, Swedish Institute of Computer Science.
    00015  * All rights reserved.
    00016  *
    00017  * Redistribution and use in source and binary forms, with or without
    00018  * modification, are permitted provided that the following conditions
    00019  * are met:
    00020  * 1. Redistributions of source code must retain the above copyright
    00021  *    notice, this list of conditions and the following disclaimer.
    00022  * 2. Redistributions in binary form must reproduce the above copyright
    00023  *    notice, this list of conditions and the following disclaimer in the
    00024  *    documentation and/or other materials provided with the distribution.
    00025  * 3. Neither the name of the Institute nor the names of its contributors
    00026  *    may be used to endorse or promote products derived from this software
    00027  *    without specific prior written permission.
    00028  *
    00029  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
    00030  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    00031  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    00032  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
    00033  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    00034  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    00035  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    00036  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    00037  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    00038  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    00039  * SUCH DAMAGE.
    00040  *
    00041  * This file is part of the uIP TCP/IP stack
    00042  *
    00043  * Author: Adam Dunkels <adam@sics.se>
    00044  *
    00045  * $Id: timer.c,v 1.2 2006/06/12 08:00:30 adam Exp $
    00046  */
    00047
    00048 #include "clock.h"
    00049 #include "timer.h"
    00050
    00051 /*---------------------------------------------------------------------------*/
    00052 /**
    00053  * Set a timer.
    00054  *
    00055  * This function is used to set a timer for a time sometime in the
    00056  * future. The function timer_expired() will evaluate to true after
    00057  * the timer has expired.
    00058  *
    00059  * \param t A pointer to the timer
    00060  * \param interval The interval before the timer expires.
    00061  *
    00062  */
    00063 void
    00064 timer_set(struct timer *t, clock_time_t interval)
    00065 {
    00066   t->interval = interval;
    00067   t->start = clock_time();
    00068 }
    00069 /*---------------------------------------------------------------------------*/
    00070 /**
    00071  * Reset the timer with the same interval.
    00072  *
    00073  * This function resets the timer with the same interval that was
    00074  * given to the timer_set() function. The start point of the interval
    00075  * is the exact time that the timer last expired. Therefore, this
    00076  * function will cause the timer to be stable over time, unlike the
    00077  * timer_rester() function.
    00078  *
    00079  * \param t A pointer to the timer.
    00080  *
    00081  * \sa timer_restart()
    00082  */
    00083 void
    00084 timer_reset(struct timer *t)
    00085 {
    00086   t->start += t->interval;
    00087 }
    00088 /*---------------------------------------------------------------------------*/
    00089 /**
    00090  * Restart the timer from the current point in time
    00091  *
    00092  * This function restarts a timer with the same interval that was
    00093  * given to the timer_set() function. The timer will start at the
    00094  * current time.
    00095  *
    00096  * \note A periodic timer will drift if this function is used to reset
    00097  * it. For preioric timers, use the timer_reset() function instead.
    00098  *
    00099  * \param t A pointer to the timer.
    00100  *
    00101  * \sa timer_reset()
    00102  */
    00103 void
    00104 timer_restart(struct timer *t)
    00105 {
    00106   t->start = clock_time();
    00107 }
    00108 /*---------------------------------------------------------------------------*/
    00109 /**
    00110  * Check if a timer has expired.
    00111  *
    00112  * This function tests if a timer has expired and returns true or
    00113  * false depending on its status.
    00114  *
    00115  * \param t A pointer to the timer
    00116  *
    00117  * \return Non-zero if the timer has expired, zero otherwise.
    00118  *
    00119  */
    00120 int
    00121 timer_expired(struct timer *t)
    00122 {
    00123   return (clock_time_t)(clock_time() - t->start) >= (clock_time_t)t->interval;
    00124 }
    00125 /*---------------------------------------------------------------------------*/
    00126
    00127 /** @} */