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.
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
35 * This file is part of the lwIP TCP/IP stack.
37 * Author: Adam Dunkels <adam@sics.se>
44 #include "lwip/timeouts.h"
45 #include "lwip/priv/tcp_priv.h"
48 #include "lwip/memp.h"
49 #include "lwip/priv/tcpip_priv.h"
51 #include "lwip/ip4_frag.h"
52 #include "lwip/etharp.h"
53 #include "lwip/dhcp.h"
54 #include "lwip/autoip.h"
55 #include "lwip/igmp.h"
58 #include "lwip/ip6_frag.h"
59 #include "lwip/mld6.h"
61 #include "lwip/pbuf.h"
63 #if LWIP_DEBUG_TIMERNAMES
64 #define HANDLER(x) x, #x
65 #else /* LWIP_DEBUG_TIMERNAMES */
67 #endif /* LWIP_DEBUG_TIMERNAMES */
69 /** This array contains all stack-internal cyclic timers. To get the number of
70 * timers, use LWIP_ARRAYSIZE() */
71 const struct lwip_cyclic_timer lwip_cyclic_timers[] = {
74 {IP_TMR_INTERVAL, HANDLER(ip_reass_tmr)},
75 #endif /* IP_REASSEMBLY */
77 {ARP_TMR_INTERVAL, HANDLER(etharp_tmr)},
80 {DHCP_COARSE_TIMER_MSECS, HANDLER(dhcp_coarse_tmr)},
81 {DHCP_FINE_TIMER_MSECS, HANDLER(dhcp_fine_tmr)},
82 #endif /* LWIP_DHCP */
84 {AUTOIP_TMR_INTERVAL, HANDLER(autoip_tmr)},
85 #endif /* LWIP_AUTOIP */
87 {IGMP_TMR_INTERVAL, HANDLER(igmp_tmr)},
88 #endif /* LWIP_IGMP */
89 #endif /* LWIP_IPV4 */
91 {DNS_TMR_INTERVAL, HANDLER(dns_tmr)},
95 {IP6_REASS_TMR_INTERVAL, HANDLER(ip6_reass_tmr)},
96 #endif /* LWIP_IPV6_REASS */
98 {ND6_TMR_INTERVAL, HANDLER(nd6_tmr)},
99 #endif /* LWIP_IPV6_ND */
100 #endif /* LWIP_IPV6 */
103 #if LWIP_TIMERS && !LWIP_TIMERS_CUSTOM
105 /** The one and only timeout list */
106 static struct sys_timeo *next_timeout;
107 static u32_t timeouts_last_time;
110 /** global variable that shows if the tcp timer is currently scheduled or not */
111 static int tcpip_tcp_timer_active;
114 * Timer callback function that calls tcp_tmr() and reschedules itself.
116 * @param arg unused argument
119 tcpip_tcp_timer(void *arg)
121 LWIP_UNUSED_ARG(arg);
123 /* call TCP timer handler */
125 /* timer still needed? */
126 if (tcp_active_pcbs || tcp_tw_pcbs) {
128 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
131 tcpip_tcp_timer_active = 0;
136 * Called from TCP_REG when registering a new PCB:
137 * the reason is to have the TCP timer only running when
138 * there are active (or time-wait) PCBs.
141 tcp_timer_needed(void)
143 /* timer is off but needed again? */
144 if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
145 /* enable and start timer */
146 tcpip_tcp_timer_active = 1;
148 #if LWIP_TCPIP_TIMEOUT
149 // When tcp_connect is called from a different thread, it is
150 // possible that the tcp timers do not get started properly. This
151 // happens when there are no TCP timers active and the SYN packet
152 // is lost (or not responded to). We gate the different code
153 // paths based on LWIP_TCPIP_CORE_LOCKING, which is a prerequisite
154 // to attempt to call the raw api from multiple threads.
155 // Submitted as LwIP patch 8737
157 tcpip_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
159 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
160 #endif //LWIP_TCPIP_CORE_LOCKING
163 #endif /* LWIP_TCP */
166 * Timer callback function that calls mld6_tmr() and reschedules itself.
168 * @param arg unused argument
171 cyclic_timer(void *arg)
173 const struct lwip_cyclic_timer* cyclic = (const struct lwip_cyclic_timer*)arg;
174 #if LWIP_DEBUG_TIMERNAMES
175 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: %s()\n", cyclic->handler_name));
178 sys_timeout(cyclic->interval_ms, cyclic_timer, arg);
181 /** Initialize this module */
182 void sys_timeouts_init(void)
185 /* tcp_tmr() at index 0 is started on demand */
186 for (i = (LWIP_TCP ? 1 : 0); i < LWIP_ARRAYSIZE(lwip_cyclic_timers); i++) {
187 /* we have to cast via size_t to get rid of const warning
188 (this is OK as cyclic_timer() casts back to const* */
189 sys_timeout(lwip_cyclic_timers[i].interval_ms, cyclic_timer, LWIP_CONST_CAST(void*, &lwip_cyclic_timers[i]));
192 /* Initialise timestamp for sys_check_timeouts */
193 timeouts_last_time = sys_now();
197 * Create a one-shot timer (aka timeout). Timeouts are processed in the
199 * - while waiting for a message using sys_timeouts_mbox_fetch()
200 * - by calling sys_check_timeouts() (NO_SYS==1 only)
202 * @param msecs time in milliseconds after that the timer should expire
203 * @param handler callback function to call when msecs have elapsed
204 * @param arg argument to pass to the callback function
206 #if LWIP_DEBUG_TIMERNAMES
208 sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char* handler_name)
209 #else /* LWIP_DEBUG_TIMERNAMES */
211 sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
212 #endif /* LWIP_DEBUG_TIMERNAMES */
214 struct sys_timeo *timeout, *t;
217 LWIP_ASSERT_CORE_LOCKED();
219 timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
220 if (timeout == NULL) {
221 LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout != NULL);
226 if (next_timeout == NULL) {
228 timeouts_last_time = now;
230 diff = now - timeouts_last_time;
233 timeout->next = NULL;
234 timeout->h = handler;
236 timeout->time = msecs + diff;
237 #if LWIP_DEBUG_TIMERNAMES
238 timeout->handler_name = handler_name;
239 LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" handler=%s arg=%p\n",
240 (void *)timeout, msecs, handler_name, (void *)arg));
241 #endif /* LWIP_DEBUG_TIMERNAMES */
243 if (next_timeout == NULL) {
244 next_timeout = timeout;
248 if (next_timeout->time > msecs) {
249 next_timeout->time -= msecs;
250 timeout->next = next_timeout;
251 next_timeout = timeout;
253 for (t = next_timeout; t != NULL; t = t->next) {
254 timeout->time -= t->time;
255 if (t->next == NULL || t->next->time > timeout->time) {
256 if (t->next != NULL) {
257 t->next->time -= timeout->time;
258 } else if (timeout->time > msecs) {
259 /* If this is the case, 'timeouts_last_time' and 'now' differs too much.
260 This can be due to sys_check_timeouts() not being called at the right
261 times, but also when stopping in a breakpoint. Anyway, let's assume
262 this is not wanted, so add the first timer's time instead of 'diff' */
263 timeout->time = msecs + next_timeout->time;
265 timeout->next = t->next;
274 * Go through timeout list (for this task only) and remove the first matching
275 * entry (subsequent entries remain untouched), even though the timeout has not
278 * @param handler callback function that would be called by the timeout
279 * @param arg callback argument that would be passed to handler
282 sys_untimeout(sys_timeout_handler handler, void *arg)
284 struct sys_timeo *prev_t, *t;
286 LWIP_ASSERT_CORE_LOCKED();
288 if (next_timeout == NULL) {
292 for (t = next_timeout, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
293 if ((t->h == handler) && (t->arg == arg)) {
294 /* We have a match */
295 /* Unlink from previous in list */
296 if (prev_t == NULL) {
297 next_timeout = t->next;
299 prev_t->next = t->next;
301 /* If not the last one, add time of this one back to next */
302 if (t->next != NULL) {
303 t->next->time += t->time;
305 memp_free(MEMP_SYS_TIMEOUT, t);
313 * @ingroup lwip_nosys
314 * Handle timeouts for NO_SYS==1 (i.e. without using
315 * tcpip_thread/sys_timeouts_mbox_fetch(). Uses sys_now() to call timeout
316 * handler functions when timeouts expire.
318 * Must be called periodically from your main loop.
320 #if !NO_SYS && !defined __DOXYGEN__
324 sys_check_timeouts(void)
326 LWIP_ASSERT_CORE_LOCKED();
329 struct sys_timeo *tmptimeout;
331 sys_timeout_handler handler;
337 /* this cares for wraparounds */
338 diff = now - timeouts_last_time;
340 PBUF_CHECK_FREE_OOSEQ();
342 tmptimeout = next_timeout;
343 if (tmptimeout && (tmptimeout->time <= diff)) {
344 /* timeout has expired */
346 timeouts_last_time += tmptimeout->time;
347 diff -= tmptimeout->time;
348 next_timeout = tmptimeout->next;
349 handler = tmptimeout->h;
350 arg = tmptimeout->arg;
351 #if LWIP_DEBUG_TIMERNAMES
352 if (handler != NULL) {
353 LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%s arg=%p\n",
354 tmptimeout->handler_name, arg));
356 #endif /* LWIP_DEBUG_TIMERNAMES */
357 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
358 if (handler != NULL) {
360 /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the
361 timeout handler function. */
369 LWIP_TCPIP_THREAD_ALIVE();
371 /* repeat until all expired timers have been called */
376 /** Set back the timestamp of the last call to sys_check_timeouts()
377 * This is necessary if sys_check_timeouts() hasn't been called for a long
378 * time (e.g. while saving energy) to prevent all timer functions of that
379 * period being called.
382 sys_restart_timeouts(void)
384 LWIP_ASSERT_CORE_LOCKED();
386 timeouts_last_time = sys_now();
389 /** Return the time left before the next timeout is due. If no timeouts are
390 * enqueued, returns 0xffffffff
396 sys_timeouts_sleeptime(void)
400 LWIP_ASSERT_CORE_LOCKED();
402 if (next_timeout == NULL) {
405 diff = sys_now() - timeouts_last_time;
406 if (diff > next_timeout->time) {
409 return next_timeout->time - diff;
416 * Wait (forever) for a message to arrive in an mbox.
417 * While waiting, timeouts are processed.
419 * @param mbox the mbox to fetch the message from
420 * @param msg the place to store the message
423 sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
429 sys_arch_mbox_fetch(mbox, msg, 0);
433 sleeptime = sys_timeouts_sleeptime();
434 if (sleeptime == 0 || sys_arch_mbox_fetch(mbox, msg, sleeptime) == SYS_ARCH_TIMEOUT) {
435 /* If a SYS_ARCH_TIMEOUT value is returned, a timeout occurred
436 before a message could be fetched. */
437 sys_check_timeouts();
438 /* We try again to fetch a message from the mbox. */
445 #else /* LWIP_TIMERS && !LWIP_TIMERS_CUSTOM */
446 /* Satisfy the TCP code which calls this function */
448 tcp_timer_needed(void)
451 #endif /* LWIP_TIMERS && !LWIP_TIMERS_CUSTOM */