The Pedigree Project 0.1
timeouts.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
28/*
29 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
30 * All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without modification,
33 * are permitted provided that the following conditions are met:
34 *
35 * 1. Redistributions of source code must retain the above copyright notice,
36 * this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright notice,
38 * this list of conditions and the following disclaimer in the documentation
39 * and/or other materials provided with the distribution.
40 * 3. The name of the author may not be used to endorse or promote products
41 * derived from this software without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
44 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
45 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
46 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
47 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
48 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
49 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
50 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
51 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
52 * OF SUCH DAMAGE.
53 *
54 * This file is part of the lwIP TCP/IP stack.
55 *
56 * Author: Adam Dunkels <adam@sics.se>
57 * Simon Goldschmidt
58 *
59 */
60
61#include "lwip/opt.h"
62
63#include "lwip/timeouts.h"
64#include "lwip/priv/tcp_priv.h"
65
66#include "lwip/def.h"
67#include "lwip/memp.h"
69
70#include "lwip/ip4_frag.h"
71#include "lwip/etharp.h"
72#include "lwip/dhcp.h"
73#include "lwip/autoip.h"
74#include "lwip/igmp.h"
75#include "lwip/dns.h"
76#include "lwip/nd6.h"
77#include "lwip/ip6_frag.h"
78#include "lwip/mld6.h"
79#include "lwip/sys.h"
80#include "lwip/pbuf.h"
81
82#if LWIP_DEBUG_TIMERNAMES
83#define HANDLER(x) x, #x
84#else /* LWIP_DEBUG_TIMERNAMES */
85#define HANDLER(x) x
86#endif /* LWIP_DEBUG_TIMERNAMES */
87
91#if LWIP_TCP
92 /* The TCP timer is a special case: it does not have to run always and
93 is triggered to start from TCP using tcp_timer_needed() */
94 {TCP_TMR_INTERVAL, HANDLER(tcp_tmr)},
95#endif /* LWIP_TCP */
96#if LWIP_IPV4
97#if IP_REASSEMBLY
98 {IP_TMR_INTERVAL, HANDLER(ip_reass_tmr)},
99#endif /* IP_REASSEMBLY */
100#if LWIP_ARP
101 {ARP_TMR_INTERVAL, HANDLER(etharp_tmr)},
102#endif /* LWIP_ARP */
103#if LWIP_DHCP
104 {DHCP_COARSE_TIMER_MSECS, HANDLER(dhcp_coarse_tmr)},
105 {DHCP_FINE_TIMER_MSECS, HANDLER(dhcp_fine_tmr)},
106#endif /* LWIP_DHCP */
107#if LWIP_AUTOIP
108 {AUTOIP_TMR_INTERVAL, HANDLER(autoip_tmr)},
109#endif /* LWIP_AUTOIP */
110#if LWIP_IGMP
111 {IGMP_TMR_INTERVAL, HANDLER(igmp_tmr)},
112#endif /* LWIP_IGMP */
113#endif /* LWIP_IPV4 */
114#if LWIP_DNS
115 {DNS_TMR_INTERVAL, HANDLER(dns_tmr)},
116#endif /* LWIP_DNS */
117#if LWIP_IPV6
118 {ND6_TMR_INTERVAL, HANDLER(nd6_tmr)},
119#if LWIP_IPV6_REASS
120 {IP6_REASS_TMR_INTERVAL, HANDLER(ip6_reass_tmr)},
121#endif /* LWIP_IPV6_REASS */
122#if LWIP_IPV6_MLD
123 {MLD6_TMR_INTERVAL, HANDLER(mld6_tmr)},
124#endif /* LWIP_IPV6_MLD */
125#endif /* LWIP_IPV6 */
126};
127
128#if LWIP_TIMERS && !LWIP_TIMERS_CUSTOM
129
131static struct sys_timeo *next_timeout;
132static u32_t timeouts_last_time;
133
134#if LWIP_TCP
136static int tcpip_tcp_timer_active;
137
143static void
144tcpip_tcp_timer(void *arg)
145{
146 LWIP_UNUSED_ARG(arg);
147
148 /* call TCP timer handler */
149 tcp_tmr();
150 /* timer still needed? */
151 if (tcp_active_pcbs || tcp_tw_pcbs) {
152 /* restart timer */
153 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
154 } else {
155 /* disable timer */
156 tcpip_tcp_timer_active = 0;
157 }
158}
159
165void
166tcp_timer_needed(void)
167{
168 /* timer is off but needed again? */
169 if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
170 /* enable and start timer */
171 tcpip_tcp_timer_active = 1;
172 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
173 }
174}
175#endif /* LWIP_TCP */
176
182static void
183cyclic_timer(void *arg)
184{
185 const struct lwip_cyclic_timer* cyclic = (const struct lwip_cyclic_timer*)arg;
186#if LWIP_DEBUG_TIMERNAMES
187 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: %s()\n", cyclic->handler_name));
188#endif
189 cyclic->handler();
190 sys_timeout(cyclic->interval_ms, cyclic_timer, arg);
191}
192
194void sys_timeouts_init(void)
195{
196 size_t i;
197 /* tcp_tmr() at index 0 is started on demand */
198 for (i = 1; i < LWIP_ARRAYSIZE(lwip_cyclic_timers); i++) {
199 /* we have to cast via size_t to get rid of const warning
200 (this is OK as cyclic_timer() casts back to const* */
201 sys_timeout(lwip_cyclic_timers[i].interval_ms, cyclic_timer, LWIP_CONST_CAST(void*, &lwip_cyclic_timers[i]));
202 }
203
204 /* Initialise timestamp for sys_check_timeouts */
205 timeouts_last_time = sys_now();
206}
207
218#if LWIP_DEBUG_TIMERNAMES
219void
220sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char* handler_name)
221#else /* LWIP_DEBUG_TIMERNAMES */
222void
223sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
224#endif /* LWIP_DEBUG_TIMERNAMES */
225{
226 struct sys_timeo *timeout, *t;
227 u32_t now, diff;
228
229 timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
230 if (timeout == NULL) {
231 LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout != NULL);
232 return;
233 }
234
235 now = sys_now();
236 if (next_timeout == NULL) {
237 diff = 0;
238 timeouts_last_time = now;
239 } else {
240 diff = now - timeouts_last_time;
241 }
242
243 timeout->next = NULL;
244 timeout->h = handler;
245 timeout->arg = arg;
246 timeout->time = msecs + diff;
247#if LWIP_DEBUG_TIMERNAMES
248 timeout->handler_name = handler_name;
249 LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" handler=%s arg=%p\n",
250 (void *)timeout, msecs, handler_name, (void *)arg));
251#endif /* LWIP_DEBUG_TIMERNAMES */
252
253 if (next_timeout == NULL) {
254 next_timeout = timeout;
255 return;
256 }
257
258 if (next_timeout->time > msecs) {
259 next_timeout->time -= msecs;
260 timeout->next = next_timeout;
261 next_timeout = timeout;
262 } else {
263 for (t = next_timeout; t != NULL; t = t->next) {
264 timeout->time -= t->time;
265 if (t->next == NULL || t->next->time > timeout->time) {
266 if (t->next != NULL) {
267 t->next->time -= timeout->time;
268 } else if (timeout->time > msecs) {
269 /* If this is the case, 'timeouts_last_time' and 'now' differs too much.
270 This can be due to sys_check_timeouts() not being called at the right
271 times, but also when stopping in a breakpoint. Anyway, let's assume
272 this is not wanted, so add the first timer's time instead of 'diff' */
273 timeout->time = msecs + next_timeout->time;
274 }
275 timeout->next = t->next;
276 t->next = timeout;
277 break;
278 }
279 }
280 }
281}
282
291void
292sys_untimeout(sys_timeout_handler handler, void *arg)
293{
294 struct sys_timeo *prev_t, *t;
295
296 if (next_timeout == NULL) {
297 return;
298 }
299
300 for (t = next_timeout, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
301 if ((t->h == handler) && (t->arg == arg)) {
302 /* We have a match */
303 /* Unlink from previous in list */
304 if (prev_t == NULL) {
305 next_timeout = t->next;
306 } else {
307 prev_t->next = t->next;
308 }
309 /* If not the last one, add time of this one back to next */
310 if (t->next != NULL) {
311 t->next->time += t->time;
312 }
313 memp_free(MEMP_SYS_TIMEOUT, t);
314 return;
315 }
316 }
317 return;
318}
319
328#if !NO_SYS && !defined __DOXYGEN__
329static
330#endif /* !NO_SYS */
331void
332sys_check_timeouts(void)
333{
334 if (next_timeout) {
335 struct sys_timeo *tmptimeout;
336 u32_t diff;
337 sys_timeout_handler handler;
338 void *arg;
339 u8_t had_one;
340 u32_t now;
341
342 now = sys_now();
343 /* this cares for wraparounds */
344 diff = now - timeouts_last_time;
345 do {
346 PBUF_CHECK_FREE_OOSEQ();
347 had_one = 0;
348 tmptimeout = next_timeout;
349 if (tmptimeout && (tmptimeout->time <= diff)) {
350 /* timeout has expired */
351 had_one = 1;
352 timeouts_last_time += tmptimeout->time;
353 diff -= tmptimeout->time;
354 next_timeout = tmptimeout->next;
355 handler = tmptimeout->h;
356 arg = tmptimeout->arg;
357#if LWIP_DEBUG_TIMERNAMES
358 if (handler != NULL) {
359 LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%s arg=%p\n",
360 tmptimeout->handler_name, arg));
361 }
362#endif /* LWIP_DEBUG_TIMERNAMES */
363 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
364 if (handler != NULL) {
365#if !NO_SYS
366 /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the
367 timeout handler function. */
368 LOCK_TCPIP_CORE();
369#endif /* !NO_SYS */
370 handler(arg);
371#if !NO_SYS
372 UNLOCK_TCPIP_CORE();
373#endif /* !NO_SYS */
374 }
376 }
377 /* repeat until all expired timers have been called */
378 } while (had_one);
379 }
380}
381
387void
388sys_restart_timeouts(void)
389{
390 timeouts_last_time = sys_now();
391}
392
396#if !NO_SYS
397static
398#endif /* !NO_SYS */
399u32_t
400sys_timeouts_sleeptime(void)
401{
402 u32_t diff;
403 if (next_timeout == NULL) {
404 return 0xffffffff;
405 }
406 diff = sys_now() - timeouts_last_time;
407 if (diff > next_timeout->time) {
408 return 0;
409 } else {
410 return next_timeout->time - diff;
411 }
412}
413
414#if !NO_SYS
415
423void
424sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
425{
426 u32_t sleeptime;
427
428again:
429 if (!next_timeout) {
430 sys_arch_mbox_fetch(mbox, msg, 0);
431 return;
432 }
433
434 sleeptime = sys_timeouts_sleeptime();
435 if (sleeptime == 0 || sys_arch_mbox_fetch(mbox, msg, sleeptime) == SYS_ARCH_TIMEOUT) {
436 /* If a SYS_ARCH_TIMEOUT value is returned, a timeout occurred
437 before a message could be fetched. */
438 sys_check_timeouts();
439 /* We try again to fetch a message from the mbox. */
440 goto again;
441 }
442}
443
444#endif /* NO_SYS */
445
446#else /* LWIP_TIMERS && !LWIP_TIMERS_CUSTOM */
447/* Satisfy the TCP code which calls this function */
448void
449tcp_timer_needed(void)
450{
451}
452#endif /* LWIP_TIMERS && !LWIP_TIMERS_CUSTOM */
#define LWIP_DEBUGF(debug, message)
Definition debug.h:183
#define LWIP_UNUSED_ARG(x)
Definition arch.h:327
#define LWIP_CONST_CAST(target_type, val)
Definition arch.h:199
#define TIMERS_DEBUG
Definition opt.h:2755
#define LWIP_TCPIP_THREAD_ALIVE()
Definition opt.h:1593
u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
Definition sys_arch.cc:290
u32_t sys_now(void)
Definition sys_arch.cc:78
void * memp_malloc(memp_t type)
Definition memp.c:404
void memp_free(memp_t type, void *mem)
Definition memp.c:488
#define SYS_ARCH_TIMEOUT
Definition sys.h:106
const struct lwip_cyclic_timer lwip_cyclic_timers[]
Definition timeouts.c:90