From c257b56a39d89fa0330394e9978ec7c2e878c6f3 Mon Sep 17 00:00:00 2001 From: goldsimon Date: Thu, 11 Jan 2018 09:39:36 +0100 Subject: [PATCH] move sys_timeouts_mbox_fetch() to tcpip_timeouts_mbox_fetch() This cleans up the code: sys_timeouts_mbox_fetch() was only used from tcpip.c anyway, so let's move it there. Signed-off-by: goldsimon --- src/api/tcpip.c | 50 ++++++++++++++++++++++++++---- src/core/timeouts.c | 61 +++++-------------------------------- src/include/lwip/timeouts.h | 10 +++--- 3 files changed, 56 insertions(+), 65 deletions(-) diff --git a/src/api/tcpip.c b/src/api/tcpip.c index 1176c11c..dd2891d7 100644 --- a/src/api/tcpip.c +++ b/src/api/tcpip.c @@ -65,15 +65,53 @@ static sys_mbox_t mbox; sys_mutex_t lock_tcpip_core; #endif /* LWIP_TCPIP_CORE_LOCKING */ -#if LWIP_TIMERS -/* wait for a message, timeouts are processed while waiting */ -#define TCPIP_MBOX_FETCH(mbox, msg) sys_timeouts_mbox_fetch(mbox, msg) -#else /* LWIP_TIMERS */ +static void tcpip_thread_handle_msg(struct tcpip_msg *msg); + +#if !LWIP_TIMERS /* wait for a message with timers disabled (e.g. pass a timer-check trigger into tcpip_thread) */ #define TCPIP_MBOX_FETCH(mbox, msg) sys_mbox_fetch(mbox, msg) -#endif /* LWIP_TIMERS */ +#else /* !LWIP_TIMERS */ +/* wait for a message, timeouts are processed while waiting */ +#define TCPIP_MBOX_FETCH(mbox, msg) tcpip_timeouts_mbox_fetch(mbox, msg) +/** + * Wait (forever) for a message to arrive in an mbox. + * While waiting, timeouts are processed. + * + * @param mbox the mbox to fetch the message from + * @param msg the place to store the message + */ +static void +tcpip_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg) +{ + u32_t sleeptime, res; -static void tcpip_thread_handle_msg(struct tcpip_msg *msg); +again: + LWIP_ASSERT_CORE_LOCKED(); + + sleeptime = sys_timeouts_sleeptime(); + if (sleeptime == SYS_TIMEOUTS_SLEEPTIME_INFINITE) { + UNLOCK_TCPIP_CORE(); + sys_arch_mbox_fetch(mbox, msg, 0); + LOCK_TCPIP_CORE(); + return; + } else if (sleeptime == 0) { + sys_check_timeouts(); + /* We try again to fetch a message from the mbox. */ + goto again; + } + + UNLOCK_TCPIP_CORE(); + res = sys_arch_mbox_fetch(mbox, msg, sleeptime); + LOCK_TCPIP_CORE(); + if (res == SYS_ARCH_TIMEOUT) { + /* If a SYS_ARCH_TIMEOUT value is returned, a timeout occurred + before a message could be fetched. */ + sys_check_timeouts(); + /* We try again to fetch a message from the mbox. */ + goto again; + } +} +#endif /* !LWIP_TIMERS */ /** * The main lwIP thread. This thread has exclusive access to lwIP core functions diff --git a/src/core/timeouts.c b/src/core/timeouts.c index c4054177..4a71d430 100644 --- a/src/core/timeouts.c +++ b/src/core/timeouts.c @@ -66,8 +66,10 @@ #define HANDLER(x) x #endif /* LWIP_DEBUG_TIMERNAMES */ +#define LWIP_MAX_TIMEOUT 0x7fffffff + /* Check if timer's expiry time is greater than time and care about u32_t wraparounds */ -#define TIME_LESS_THAN(t, compare_to) ( (((u32_t)((t)-(compare_to))) > 0x7fffffff) ? 1 : 0 ) +#define TIME_LESS_THAN(t, compare_to) ( (((u32_t)((t)-(compare_to))) > LWIP_MAX_TIMEOUT) ? 1 : 0 ) /** This array contains all stack-internal cyclic timers. To get the number of * timers, use LWIP_ARRAYSIZE() */ @@ -340,9 +342,6 @@ sys_untimeout(sys_timeout_handler handler, void *arg) * * Must be called periodically from your main loop. */ -#if !LWIP_TESTMODE && !NO_SYS && !defined __DOXYGEN__ -static -#endif /* !NO_SYS */ void sys_check_timeouts(void) { @@ -417,9 +416,6 @@ sys_restart_timeouts(void) /** Return the time left before the next timeout is due. If no timeouts are * enqueued, returns 0xffffffff */ -#if !LWIP_TESTMODE && !NO_SYS -static -#endif /* !NO_SYS */ u32_t sys_timeouts_sleeptime(void) { @@ -428,61 +424,18 @@ sys_timeouts_sleeptime(void) LWIP_ASSERT_CORE_LOCKED(); if (next_timeout == NULL) { - return 0xffffffff; + return SYS_TIMEOUTS_SLEEPTIME_INFINITE; } now = sys_now(); if (TIME_LESS_THAN(next_timeout->time, now)) { return 0; } else { - return (u32_t)(next_timeout->time - now); + u32_t ret = (u32_t)(next_timeout->time - now); + LWIP_ASSERT("invalid sleeptime", ret <= LWIP_MAX_TIMEOUT); + return ret; } } -#if !NO_SYS - -/** - * Wait (forever) for a message to arrive in an mbox. - * While waiting, timeouts are processed. - * - * @param mbox the mbox to fetch the message from - * @param msg the place to store the message - */ -void -sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg) -{ - u32_t sleeptime, res; - -again: - LWIP_ASSERT_CORE_LOCKED(); - - if (!next_timeout) { - UNLOCK_TCPIP_CORE(); - sys_arch_mbox_fetch(mbox, msg, 0); - LOCK_TCPIP_CORE(); - return; - } - - sleeptime = sys_timeouts_sleeptime(); - if (sleeptime == 0) { - sys_check_timeouts(); - /* We try again to fetch a message from the mbox. */ - goto again; - } - - UNLOCK_TCPIP_CORE(); - res = sys_arch_mbox_fetch(mbox, msg, sleeptime); - LOCK_TCPIP_CORE(); - if (res == SYS_ARCH_TIMEOUT) { - /* If a SYS_ARCH_TIMEOUT value is returned, a timeout occurred - before a message could be fetched. */ - sys_check_timeouts(); - /* We try again to fetch a message from the mbox. */ - goto again; - } -} - -#endif /* NO_SYS */ - #else /* LWIP_TIMERS && !LWIP_TIMERS_CUSTOM */ /* Satisfy the TCP code which calls this function */ void diff --git a/src/include/lwip/timeouts.h b/src/include/lwip/timeouts.h index f28fa9e6..eadefb89 100644 --- a/src/include/lwip/timeouts.h +++ b/src/include/lwip/timeouts.h @@ -56,6 +56,11 @@ extern "C" { #endif /* LWIP_DEBUG*/ #endif +/** Returned by sys_timeouts_sleeptime() to indicate there is no timer, so we + * can sleep forever. + */ +#define SYS_TIMEOUTS_SLEEPTIME_INFINITE 0xFFFFFFFF + /** Function prototype for a stack-internal timer function that has to be * called at a defined interval */ typedef void (* lwip_cyclic_timer_handler)(void); @@ -106,13 +111,8 @@ void sys_timeout(u32_t msecs, sys_timeout_handler handler, void *arg); void sys_untimeout(sys_timeout_handler handler, void *arg); void sys_restart_timeouts(void); -#if LWIP_TESTMODE || NO_SYS void sys_check_timeouts(void); u32_t sys_timeouts_sleeptime(void); -#endif -#if !NO_SYS -void sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg); -#endif /* NO_SYS */ #if LWIP_TESTMODE struct sys_timeo** lwip_sys_timers_get_next_timout(void);