From 68385d37def8d102b66da0cfc8f9b3d5f2a58ae8 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Tue, 2 Jan 2018 18:26:28 +0100 Subject: [PATCH] Work on task #14780: Add debug helper asserts to ensure threading/locking requirements are met Implement checks in unix port --- ports/unix/lib/lwipopts.h | 23 +++++++++++++++++++- ports/unix/minimal/lwipopts.h | 22 +++++++++++++++++++ ports/unix/port/sys_arch.c | 40 ++++++++++++++++++++++++++++++++++- ports/unix/unixsim/lwipopts.h | 24 ++++++++++++++++++++- 4 files changed, 106 insertions(+), 3 deletions(-) diff --git a/ports/unix/lib/lwipopts.h b/ports/unix/lib/lwipopts.h index 7675261..5bd79f9 100644 --- a/ports/unix/lib/lwipopts.h +++ b/ports/unix/lib/lwipopts.h @@ -414,6 +414,27 @@ #define PPP_SUPPORT 0 -/* Misc */ +/* + --------------------------------------- + ---------- Threading options ---------- + --------------------------------------- +*/ + +#define LWIP_TCPIP_CORE_LOCKING 1 + +#if !NO_SYS +void sys_check_core_locking(void); +#define LWIP_ASSERT_CORE_LOCKED() sys_check_core_locking() +void sys_mark_tcpip_thread(void); +#define LWIP_MARK_TCPIP_THREAD() sys_mark_tcpip_thread() + +#if LWIP_TCPIP_CORE_LOCKING +void sys_lock_tcpip_core(void); +#define LOCK_TCPIP_CORE() sys_lock_tcpip_core() +void sys_unlock_tcpip_core(void); +#define UNLOCK_TCPIP_CORE() sys_unlock_tcpip_core() +#endif +#endif + #endif /* LWIP_LWIPOPTS_H */ diff --git a/ports/unix/minimal/lwipopts.h b/ports/unix/minimal/lwipopts.h index 3388839..e83dd47 100644 --- a/ports/unix/minimal/lwipopts.h +++ b/ports/unix/minimal/lwipopts.h @@ -437,4 +437,26 @@ extern unsigned char debug_flags; #define LWIP_DBG_TYPES_ON debug_flags +/* + --------------------------------------- + ---------- Threading options ---------- + --------------------------------------- +*/ + +#define LWIP_TCPIP_CORE_LOCKING 1 + +#if !NO_SYS +void sys_check_core_locking(void); +#define LWIP_ASSERT_CORE_LOCKED() sys_check_core_locking() +void sys_mark_tcpip_thread(void); +#define LWIP_MARK_TCPIP_THREAD() sys_mark_tcpip_thread() + +#if LWIP_TCPIP_CORE_LOCKING +void sys_lock_tcpip_core(void); +#define LOCK_TCPIP_CORE() sys_lock_tcpip_core() +void sys_unlock_tcpip_core(void); +#define UNLOCK_TCPIP_CORE() sys_unlock_tcpip_core() +#endif +#endif + #endif /* LWIP_LWIPOPTS_H */ diff --git a/ports/unix/port/sys_arch.c b/ports/unix/port/sys_arch.c index c94969b..9338531 100644 --- a/ports/unix/port/sys_arch.c +++ b/ports/unix/port/sys_arch.c @@ -64,6 +64,7 @@ #include "lwip/sys.h" #include "lwip/opt.h" #include "lwip/stats.h" +#include "lwip/tcpip.h" static void get_monotonic_time(struct timespec *ts) @@ -180,6 +181,42 @@ sys_thread_new(const char *name, lwip_thread_fn function, void *arg, int stacksi return st; } +#if !NO_SYS +#if LWIP_TCPIP_CORE_LOCKING +static u8_t lwip_core_locked; +void sys_lock_tcpip_core(void) +{ + sys_mutex_lock(&lock_tcpip_core); + lwip_core_locked = 1; +} + +void sys_unlock_tcpip_core(void) +{ + lwip_core_locked = 0; + sys_mutex_unlock(&lock_tcpip_core); +} +#endif /* LWIP_TCPIP_CORE_LOCKING */ + +static pthread_t lwip_tcpip_thread_id; +void sys_mark_tcpip_thread(void) +{ + lwip_tcpip_thread_id = pthread_self(); +} + +void sys_check_core_locking(void) +{ + if (lwip_tcpip_thread_id != 0) { + pthread_t current_thread_id = pthread_self(); + +#if LWIP_TCPIP_CORE_LOCKING + LWIP_ASSERT("Function called without core lock", (current_thread_id == lwip_tcpip_thread_id) || lwip_core_locked); +#else /* LWIP_TCPIP_CORE_LOCKING */ + LWIP_ASSERT("Function called from wrong thread", current_thread_id == lwip_tcpip_thread_id); +#endif /* LWIP_TCPIP_CORE_LOCKING */ + } +} +#endif /* !NO_SYS */ + /*-----------------------------------------------------------------------------------*/ /* Mailbox */ err_t @@ -639,7 +676,8 @@ sys_arch_unprotect(sys_prot_t pval) LWIP_UNUSED_ARG(pval); if (lwprot_thread == pthread_self()) { - if (--lwprot_count == 0) + lwprot_count--; + if (lwprot_count == 0) { lwprot_thread = (pthread_t) 0xDEAD; pthread_mutex_unlock(&lwprot_mutex); diff --git a/ports/unix/unixsim/lwipopts.h b/ports/unix/unixsim/lwipopts.h index 1009e85..68ce048 100644 --- a/ports/unix/unixsim/lwipopts.h +++ b/ports/unix/unixsim/lwipopts.h @@ -369,6 +369,28 @@ extern void sntp_set_system_time(u32_t sec); #endif /* PPP_SUPPORT > 0 */ -#define LWIP_HTTPD_SSI 1 +#define LWIP_HTTPD_SSI 1 + +/* + --------------------------------------- + ---------- Threading options ---------- + --------------------------------------- +*/ + +#define LWIP_TCPIP_CORE_LOCKING 1 + +#if !NO_SYS +void sys_check_core_locking(void); +#define LWIP_ASSERT_CORE_LOCKED() sys_check_core_locking() +void sys_mark_tcpip_thread(void); +#define LWIP_MARK_TCPIP_THREAD() sys_mark_tcpip_thread() + +#if LWIP_TCPIP_CORE_LOCKING +void sys_lock_tcpip_core(void); +#define LOCK_TCPIP_CORE() sys_lock_tcpip_core() +void sys_unlock_tcpip_core(void); +#define UNLOCK_TCPIP_CORE() sys_unlock_tcpip_core() +#endif +#endif #endif /* LWIP_LWIPOPTS_H */