ReactOS 0.4.15-dev-7953-g1f49173
timers.c
Go to the documentation of this file.
1
9/*
10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
15 *
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.
23 *
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.
34 *
35 * This file is part of the lwIP TCP/IP stack.
36 *
37 * Author: Adam Dunkels <adam@sics.se>
38 * Simon Goldschmidt
39 *
40 */
41
42#include "lwip/opt.h"
43
44#include "lwip/timers.h"
45#include "lwip/tcp_impl.h"
46
47#if LWIP_TIMERS
48
49#include "lwip/def.h"
50#include "lwip/memp.h"
51#include "lwip/tcpip.h"
52
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"
59#include "lwip/sys.h"
60#include "lwip/pbuf.h"
61
62
64static struct sys_timeo *next_timeout;
65#if NO_SYS
66static u32_t timeouts_last_time;
67#endif /* NO_SYS */
68
69#if LWIP_TCP
71static int tcpip_tcp_timer_active;
72
78static void
79tcpip_tcp_timer(void *arg)
80{
82
83 /* call TCP timer handler */
84 tcp_tmr();
85 /* timer still needed? */
86 if (tcp_active_pcbs || tcp_tw_pcbs) {
87 /* restart timer */
88 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
89 } else {
90 /* disable timer */
91 tcpip_tcp_timer_active = 0;
92 }
93}
94
100void
102{
103 /* timer is off but needed again? */
104 if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
105 /* enable and start timer */
106 tcpip_tcp_timer_active = 1;
107 sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
108 }
109}
110#endif /* LWIP_TCP */
111
112#if IP_REASSEMBLY
118static void
119ip_reass_timer(void *arg)
120{
122 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: ip_reass_tmr()\n"));
123 ip_reass_tmr();
124 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
125}
126#endif /* IP_REASSEMBLY */
127
128#if LWIP_ARP
134static void
135arp_timer(void *arg)
136{
138 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: etharp_tmr()\n"));
139 etharp_tmr();
140 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
141}
142#endif /* LWIP_ARP */
143
144#if LWIP_DHCP
150static void
151dhcp_timer_coarse(void *arg)
152{
154 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_coarse_tmr()\n"));
155 dhcp_coarse_tmr();
156 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
157}
158
164static void
165dhcp_timer_fine(void *arg)
166{
168 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dhcp_fine_tmr()\n"));
169 dhcp_fine_tmr();
170 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
171}
172#endif /* LWIP_DHCP */
173
174#if LWIP_AUTOIP
180static void
181autoip_timer(void *arg)
182{
184 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: autoip_tmr()\n"));
185 autoip_tmr();
186 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
187}
188#endif /* LWIP_AUTOIP */
189
190#if LWIP_IGMP
196static void
197igmp_timer(void *arg)
198{
200 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: igmp_tmr()\n"));
201 igmp_tmr();
202 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
203}
204#endif /* LWIP_IGMP */
205
206#if LWIP_DNS
212static void
213dns_timer(void *arg)
214{
216 LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: dns_tmr()\n"));
217 dns_tmr();
218 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
219}
220#endif /* LWIP_DNS */
221
223void sys_timeouts_init(void)
224{
225#if IP_REASSEMBLY
226 sys_timeout(IP_TMR_INTERVAL, ip_reass_timer, NULL);
227#endif /* IP_REASSEMBLY */
228#if LWIP_ARP
229 sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL);
230#endif /* LWIP_ARP */
231#if LWIP_DHCP
232 sys_timeout(DHCP_COARSE_TIMER_MSECS, dhcp_timer_coarse, NULL);
233 sys_timeout(DHCP_FINE_TIMER_MSECS, dhcp_timer_fine, NULL);
234#endif /* LWIP_DHCP */
235#if LWIP_AUTOIP
236 sys_timeout(AUTOIP_TMR_INTERVAL, autoip_timer, NULL);
237#endif /* LWIP_AUTOIP */
238#if LWIP_IGMP
239 sys_timeout(IGMP_TMR_INTERVAL, igmp_timer, NULL);
240#endif /* LWIP_IGMP */
241#if LWIP_DNS
242 sys_timeout(DNS_TMR_INTERVAL, dns_timer, NULL);
243#endif /* LWIP_DNS */
244
245#if NO_SYS
246 /* Initialise timestamp for sys_check_timeouts */
247 timeouts_last_time = sys_now();
248#endif
249}
250
261#if LWIP_DEBUG_TIMERNAMES
262void
263sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char* handler_name)
264#else /* LWIP_DEBUG_TIMERNAMES */
265void
267#endif /* LWIP_DEBUG_TIMERNAMES */
268{
269 struct sys_timeo *timeout, *t;
270
271 timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
272 if (timeout == NULL) {
273 LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout != NULL);
274 return;
275 }
276 timeout->next = NULL;
277 timeout->h = handler;
278 timeout->arg = arg;
279 timeout->time = msecs;
280#if LWIP_DEBUG_TIMERNAMES
281 timeout->handler_name = handler_name;
282 LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" handler=%s arg=%p\n",
283 (void *)timeout, msecs, handler_name, (void *)arg));
284#endif /* LWIP_DEBUG_TIMERNAMES */
285
286 if (next_timeout == NULL) {
287 next_timeout = timeout;
288 return;
289 }
290
291 if (next_timeout->time > msecs) {
292 next_timeout->time -= msecs;
293 timeout->next = next_timeout;
294 next_timeout = timeout;
295 } else {
296 for(t = next_timeout; t != NULL; t = t->next) {
297 timeout->time -= t->time;
298 if (t->next == NULL || t->next->time > timeout->time) {
299 if (t->next != NULL) {
300 t->next->time -= timeout->time;
301 }
302 timeout->next = t->next;
303 t->next = timeout;
304 break;
305 }
306 }
307 }
308}
309
320void
322{
323 struct sys_timeo *prev_t, *t;
324
325 if (next_timeout == NULL) {
326 return;
327 }
328
329 for (t = next_timeout, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
330 if ((t->h == handler) && (t->arg == arg)) {
331 /* We have a match */
332 /* Unlink from previous in list */
333 if (prev_t == NULL) {
334 next_timeout = t->next;
335 } else {
336 prev_t->next = t->next;
337 }
338 /* If not the last one, add time of this one back to next */
339 if (t->next != NULL) {
340 t->next->time += t->time;
341 }
342 memp_free(MEMP_SYS_TIMEOUT, t);
343 return;
344 }
345 }
346 return;
347}
348
349#if NO_SYS
350
357void
358sys_check_timeouts(void)
359{
360 if (next_timeout) {
361 struct sys_timeo *tmptimeout;
362 u32_t diff;
364 void *arg;
365 u8_t had_one;
366 u32_t now;
367
368 now = sys_now();
369 /* this cares for wraparounds */
370 diff = now - timeouts_last_time;
371 do
372 {
373#if PBUF_POOL_FREE_OOSEQ
374 PBUF_CHECK_FREE_OOSEQ();
375#endif /* PBUF_POOL_FREE_OOSEQ */
376 had_one = 0;
377 tmptimeout = next_timeout;
378 if (tmptimeout && (tmptimeout->time <= diff)) {
379 /* timeout has expired */
380 had_one = 1;
381 timeouts_last_time = now;
382 diff -= tmptimeout->time;
383 next_timeout = tmptimeout->next;
384 handler = tmptimeout->h;
385 arg = tmptimeout->arg;
386#if LWIP_DEBUG_TIMERNAMES
387 if (handler != NULL) {
388 LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%s arg=%p\n",
389 tmptimeout->handler_name, arg));
390 }
391#endif /* LWIP_DEBUG_TIMERNAMES */
392 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
393 if (handler != NULL) {
394 handler(arg);
395 }
396 }
397 /* repeat until all expired timers have been called */
398 }while(had_one);
399 }
400}
401
407void
408sys_restart_timeouts(void)
409{
410 timeouts_last_time = sys_now();
411}
412
413#else /* NO_SYS */
414
422void
424{
425 u32_t time_needed;
426 struct sys_timeo *tmptimeout;
428 void *arg;
429
430 again:
431 if (!next_timeout) {
432 time_needed = sys_arch_mbox_fetch(mbox, msg, 0);
433 } else {
434 if (next_timeout->time > 0) {
435 time_needed = sys_arch_mbox_fetch(mbox, msg, next_timeout->time);
436 } else {
437 time_needed = SYS_ARCH_TIMEOUT;
438 }
439
440 if (time_needed == SYS_ARCH_TIMEOUT) {
441 /* If time == SYS_ARCH_TIMEOUT, a timeout occured before a message
442 could be fetched. We should now call the timeout handler and
443 deallocate the memory allocated for the timeout. */
444 tmptimeout = next_timeout;
445 next_timeout = tmptimeout->next;
446 handler = tmptimeout->h;
447 arg = tmptimeout->arg;
448#if LWIP_DEBUG_TIMERNAMES
449 if (handler != NULL) {
450 LWIP_DEBUGF(TIMERS_DEBUG, ("stmf calling h=%s arg=%p\n",
451 tmptimeout->handler_name, arg));
452 }
453#endif /* LWIP_DEBUG_TIMERNAMES */
454 memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
455 if (handler != NULL) {
456 /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the
457 timeout handler function. */
459 handler(arg);
461 }
463
464 /* We try again to fetch a message from the mbox. */
465 goto again;
466 } else {
467 /* If time != SYS_ARCH_TIMEOUT, a message was received before the timeout
468 occured. The time variable is set to the number of
469 milliseconds we waited for the message. */
470 if (time_needed < next_timeout->time) {
471 next_timeout->time -= time_needed;
472 } else {
473 next_timeout->time = 0;
474 }
475 }
476 }
477}
478
479#endif /* NO_SYS */
480
481#else /* LWIP_TIMERS */
482/* Satisfy the TCP code which calls this function */
483void
485{
486}
487#endif /* LWIP_TIMERS */
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:73
#define msg(x)
Definition: auth_time.c:54
#define NULL
Definition: types.h:112
UINT(* handler)(MSIPACKAGE *)
Definition: action.c:7482
unsigned long u32_t
Definition: cc.h:25
#define U32_F
Definition: cc.h:39
unsigned char u8_t
Definition: cc.h:23
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:95
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:66
#define LOCK_TCPIP_CORE()
Definition: tcpip.h:67
#define LWIP_TCPIP_THREAD_ALIVE()
Definition: tcpip.h:54
#define UNLOCK_TCPIP_CORE()
Definition: tcpip.h:68
time_t now
Definition: finger.c:65
GLdouble GLdouble t
Definition: gl.h:2047
void * memp_malloc(memp_t type)
Definition: memp.c:390
void memp_free(memp_t type, void *mem)
Definition: memp.c:435
__u16 time
Definition: mkdosfs.c:8
#define TIMERS_DEBUG
Definition: opt.h:2003
struct define * next
Definition: compiler.c:65
sys_timeout_handler h
Definition: timers.h:70
void * arg
Definition: timers.h:71
u32_t time
Definition: timers.h:69
struct sys_timeo * next
Definition: timers.h:68
Definition: dhcpd.h:245
struct timeout * next
Definition: dhcpd.h:246
#define SYS_ARCH_TIMEOUT
Definition: sys.h:78
u32_t sys_now(void)
Definition: sys_arch.c:23
u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout)
Definition: sys_arch.c:177
static sys_mbox_t mbox
Definition: tcpip.c:58
void tcp_timer_needed(void)
Definition: timers.c:484
void(* sys_timeout_handler)(void *arg)
Definition: timers.h:65
void sys_timeouts_init(void)
void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
void sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
void sys_untimeout(sys_timeout_handler handler, void *arg)
void * arg
Definition: msvc.h:10