'Merge branch 'thread_local_storage_delete_callbacks' into 'master'

The thread-local-storage feature in FreeRTOS attaches an application-usable array of pointers to a thread control block. These pointers usually point to a structure the thread allocates. When a thread gets (voluntarily or involuntarily) destroyed, this memory can leak. This merge adds a matching second array of user-settable pointers to destructor routines. As soon as the task gets cleaned up (which happens in the idle thread), the destructors get called and the memory can be freed.

See merge request !19
This commit is contained in:
Jeroen Domburg 2016-08-24 13:29:06 +08:00
parent 94bcf9d994
commit 29c2e58c75
6 changed files with 64 additions and 3 deletions

View file

@ -73,7 +73,7 @@ config FREERTOS_CHECK_STACKOVERFLOW_CANARY
(configCHECK_FOR_STACK_OVERFLOW=2)
endchoice
config CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS
config FREERTOS_THREAD_LOCAL_STORAGE_POINTERS
int "Amount of thread local storage pointers"
range 0 256
default 0

View file

@ -97,6 +97,7 @@
#endif
#define configNUM_THREAD_LOCAL_STORAGE_POINTERS CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS
#define configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS 1
/* TODO: config freq by menuconfig */
#define XT_CLOCK_FREQ 80000000

View file

@ -1175,6 +1175,11 @@ constant. */
void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet, BaseType_t xIndex, void *pvValue ) PRIVILEGED_FUNCTION;
void *pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery, BaseType_t xIndex ) PRIVILEGED_FUNCTION;
#if ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
typedef void (*TlsDeleteCallbackFunction_t)( int, void * );
void vTaskSetThreadLocalStoragePointerAndDelCallback( TaskHandle_t xTaskToSet, BaseType_t xIndex, void *pvValue , TlsDeleteCallbackFunction_t xDelCallback);
#endif
#endif
/**

View file

@ -24,3 +24,9 @@ means that when you set a handler for an interrupt, it will get triggered if
the interrupt is triggered on both CPU0 as well as on CPU1. This is something
we may change in future FreeRTOS-esp32 releases.
- This FreeRTOS version has the task local storage backported from the 8.2.x
versions. It, however, has an addition: you can also set a callback when you
set the pointer. This callback will be called by the idle task, with the
pointer as an argument, when the thread is destroyed. This depends on the idle
task getting CPU time; when a thread is hogging the CPU without yielding,
the idle thread won't be called and the delete callback won't be called either.

View file

@ -180,6 +180,9 @@ typedef struct tskTaskControlBlock
#if( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
void *pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
#if ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
TlsDeleteCallbackFunction_t pvThreadLocalStoragePointersDelCallback[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
#endif
#endif
#if ( configGENERATE_RUN_TIME_STATS == 1 )
@ -3045,7 +3048,10 @@ UBaseType_t x;
{
for( x = 0; x < ( UBaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS; x++ )
{
pxNewTCB->pvThreadLocalStoragePointers[ x ] = NULL;
pxTCB->pvThreadLocalStoragePointers[ x ] = NULL;
#if ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
pxTCB->pvThreadLocalStoragePointersDelCallback[ x ] = (TlsDeleteCallbackFunction_t)NULL;
#endif
}
}
#endif
@ -3068,6 +3074,29 @@ UBaseType_t x;
/*-----------------------------------------------------------*/
#if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
#if ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
void vTaskSetThreadLocalStoragePointerAndDelCallback( TaskHandle_t xTaskToSet, BaseType_t xIndex, void *pvValue , TlsDeleteCallbackFunction_t xDelCallback)
{
TCB_t *pxTCB;
if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
{
pxTCB = prvGetTCBFromHandle( xTaskToSet );
taskENTER_CRITICAL(&xTaskQueueMutex);
pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
pxTCB->pvThreadLocalStoragePointersDelCallback[ xIndex ] = xDelCallback;
taskEXIT_CRITICAL(&xTaskQueueMutex);
}
}
void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet, BaseType_t xIndex, void *pvValue )
{
vTaskSetThreadLocalStoragePointerAndDelCallback( xTaskToSet, xIndex, pvValue, (TlsDeleteCallbackFunction_t)NULL );
}
#else
void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet, BaseType_t xIndex, void *pvValue )
{
TCB_t *pxTCB;
@ -3078,6 +3107,7 @@ UBaseType_t x;
pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
}
}
#endif /* configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS */
#endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
/*-----------------------------------------------------------*/
@ -3181,7 +3211,19 @@ static void prvCheckTasksWaitingTermination( void )
--uxTasksDeleted;
}
taskEXIT_CRITICAL(&xTaskQueueMutex);
#if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 ) && ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
{
int x;
for( x = 0; x < ( UBaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS; x++ )
{
if (pxTCB->pvThreadLocalStoragePointersDelCallback[ x ] != NULL)
{
pxTCB->pvThreadLocalStoragePointersDelCallback[ x ](x, pxTCB->pvThreadLocalStoragePointers[ x ]);
}
}
}
#endif
prvDeleteTCB( pxTCB );
}
else

View file

@ -9,6 +9,13 @@ config LWIP_MAX_SOCKETS
sockets to be open at the same time conserves memory. Specify
the maximum amount of sockets here.
config LWIP_THREAD_LOCAL_STORAGE_INDEX
int "Index for thread-local-storage pointer for lwip"
default 0
help
Specify the thread-local-storage-pointer index for lwip
use.
endmenu