From 91135da1904990f8c755dac2384f150ee872e952 Mon Sep 17 00:00:00 2001 From: liuzhifu Date: Fri, 19 Aug 2016 17:23:04 +0800 Subject: [PATCH] impact components: lwip/freertos 1. Remove xTaskGetPerTaskData 2. Implement lwip per thread semaphore with vTaskSetThreadLocalStoragePointer and pvTaskGetThreadLocalStoragePointer 3. Add sys_thread_sem_get/sys_thread_sem_init/sys_thread_sem_deinit --- components/freertos/tasks.c | 29 ---------- .../lwip/include/lwip/lwip/priv/api_msg.h | 6 +-- .../lwip/include/lwip/port/arch/sys_arch.h | 6 +-- components/lwip/port/freertos/sys_arch.c | 54 ++++++++++++++++--- 4 files changed, 53 insertions(+), 42 deletions(-) diff --git a/components/freertos/tasks.c b/components/freertos/tasks.c index adeaee60c..f9ad8ff8c 100644 --- a/components/freertos/tasks.c +++ b/components/freertos/tasks.c @@ -206,10 +206,6 @@ typedef struct tskTaskControlBlock volatile eNotifyValue eNotifyState; #endif -#if (configESP32_PER_TASK_DATA == 1) - void *data; -#endif - } tskTCB; /* The old tskTCB name is maintained above then typedefed to the new TCB_t name @@ -611,9 +607,6 @@ BaseType_t i; if( pxNewTCB != NULL ) { -#if (configESP32_PER_TASK_DATA == 1) - pxNewTCB->data = NULL; -#endif #if( portUSING_MPU_WRAPPERS == 1 ) /* Should the task be created in privileged mode? */ BaseType_t xRunPrivileged; @@ -799,11 +792,6 @@ BaseType_t i; being deleted. */ pxTCB = prvGetTCBFromHandle( xTaskToDelete ); -#if (configESP32_PER_TASK_DATA == 1) - if (pxTCB->data){ - vSemaphoreDelete( pxTCB->data ); - } -#endif /* Remove task from the ready list and place in the termination list. This will stop the task from be scheduled. The idle task will check the termination list and free up any memory allocated by the @@ -4592,23 +4580,6 @@ TickType_t uxReturn; #endif /* configUSE_TASK_NOTIFICATIONS */ -/*-----------------------------------------------------------*/ -#if (configESP32_PER_TASK_DATA == 1) -void* xTaskGetPerTaskData(void) -{ - TCB_t *pxTCB = (TCB_t*)(xTaskGetCurrentTaskHandle()); - if (pxTCB){ - if (!pxTCB->data){ - vSemaphoreCreateBinary(pxTCB->data); - } - return (void*)(&pxTCB->data); - } - - return NULL; -} -#endif /* configESP32_PER_TASK_DATA */ -/*-----------------------------------------------------------*/ - #ifdef FREERTOS_MODULE_TEST #include "tasks_test_access_functions.h" #endif diff --git a/components/lwip/include/lwip/lwip/priv/api_msg.h b/components/lwip/include/lwip/lwip/priv/api_msg.h index dceff82d6..329fa0de3 100755 --- a/components/lwip/include/lwip/lwip/priv/api_msg.h +++ b/components/lwip/include/lwip/lwip/priv/api_msg.h @@ -188,9 +188,9 @@ struct dns_api_msg { #if LWIP_NETCONN_SEM_PER_THREAD #ifdef LWIP_ESP8266 -#define LWIP_NETCONN_THREAD_SEM_GET() sys_thread_sem() -#define LWIP_NETCONN_THREAD_SEM_ALLOC() -#define LWIP_NETCONN_THREAD_SEM_FREE() +#define LWIP_NETCONN_THREAD_SEM_GET() sys_thread_sem_get() +#define LWIP_NETCONN_THREAD_SEM_ALLOC() sys_thread_sem_init() +#define LWIP_NETCONN_THREAD_SEM_FREE() sys_thread_sem_deinit() #endif #endif diff --git a/components/lwip/include/lwip/port/arch/sys_arch.h b/components/lwip/include/lwip/port/arch/sys_arch.h index 3d9e460e0..945b4e170 100644 --- a/components/lwip/include/lwip/port/arch/sys_arch.h +++ b/components/lwip/include/lwip/port/arch/sys_arch.h @@ -64,10 +64,10 @@ typedef struct sys_mbox_s { void sys_arch_assert(const char *file, int line); uint32_t system_get_time(void); -sys_sem_t* sys_thread_sem(void); void sys_delay_ms(uint32_t ms); -void* xTaskGetPerTaskData(void); - +sys_sem_t* sys_thread_sem_init(void); +void sys_thread_sem_deinit(void); +sys_sem_t* sys_thread_sem_get(void); #endif /* __SYS_ARCH_H__ */ diff --git a/components/lwip/port/freertos/sys_arch.c b/components/lwip/port/freertos/sys_arch.c index 93272a603..3ae4bcda4 100755 --- a/components/lwip/port/freertos/sys_arch.c +++ b/components/lwip/port/freertos/sys_arch.c @@ -475,17 +475,57 @@ sys_arch_assert(const char *file, int line) while(1); } -/* This is a super hacky thread-local-storage repository - FreeRTOS 8.2.3 & up have thread local storage in the - OS, which is how we should do this. Once we upgrade FreeRTOS, - we can drop this hacky store and use the FreeRTOS TLS API. -*/ -sys_sem_t* sys_thread_sem(void) +/* + * get per thread semphore + */ +sys_sem_t* sys_thread_sem_get(void) { - sys_sem_t *sem = (sys_sem_t*)xTaskGetPerTaskData(); + sys_sem_t *sem = (sys_sem_t*)pvTaskGetThreadLocalStoragePointer(xTaskGetCurrentTaskHandle(), 0); + if (!sem){ + sem = sys_thread_sem_init(); + } + LWIP_DEBUGF(THREAD_SAFE_DEBUG, ("sem_get s=%p\n", sem)); return sem; } +sys_sem_t* sys_thread_sem_init(void) +{ + sys_sem_t *sem = (sys_sem_t*)malloc(sizeof(sys_sem_t*)); + + if (!sem){ + printf("sem f1\n"); + return 0; + } + + *sem = xSemaphoreCreateBinary(); + if (!(*sem)){ + free(sem); + printf("sem f2\n"); + return 0; + } + + LWIP_DEBUGF(THREAD_SAFE_DEBUG, ("sem init %p %p\n", sem, *sem)); + vTaskSetThreadLocalStoragePointer(xTaskGetCurrentTaskHandle(), 0, sem); + + return sem; +} + +void sys_thread_sem_deinit(void) +{ + sys_sem_t *sem = sys_thread_sem_get(); + if (sem && *sem){ + LWIP_DEBUGF(THREAD_SAFE_DEBUG, ("sem del:%p %p\n", sem, *sem)); + vSemaphoreDelete(*sem); + } + + if (sem){ + free(sem); + } + + vTaskSetThreadLocalStoragePointer(xTaskGetCurrentTaskHandle(), 0, 0); + return; +} + void sys_delay_ms(uint32_t ms) { vTaskDelay(ms/portTICK_RATE_MS);