worldstone: add -s for statistical profiling
[minix.git] / lib / liblwip / core / timers.c
blobd28cb0843b19702798d9850278b6135df5967e1e
1 /**
2 * @file
3 * Stack-internal timers implementation.
4 * This file includes timer callbacks for stack-internal timers as well as
5 * functions to set up or stop timers and check for expired timers.
7 */
9 /*
10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
16 * 1. Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * OF SUCH DAMAGE.
35 * This file is part of the lwIP TCP/IP stack.
37 * Author: Adam Dunkels <adam@sics.se>
38 * Simon Goldschmidt
42 #include "lwip/opt.h"
44 #include "lwip/timers.h"
46 #if LWIP_TIMERS
48 #include "lwip/def.h"
49 #include "lwip/memp.h"
50 #include "lwip/tcpip.h"
52 #include "lwip/tcp_impl.h"
53 #include "lwip/ip_frag.h"
54 #include "netif/etharp.h"
55 #include "lwip/dhcp.h"
56 #include "lwip/autoip.h"
57 #include "lwip/igmp.h"
58 #include "lwip/dns.h"
61 /** The one and only timeout list */
62 static struct sys_timeo *next_timeout;
63 #if NO_SYS
64 static u32_t timeouts_last_time;
65 #endif /* NO_SYS */
67 #if LWIP_TCP
68 /** global variable that shows if the tcp timer is currently scheduled or not */
69 static int tcpip_tcp_timer_active;
71 /**
72 * Timer callback function that calls tcp_tmr() and reschedules itself.
74 * @param arg unused argument
76 static void
77 tcpip_tcp_timer(void *arg)
79 LWIP_UNUSED_ARG(arg);
81 /* call TCP timer handler */
82 tcp_tmr();
83 /* timer still needed? */
84 if (tcp_active_pcbs || tcp_tw_pcbs) {
85 /* restart timer */
86 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
87 } else {
88 /* disable timer */
89 tcpip_tcp_timer_active = 0;
93 /**
94 * Called from TCP_REG when registering a new PCB:
95 * the reason is to have the TCP timer only running when
96 * there are active (or time-wait) PCBs.
98 void
99 tcp_timer_needed(void)
101 /* timer is off but needed again? */
102 if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
103 /* enable and start timer */
104 tcpip_tcp_timer_active = 1;
105 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
108 #endif /* LWIP_TCP */
110 #if IP_REASSEMBLY
112 * Timer callback function that calls ip_reass_tmr() and reschedules itself.
114 * @param arg unused argument
116 static void
117 ip_reass_timer(void *arg)
119 LWIP_UNUSED_ARG(arg);
120 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip_reass_tmr()\n"));
121 ip_reass_tmr();
122 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
124 #endif /* IP_REASSEMBLY */
126 #if LWIP_ARP
128 * Timer callback function that calls etharp_tmr() and reschedules itself.
130 * @param arg unused argument
132 static void
133 arp_timer(void *arg)
135 LWIP_UNUSED_ARG(arg);
136 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: etharp_tmr()\n"));
137 etharp_tmr();
138 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
140 #endif /* LWIP_ARP */
142 #if LWIP_DHCP
144 * Timer callback function that calls dhcp_coarse_tmr() and reschedules itself.
146 * @param arg unused argument
148 static void
149 dhcp_timer_coarse(void *arg)
151 LWIP_UNUSED_ARG(arg);
152 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
153 dhcp_coarse_tmr();
154 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
158 * Timer callback function that calls dhcp_fine_tmr() and reschedules itself.
160 * @param arg unused argument
162 static void
163 dhcp_timer_fine(void *arg)
165 LWIP_UNUSED_ARG(arg);
166 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
167 dhcp_fine_tmr();
168 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
170 #endif /* LWIP_DHCP */
172 #if LWIP_AUTOIP
174 * Timer callback function that calls autoip_tmr() and reschedules itself.
176 * @param arg unused argument
178 static void
179 autoip_timer(void *arg)
181 LWIP_UNUSED_ARG(arg);
182 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: autoip_tmr()\n"));
183 autoip_tmr();
184 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
186 #endif /* LWIP_AUTOIP */
188 #if LWIP_IGMP
190 * Timer callback function that calls igmp_tmr() and reschedules itself.
192 * @param arg unused argument
194 static void
195 igmp_timer(void *arg)
197 LWIP_UNUSED_ARG(arg);
198 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: igmp_tmr()\n"));
199 igmp_tmr();
200 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
202 #endif /* LWIP_IGMP */
204 #if LWIP_DNS
206 * Timer callback function that calls dns_tmr() and reschedules itself.
208 * @param arg unused argument
210 static void
211 dns_timer(void *arg)
213 LWIP_UNUSED_ARG(arg);
214 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dns_tmr()\n"));
215 dns_tmr();
216 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
218 #endif /* LWIP_DNS */
220 /** Initialize this module */
221 void sys_timeouts_init(void)
223 #if IP_REASSEMBLY
224 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
225 #endif /* IP_REASSEMBLY */
226 #if LWIP_ARP
227 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
228 #endif /* LWIP_ARP */
229 #if LWIP_DHCP
230 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
231 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
232 #endif /* LWIP_DHCP */
233 #if LWIP_AUTOIP
234 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
235 #endif /* LWIP_AUTOIP */
236 #if LWIP_IGMP
237 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
238 #endif /* LWIP_IGMP */
239 #if LWIP_DNS
240 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
241 #endif /* LWIP_DNS */
243 #if NO_SYS
244 /* Initialise timestamp for sys_check_timeouts */
245 timeouts_last_time = sys_now();
246 #endif
250 * Create a one-shot timer (aka timeout). Timeouts are processed in the
251 * following cases:
252 * - while waiting for a message using sys_timeouts_mbox_fetch()
253 * - by calling sys_check_timeouts() (NO_SYS==1 only)
255 * @param msecs time in milliseconds after that the timer should expire
256 * @param handler callback function to call when msecs have elapsed
257 * @param arg argument to pass to the callback function
259 #if LWIP_DEBUG_TIMERNAMES
260 void
261 sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char* handler_name)
262 #else /* LWIP_DEBUG_TIMERNAMES */
263 void
264 sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
265 #endif /* LWIP_DEBUG_TIMERNAMES */
267 struct sys_timeo *timeout, *t;
269 timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
270 if (timeout == NULL) {
271 LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout != NULL);
272 return;
274 timeout->next = NULL;
275 timeout->h = handler;
276 timeout->arg = arg;
277 timeout->time = msecs;
278 #if LWIP_DEBUG_TIMERNAMES
279 timeout->handler_name = handler_name;
280 LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" handler=%s arg=%p\n",
281 (void *)timeout, msecs, handler_name, (void *)arg));
282 #endif /* LWIP_DEBUG_TIMERNAMES */
284 if (next_timeout == NULL) {
285 next_timeout = timeout;
286 return;
289 if (next_timeout->time > msecs) {
290 next_timeout->time -= msecs;
291 timeout->next = next_timeout;
292 next_timeout = timeout;
293 } else {
294 for(t = next_timeout; t != NULL; t = t->next) {
295 timeout->time -= t->time;
296 if (t->next == NULL || t->next->time > timeout->time) {
297 if (t->next != NULL) {
298 t->next->time -= timeout->time;
300 timeout->next = t->next;
301 t->next = timeout;
302 break;
309 * Go through timeout list (for this task only) and remove the first matching
310 * entry, even though the timeout has not triggered yet.
312 * @note This function only works as expected if there is only one timeout
313 * calling 'handler' in the list of timeouts.
315 * @param handler callback function that would be called by the timeout
316 * @param arg callback argument that would be passed to handler
318 void
319 sys_untimeout(sys_timeout_handler handler, void *arg)
321 struct sys_timeo *prev_t, *t;
323 if (next_timeout == NULL) {
324 return;
327 for (t = next_timeout, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
328 if ((t->h == handler) && (t->arg == arg)) {
329 /* We have a match */
330 /* Unlink from previous in list */
331 if (prev_t == NULL) {
332 next_timeout = t->next;
333 } else {
334 prev_t->next = t->next;
336 /* If not the last one, add time of this one back to next */
337 if (t->next != NULL) {
338 t->next->time += t->time;
340 memp_free(MEMP_SYS_TIMEOUT, t);
341 return;
344 return;
347 #if NO_SYS
349 /** Handle timeouts for NO_SYS==1 (i.e. without using
350 * tcpip_thread/sys_timeouts_mbox_fetch(). Uses sys_now() to call timeout
351 * handler functions when timeouts expire.
353 * Must be called periodically from your main loop.
355 void
356 sys_check_timeouts(void)
358 struct sys_timeo *tmptimeout;
359 u32_t diff;
360 sys_timeout_handler handler;
361 void *arg;
362 int had_one;
363 u32_t now;
365 now = sys_now();
366 if (next_timeout) {
367 /* this cares for wraparounds */
368 diff = LWIP_U32_DIFF(now, timeouts_last_time);
371 had_one = 0;
372 tmptimeout = next_timeout;
373 if (tmptimeout->time <= diff) {
374 /* timeout has expired */
375 had_one = 1;
376 timeouts_last_time = now;
377 diff -= tmptimeout->time;
378 next_timeout = tmptimeout->next;
379 handler = tmptimeout->h;
380 arg = tmptimeout->arg;
381 #if LWIP_DEBUG_TIMERNAMES
382 if (handler != NULL) {
383 LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%s arg=%p\n",
384 tmptimeout->handler_name, arg));
386 #endif /* LWIP_DEBUG_TIMERNAMES */
387 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
388 if (handler != NULL) {
389 handler(arg);
392 /* repeat until all expired timers have been called */
393 }while(had_one);
397 /** Set back the timestamp of the last call to sys_check_timeouts()
398 * This is necessary if sys_check_timeouts() hasn't been called for a long
399 * time (e.g. while saving energy) to prevent all timer functions of that
400 * period being called.
402 void
403 sys_restart_timeouts(void)
405 timeouts_last_time = sys_now();
408 #else /* NO_SYS */
411 * Wait (forever) for a message to arrive in an mbox.
412 * While waiting, timeouts are processed.
414 * @param mbox the mbox to fetch the message from
415 * @param msg the place to store the message
417 void
418 sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
420 u32_t time_needed;
421 struct sys_timeo *tmptimeout;
422 sys_timeout_handler handler;
423 void *arg;
425 again:
426 if (!next_timeout) {
427 time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
428 } else {
429 if (next_timeout->time > 0) {
430 time_needed = sys_arch_mbox_fetch(mbox, msg, next_timeout->time);
431 } else {
432 time_needed = SYS_ARCH_TIMEOUT;
435 if (time_needed == SYS_ARCH_TIMEOUT) {
436 /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message
437 could be fetched. We should now call the timeout handler and
438 deallocate the memory allocated for the timeout. */
439 tmptimeout = next_timeout;
440 next_timeout = tmptimeout->next;
441 handler = tmptimeout->h;
442 arg = tmptimeout->arg;
443 #if LWIP_DEBUG_TIMERNAMES
444 if (handler != NULL) {
445 LWIP_DEBUGF(TIMERS_DEBUG, ("stmf calling h=%s arg=%p\n",
446 tmptimeout->handler_name, arg));
448 #endif /* LWIP_DEBUG_TIMERNAMES */
449 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
450 if (handler != NULL) {
451 /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the
452 timeout handler function. */
453 LOCK_TCPIP_CORE();
454 handler(arg);
455 UNLOCK_TCPIP_CORE();
457 LWIP_TCPIP_THREAD_ALIVE();
459 /* We try again to fetch a message from the mbox. */
460 goto again;
461 } else {
462 /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout
463 occured. The time variable is set to the number of
464 milliseconds we waited for the message. */
465 if (time_needed < next_timeout->time) {
466 next_timeout->time -= time_needed;
467 } else {
468 next_timeout->time = 0;
474 #endif /* NO_SYS */
476 #else /* LWIP_TIMERS */
477 /* Satisfy the TCP code which calls this function */
478 void
479 tcp_timer_needed(void)
482 #endif /* LWIP_TIMERS */