From 53499f5e9faecb1593f9a3de54385ac4ebd9aa83 Mon Sep 17 00:00:00 2001 From: Our Air Quality Date: Tue, 2 Jan 2018 11:49:09 -0600 Subject: [PATCH] timers: rework the core locking around timers (bug #52719) Want the core lock held while working on the timer data structures. --- src/api/tcpip.c | 2 -- src/core/timeouts.c | 25 +++++++++++++++---------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/api/tcpip.c b/src/api/tcpip.c index 6763a5cb..608ba607 100644 --- a/src/api/tcpip.c +++ b/src/api/tcpip.c @@ -99,11 +99,9 @@ tcpip_thread(void *arg) LOCK_TCPIP_CORE(); while (1) { /* MAIN Loop */ - UNLOCK_TCPIP_CORE(); LWIP_TCPIP_THREAD_ALIVE(); /* wait for a message, timeouts are processed while waiting */ TCPIP_MBOX_FETCH(&mbox, (void **)&msg); - LOCK_TCPIP_CORE(); if (msg == NULL) { LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_thread: invalid message: NULL\n")); LWIP_ASSERT("tcpip_thread: invalid message", 0); diff --git a/src/core/timeouts.c b/src/core/timeouts.c index 01e0d0ec..de5300ad 100644 --- a/src/core/timeouts.c +++ b/src/core/timeouts.c @@ -350,15 +350,7 @@ sys_check_timeouts(void) #endif /* LWIP_DEBUG_TIMERNAMES */ memp_free(MEMP_SYS_TIMEOUT, tmptimeout); if (handler != NULL) { -#if !NO_SYS - /* For LWIP_TCPIP_CORE_LOCKING, lock the core before calling the - timeout handler function. */ - LOCK_TCPIP_CORE(); -#endif /* !NO_SYS */ handler(arg); -#if !NO_SYS - UNLOCK_TCPIP_CORE(); -#endif /* !NO_SYS */ } LWIP_TCPIP_THREAD_ALIVE(); } @@ -416,16 +408,29 @@ sys_timeouts_sleeptime(void) void sys_timeouts_mbox_fetch(sys_mbox_t *mbox, void **msg) { - u32_t sleeptime; + 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_arch_mbox_fetch(mbox, msg, sleeptime) == SYS_ARCH_TIMEOUT) { + 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();