2 * (c) Copyright 1998-2001 by Rob Braun
3 * All rights reserved. The file named COPYRIGHT specifies the terms
4 * and conditions for redistribution.
14 /* A note on the usage of timers in these functions:
15 * The timers are composed of 3 elements, a pointer to a callback function,
16 * the expire time of the timer, and a unique (pseudo-monotomically increasing)
17 * identifier for the timer.
18 * Timers are kept in a pset_h (linked list) and are sorted at insertion
19 * time, with the nearest expire time first. The sort at insertion is to
20 * avoid examining non-expired timers in the xtimer_poll() function, and
21 * to be able to easily retrieve the next expiring timer.
22 * The timers are set in the main event loop, using select() as the
23 * timing device. select() sleeps until the next timer is set to expire
24 * (or until an FD is ready, in which case it also examines the timer list).
27 static pset_h xtimer_list = NULL;
29 static int xtimer_init( void )
31 if( xtimer_list != NULL )
34 xtimer_list = pset_create( 0, 0 );
35 if( xtimer_list == NULL )
41 static int xtimer_compfunc( const void *_a, const void *_b )
43 xtime_h **a = (xtime_h **)_a;
44 xtime_h **b = (xtime_h **)_b;
45 return ((*a)->when - (*b)->when);
49 * Adds a timer to the pset_h of timers, and sorts the timer list (least time
52 * Success: the timer ID which can be used to later remove the timer (>0)
55 int xtimer_add( void (*func)(void), time_t secs )
57 xtime_h *new_xtimer = NULL;
61 if( xtimer_list == NULL ) {
62 if( xtimer_init() < 0 )
66 new_xtimer = (xtime_h *)malloc(sizeof(xtime_h));
67 if( new_xtimer == NULL ) {
77 new_xtimer->timerfunc = func;
78 new_xtimer->when = tmptime + secs;
80 if( (count = pset_count( xtimer_list )) == 0 ) {
83 new_xtimer->xtid = ((xtime_h *)(pset_pointer(xtimer_list, count-1)))->xtid + 1;
86 if( pset_add( xtimer_list, new_xtimer ) == NULL ) {
91 pset_sort( xtimer_list, xtimer_compfunc );
93 return(new_xtimer->xtid);
97 * Traverses the pset_h of timers, and executes the callback for expired
98 * timers. Timers that are expired, and have their callback executed
99 * are removed from the list of timers.
101 int xtimer_poll(void)
105 if( xtimer_list == NULL )
108 for( i = 0; i < pset_count( xtimer_list ); i++ ) {
109 xtime_h *cur_timer = pset_pointer( xtimer_list, i );
110 time_t cur_time = time(NULL);
112 /* The list is sorted, low to high. If there's no
113 * timers left, return.
115 if( cur_timer->when > cur_time ) {
118 cur_timer->timerfunc();
119 pset_delete( xtimer_list, cur_timer );
129 * Removes a timer from the list of timers. Takes the timer id as an argument
134 int xtimer_remove(int xtid)
139 for( i = 0; i < pset_count( xtimer_list ); i++ ) {
140 xtime_h *cur_timer = pset_pointer( xtimer_list, i );
141 if( cur_timer->xtid == xtid ) {
142 pset_delete( xtimer_list, cur_timer );
153 * Returns the number of seconds until the next timer expires.
154 * Returns -1 when no timers are active.
156 time_t xtimer_nexttime(void)
160 if(xtimer_list == NULL)
163 if( pset_count(xtimer_list) == 0 )
166 ret = ((xtime_h *)pset_pointer(xtimer_list, 0))->when - time(NULL) ;