UbixOS  2.0
timeouts.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 "net/opt.h"
43 
44 #include "net/timeouts.h"
45 #include "net/priv/tcp_priv.h"
46 
47 #include "net/def.h"
48 #include "net/memp.h"
49 #include "net/priv/tcpip_priv.h"
50 
51 #include "net/ip4_frag.h"
52 #include "net/etharp.h"
53 #include "net/dhcp.h"
54 #include "net/autoip.h"
55 #include "net/igmp.h"
56 #include "net/dns.h"
57 #include "net/nd6.h"
58 #include "net/ip6_frag.h"
59 #include "net/mld6.h"
60 #include "net/sys.h"
61 #include "net/pbuf.h"
62 
63 #if LWIP_DEBUG_TIMERNAMES
64 #define HANDLER(x) x, #x
65 #else /* LWIP_DEBUG_TIMERNAMES */
66 #define HANDLER(x) x
67 #endif /* LWIP_DEBUG_TIMERNAMES */
68 
72 #if LWIP_TCP
73  /* The TCP timer is a special case: it does not have to run always and
74  is triggered to start from TCP using tcp_timer_needed() */
75  {TCP_TMR_INTERVAL, HANDLER(tcp_tmr)},
76 #endif /* LWIP_TCP */
77 #if LWIP_IPV4
78 #if IP_REASSEMBLY
79  {IP_TMR_INTERVAL, HANDLER(ip_reass_tmr)},
80 #endif /* IP_REASSEMBLY */
81 #if LWIP_ARP
82  {ARP_TMR_INTERVAL, HANDLER(etharp_tmr)},
83 #endif /* LWIP_ARP */
84 #if LWIP_DHCP
85  {DHCP_COARSE_TIMER_MSECS, HANDLER(dhcp_coarse_tmr)},
86  {DHCP_FINE_TIMER_MSECS, HANDLER(dhcp_fine_tmr)},
87 #endif /* LWIP_DHCP */
88 #if LWIP_AUTOIP
89  {AUTOIP_TMR_INTERVAL, HANDLER(autoip_tmr)},
90 #endif /* LWIP_AUTOIP */
91 #if LWIP_IGMP
92  {IGMP_TMR_INTERVAL, HANDLER(igmp_tmr)},
93 #endif /* LWIP_IGMP */
94 #endif /* LWIP_IPV4 */
95 #if LWIP_DNS
96  {DNS_TMR_INTERVAL, HANDLER(dns_tmr)},
97 #endif /* LWIP_DNS */
98 #if LWIP_IPV6
99  {ND6_TMR_INTERVAL, HANDLER(nd6_tmr)},
100 #if LWIP_IPV6_REASS
101  {IP6_REASS_TMR_INTERVAL, HANDLER(ip6_reass_tmr)},
102 #endif /* LWIP_IPV6_REASS */
103 #if LWIP_IPV6_MLD
104  {MLD6_TMR_INTERVAL, HANDLER(mld6_tmr)},
105 #endif /* LWIP_IPV6_MLD */
106 #endif /* LWIP_IPV6 */
107 };
108 
109 #if LWIP_TIMERS && !LWIP_TIMERS_CUSTOM
110 
112 static struct sys_timeo *next_timeout;
113 static u32_t timeouts_last_time;
114 
115 #if LWIP_TCP
116 
117 static int tcpip_tcp_timer_active;
118 
124 static void
125 tcpip_tcp_timer(void *arg)
126 {
127  LWIP_UNUSED_ARG(arg);
128 
129  /* call TCP timer handler */
130  tcp_tmr();
131  /* timer still needed? */
132  if (tcp_active_pcbs || tcp_tw_pcbs) {
133  /* restart timer */
134  sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
135  } else {
136  /* disable timer */
137  tcpip_tcp_timer_active = 0;
138  }
139 }
140 
146 void
147 tcp_timer_needed(void)
148 {
149  /* timer is off but needed again? */
150  if (!tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
151  /* enable and start timer */
152  tcpip_tcp_timer_active = 1;
153  sys_timeout(TCP_TMR_INTERVAL, tcpip_tcp_timer, NULL);
154  }
155 }
156 #endif /* LWIP_TCP */
157 
163 static void
164 cyclic_timer(void *arg)
165 {
166  const struct lwip_cyclic_timer* cyclic = (const struct lwip_cyclic_timer*)arg;
167 #if LWIP_DEBUG_TIMERNAMES
168  LWIP_DEBUGF(TIMERS_DEBUG, ("tcpip: %s()\n", cyclic->handler_name));
169 #endif
170  cyclic->handler();
171  sys_timeout(cyclic->interval_ms, cyclic_timer, arg);
172 }
173 
175 void sys_timeouts_init(void)
176 {
177  size_t i;
178  /* tcp_tmr() at index 0 is started on demand */
179  for (i = (LWIP_TCP ? 1 : 0); i < LWIP_ARRAYSIZE(lwip_cyclic_timers); i++) {
180  /* we have to cast via size_t to get rid of const warning
181  (this is OK as cyclic_timer() casts back to const* */
182  sys_timeout(lwip_cyclic_timers[i].interval_ms, cyclic_timer, LWIP_CONST_CAST(void*, &lwip_cyclic_timers[i]));
183  }
184 
185  /* Initialise timestamp for sys_check_timeouts */
186  timeouts_last_time = sys_now();
187 }
188 
199 #if LWIP_DEBUG_TIMERNAMES
200 void
201 sys_timeout_debug(u32_t msecs, sys_timeout_handler handler, void *arg, const char* handler_name)
202 #else /* LWIP_DEBUG_TIMERNAMES */
203 void
204 sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg)
205 #endif /* LWIP_DEBUG_TIMERNAMES */
206 {
207  struct sys_timeo *timeout, *t;
208  u32_t now, diff;
209 
210  timeout = (struct sys_timeo *)memp_malloc(MEMP_SYS_TIMEOUT);
211  if (timeout == NULL) {
212  LWIP_ASSERT("sys_timeout: timeout != NULL, pool MEMP_SYS_TIMEOUT is empty", timeout != NULL);
213  return;
214  }
215 
216  now = sys_now();
217  if (next_timeout == NULL) {
218  diff = 0;
219  timeouts_last_time = now;
220  } else {
221  diff = now - timeouts_last_time;
222  }
223 
224  timeout->next = NULL;
225  timeout->h = handler;
226  timeout->arg = arg;
227  timeout->time = msecs + diff;
228 #if LWIP_DEBUG_TIMERNAMES
229  timeout->handler_name = handler_name;
230  LWIP_DEBUGF(TIMERS_DEBUG, ("sys_timeout: %p msecs=%"U32_F" handler=%s arg=%p\n",
231  (void *)timeout, msecs, handler_name, (void *)arg));
232 #endif /* LWIP_DEBUG_TIMERNAMES */
233 
234  if (next_timeout == NULL) {
235  next_timeout = timeout;
236  return;
237  }
238 
239  if (next_timeout->time > msecs) {
240  next_timeout->time -= msecs;
241  timeout->next = next_timeout;
242  next_timeout = timeout;
243  } else {
244  for (t = next_timeout; t != NULL; t = t->next) {
245  timeout->time -= t->time;
246  if (t->next == NULL || t->next->time > timeout->time) {
247  if (t->next != NULL) {
248  t->next->time -= timeout->time;
249  } else if (timeout->time > msecs) {
250  /* If this is the case, 'timeouts_last_time' and 'now' differs too much.
251  This can be due to sys_check_timeouts() not being called at the right
252  times, but also when stopping in a breakpoint. Anyway, let's assume
253  this is not wanted, so add the first timer's time instead of 'diff' */
254  timeout->time = msecs + next_timeout->time;
255  }
256  timeout->next = t->next;
257  t->next = timeout;
258  break;
259  }
260  }
261  }
262 }
263 
272 void
273 sys_untimeout(sys_timeout_handler handler, void *arg)
274 {
275  struct sys_timeo *prev_t, *t;
276 
277  if (next_timeout == NULL) {
278  return;
279  }
280 
281  for (t = next_timeout, prev_t = NULL; t != NULL; prev_t = t, t = t->next) {
282  if ((t->h == handler) && (t->arg == arg)) {
283  /* We have a match */
284  /* Unlink from previous in list */
285  if (prev_t == NULL) {
286  next_timeout = t->next;
287  } else {
288  prev_t->next = t->next;
289  }
290  /* If not the last one, add time of this one back to next */
291  if (t->next != NULL) {
292  t->next->time += t->time;
293  }
294  memp_free(MEMP_SYS_TIMEOUT, t);
295  return;
296  }
297  }
298  return;
299 }
300 
309 #if !NO_SYS && !defined __DOXYGEN__
310 static
311 #endif /* !NO_SYS */
312 void
313 sys_check_timeouts(void)
314 {
315  if (next_timeout) {
316  struct sys_timeo *tmptimeout;
317  u32_t diff;
318  sys_timeout_handler handler;
319  void *arg;
320  u8_t had_one;
321  u32_t now;
322 
323  now = sys_now();
324  /* this cares for wraparounds */
325  diff = now - timeouts_last_time;
326  do {
328  had_one = 0;
329  tmptimeout = next_timeout;
330  if (tmptimeout && (tmptimeout->time <= diff)) {
331  /* timeout has expired */
332  had_one = 1;
333  timeouts_last_time += tmptimeout->time;
334  diff -= tmptimeout->time;
335  next_timeout = tmptimeout->next;
336  handler = tmptimeout->h;
337  arg = tmptimeout->arg;
338 #if LWIP_DEBUG_TIMERNAMES
339  if (handler != NULL) {
340  LWIP_DEBUGF(TIMERS_DEBUG, ("sct calling h=%s arg=%p\n",
341  tmptimeout->handler_name, arg));
342  }
343 #endif /* LWIP_DEBUG_TIMERNAMES */
344  memp_free(MEMP_SYS_TIMEOUT, tmptimeout);
345  if (handler != NULL) {
346 #if !NO_SYS
347  /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the
348  timeout handler function. */
349  LOCK_TCPIP_CORE();
350 #endif /* !NO_SYS */
351  handler(arg);
352 #if !NO_SYS
354 #endif /* !NO_SYS */
355  }
357  }
358  /* repeat until all expired timers have been called */
359  } while (had_one);
360  }
361 }
362 
368 void
369 sys_restart_timeouts(void)
370 {
371  timeouts_last_time = sys_now();
372 }
373 
377 #if !NO_SYS
378 static
379 #endif /* !NO_SYS */
380 u32_t
381 sys_timeouts_sleeptime(void)
382 {
383  u32_t diff;
384  if (next_timeout == NULL) {
385  return 0xffffffff;
386  }
387  diff = sys_now() - timeouts_last_time;
388  if (diff > next_timeout->time) {
389  return 0;
390  } else {
391  return next_timeout->time - diff;
392  }
393 }
394 
395 #if !NO_SYS
396 
404 void
405 sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg)
406 {
407  u32_t sleeptime;
408 
409 again:
410  if (!next_timeout) {
411  sys_arch_mbox_fetch(mbox, msg, 0);
412  return;
413  }
414 
415  sleeptime = sys_timeouts_sleeptime();
416  if (sleeptime == 0 || sys_arch_mbox_fetch(mbox, msg, sleeptime) == SYS_ARCH_TIMEOUT) {
417  /* If a SYS_ARCH_TIMEOUT value is returned, a timeout occurred
418  before a message could be fetched. */
419  sys_check_timeouts();
420  /* We try again to fetch a message from the mbox. */
421  goto again;
422  }
423 }
424 
425 #endif /* NO_SYS */
426 
427 #else /* LWIP_TIMERS && !LWIP_TIMERS_CUSTOM */
428 /* Satisfy the TCP code which calls this function */
429 void
431 {
432 }
433 #endif /* LWIP_TIMERS && !LWIP_TIMERS_CUSTOM */
tcpip_priv.h
sys.h
opt.h
def.h
nd6.h
LWIP_ASSERT
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:116
pbuf.h
lwip_cyclic_timer
Definition: timeouts.h:65
ip6_frag.h
LWIP_ARRAYSIZE
#define LWIP_ARRAYSIZE(x)
Definition: def.h:58
igmp.h
ip4_frag.h
autoip.h
tcp_timer_needed
void tcp_timer_needed(void)
Definition: timeouts.c:430
UNLOCK_TCPIP_CORE
#define UNLOCK_TCPIP_CORE()
Definition: tcpip.h:61
u32_t
uint32_t u32_t
Definition: arch.h:126
LWIP_TCPIP_THREAD_ALIVE
#define LWIP_TCPIP_THREAD_ALIVE()
Definition: lwipopts.h:249
memp_malloc
void * memp_malloc(memp_t type)
Definition: memp.c:385
lwip_cyclic_timer::handler
lwip_cyclic_timer_handler handler
Definition: timeouts.h:67
memp_free
void memp_free(memp_t type, void *mem)
Definition: memp.c:469
sys_now
u32_t sys_now(void)
Definition: sys_arch.c:496
mld6.h
u8_t
uint8_t u8_t
Definition: arch.h:122
tcp_priv.h
LWIP_UNUSED_ARG
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:315
sys_arch_mbox_fetch
uint32_t sys_arch_mbox_fetch(struct sys_mbox **mb, void **msg, uint32_t timeout)
Definition: sys_arch.c:286
dhcp.h
etharp.h
PBUF_CHECK_FREE_OOSEQ
#define PBUF_CHECK_FREE_OOSEQ()
Definition: pbuf.h:217
LWIP_TCP
#define LWIP_TCP
Definition: lwipopts.h:170
HANDLER
#define HANDLER(x)
Definition: timeouts.c:66
SYS_ARCH_TIMEOUT
#define SYS_ARCH_TIMEOUT
Definition: sys.h:47
U32_F
#define U32_F
Definition: arch.h:157
memp.h
timeouts.h
lwip_cyclic_timers
const struct lwip_cyclic_timer lwip_cyclic_timers[]
Definition: timeouts.c:71
lwip_cyclic_timer::interval_ms
u32_t interval_ms
Definition: timeouts.h:66
dns.h
LWIP_DEBUGF
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:164
LOCK_TCPIP_CORE
#define LOCK_TCPIP_CORE()
Definition: tcpip.h:60
LWIP_CONST_CAST
#define LWIP_CONST_CAST(target_type, val)
Definition: arch.h:187
TIMERS_DEBUG
#define TIMERS_DEBUG
Definition: lwipopts.h:461
sys_mbox
Definition: sys_arch.h:22
NULL
#define NULL
Definition: fat_string.h:17