mirror of
https://github.com/espressif/esp-idf.git
synced 2026-05-05 12:25:03 +02:00
esp_netif/lwip: Fix core-locking config
* Fix thread safety issues in non-core locking * Add option to verify thread safety issues in lwip (core-lock assertion) * Make esp_sntp.h thread safe API * Fix sntp examples * Fix openthread libs Closes https://github.com/espressif/esp-idf/issues/9908 Closes https://github.com/espressif/esp-idf/issues/10502 Closes https://github.com/espressif/esp-idf/issues/10466
This commit is contained in:
@@ -53,7 +53,6 @@ static sys_mutex_t g_lwip_protect_mutex = NULL;
|
||||
|
||||
static pthread_key_t sys_thread_sem_key;
|
||||
static void sys_thread_sem_free(void* data);
|
||||
sys_thread_t g_lwip_task;
|
||||
|
||||
#if !LWIP_COMPAT_MUTEX
|
||||
|
||||
@@ -428,10 +427,8 @@ sys_thread_new(const char *name, lwip_thread_fn thread, void *arg, int stacksize
|
||||
ret = xTaskCreatePinnedToCore(thread, name, stacksize, arg, prio, &rtos_task,
|
||||
CONFIG_LWIP_TCPIP_TASK_AFFINITY);
|
||||
|
||||
g_lwip_task = rtos_task;
|
||||
|
||||
LWIP_DEBUGF(TCPIP_DEBUG, ("tcpip_task_hdlxxx : %x, prio:%d,stack:%d\n",
|
||||
(u32_t)g_lwip_task,TCPIP_THREAD_PRIO,TCPIP_THREAD_STACKSIZE));
|
||||
LWIP_DEBUGF(TCPIP_DEBUG, ("new lwip task : %x, prio:%d,stack:%d\n",
|
||||
(u32_t)rtos_task, prio, stacksize));
|
||||
|
||||
if (ret != pdTRUE) {
|
||||
return NULL;
|
||||
@@ -577,3 +574,36 @@ sys_delay_ms(uint32_t ms)
|
||||
{
|
||||
vTaskDelay(ms / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
bool
|
||||
sys_thread_tcpip(sys_thread_core_lock_t type)
|
||||
{
|
||||
static sys_thread_t lwip_task = NULL;
|
||||
#if LWIP_TCPIP_CORE_LOCKING
|
||||
static sys_thread_t core_lock_holder = NULL;
|
||||
#endif
|
||||
switch (type) {
|
||||
default:
|
||||
return false;
|
||||
case LWIP_CORE_IS_TCPIP_INITIALIZED:
|
||||
return lwip_task != NULL;
|
||||
case LWIP_CORE_MARK_TCPIP_TASK:
|
||||
LWIP_ASSERT("LWIP_CORE_MARK_TCPIP_TASK: lwip_task == NULL", (lwip_task == NULL));
|
||||
lwip_task = (sys_thread_t) xTaskGetCurrentTaskHandle();
|
||||
return true;
|
||||
#if LWIP_TCPIP_CORE_LOCKING
|
||||
case LWIP_CORE_LOCK_QUERY_HOLDER:
|
||||
return lwip_task ? core_lock_holder == (sys_thread_t) xTaskGetCurrentTaskHandle() : true;
|
||||
case LWIP_CORE_LOCK_MARK_HOLDER:
|
||||
core_lock_holder = (sys_thread_t) xTaskGetCurrentTaskHandle();
|
||||
return true;
|
||||
case LWIP_CORE_LOCK_UNMARK_HOLDER:
|
||||
core_lock_holder = NULL;
|
||||
return true;
|
||||
#else
|
||||
case LWIP_CORE_LOCK_QUERY_HOLDER:
|
||||
return lwip_task == NULL || lwip_task == (sys_thread_t) xTaskGetCurrentTaskHandle();
|
||||
#endif /* LWIP_TCPIP_CORE_LOCKING */
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -100,6 +100,17 @@ sys_sem_t* sys_thread_sem_init(void);
|
||||
void sys_thread_sem_deinit(void);
|
||||
sys_sem_t* sys_thread_sem_get(void);
|
||||
|
||||
typedef enum {
|
||||
LWIP_CORE_LOCK_QUERY_HOLDER,
|
||||
LWIP_CORE_LOCK_MARK_HOLDER,
|
||||
LWIP_CORE_LOCK_UNMARK_HOLDER,
|
||||
LWIP_CORE_MARK_TCPIP_TASK,
|
||||
LWIP_CORE_IS_TCPIP_INITIALIZED,
|
||||
} sys_thread_core_lock_t;
|
||||
|
||||
bool
|
||||
sys_thread_tcpip(sys_thread_core_lock_t type);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "lwip/pbuf.h"
|
||||
#include "netif/dhcp_state.h"
|
||||
|
||||
|
||||
#ifdef ESP_IDF_LWIP_HOOK_FILENAME
|
||||
#include ESP_IDF_LWIP_HOOK_FILENAME
|
||||
#endif
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "sdkconfig.h"
|
||||
#include "sntp/sntp_get_set_time.h"
|
||||
#include "sockets_ext.h"
|
||||
#include "arch/sys_arch.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
@@ -46,9 +47,20 @@ extern "C" {
|
||||
*/
|
||||
#ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING
|
||||
#define LWIP_TCPIP_CORE_LOCKING 1
|
||||
#define LOCK_TCPIP_CORE() do { sys_mutex_lock(&lock_tcpip_core); sys_thread_tcpip(LWIP_CORE_LOCK_MARK_HOLDER); } while(0)
|
||||
#define UNLOCK_TCPIP_CORE() do { sys_thread_tcpip(LWIP_CORE_LOCK_UNMARK_HOLDER); sys_mutex_unlock(&lock_tcpip_core); } while(0)
|
||||
#ifdef CONFIG_LWIP_CHECK_THREAD_SAFETY
|
||||
#define LWIP_ASSERT_CORE_LOCKED() do { LWIP_ASSERT("Required to lock TCPIP core functionality!", sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)); } while(0)
|
||||
#endif /* CONFIG_LWIP_CHECK_THREAD_SAFETY */
|
||||
|
||||
#else
|
||||
#define LWIP_TCPIP_CORE_LOCKING 0
|
||||
#endif
|
||||
#ifdef CONFIG_LWIP_CHECK_THREAD_SAFETY
|
||||
#define LWIP_ASSERT_CORE_LOCKED() do { LWIP_ASSERT("Required to run in TCPIP context!", sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)); } while(0)
|
||||
#endif /* CONFIG_LWIP_CHECK_THREAD_SAFETY */
|
||||
#endif /* CONFIG_LWIP_TCPIP_CORE_LOCKING */
|
||||
|
||||
#define LWIP_MARK_TCPIP_THREAD() sys_thread_tcpip(LWIP_CORE_MARK_TCPIP_TASK)
|
||||
|
||||
/**
|
||||
* SYS_LIGHTWEIGHT_PROT==1: if you want inter-task protection for certain
|
||||
@@ -845,11 +857,7 @@ static inline uint32_t timeout_from_offered(uint32_t lease, uint32_t min)
|
||||
* The latter 2 can be invoked up by calling netconn_thread_init()/netconn_thread_cleanup().
|
||||
* Ports may call these for threads created with sys_thread_new().
|
||||
*/
|
||||
#if LWIP_TCPIP_CORE_LOCKING
|
||||
#define LWIP_NETCONN_SEM_PER_THREAD 0
|
||||
#else
|
||||
#define LWIP_NETCONN_SEM_PER_THREAD 1
|
||||
#endif
|
||||
|
||||
/** LWIP_NETCONN_FULLDUPLEX==1: Enable code that allows reading from one thread,
|
||||
* writing from a 2nd thread and closing from a 3rd thread at the same time.
|
||||
@@ -1198,11 +1206,17 @@ static inline uint32_t timeout_from_offered(uint32_t lease, uint32_t min)
|
||||
#endif
|
||||
#define LWIP_HOOK_FILENAME "lwip_default_hooks.h"
|
||||
#define LWIP_HOOK_IP4_ROUTE_SRC ip4_route_src_hook
|
||||
#if LWIP_NETCONN_FULLDUPLEX
|
||||
#define LWIP_DONE_SOCK(s) done_socket(sock)
|
||||
#else
|
||||
#define LWIP_DONE_SOCK(s) ((void)1)
|
||||
#endif /* LWIP_NETCONN_FULLDUPLEX */
|
||||
|
||||
#define LWIP_HOOK_SOCKETS_GETSOCKOPT(s, sock, level, optname, optval, optlen, err) \
|
||||
lwip_getsockopt_impl_ext(sock, level, optname, optval, optlen, err)?(done_socket(sock), true): false
|
||||
lwip_getsockopt_impl_ext(sock, level, optname, optval, optlen, err)?(LWIP_DONE_SOCK(sock), true): false
|
||||
|
||||
#define LWIP_HOOK_SOCKETS_SETSOCKOPT(s, sock, level, optname, optval, optlen, err) \
|
||||
lwip_setsockopt_impl_ext(sock, level, optname, optval, optlen, err)?(done_socket(sock), true): false
|
||||
lwip_setsockopt_impl_ext(sock, level, optname, optval, optlen, err)?(LWIP_DONE_SOCK(sock), true): false
|
||||
|
||||
/*
|
||||
---------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user