From 7c8bb8c2934004cff79f59f66ff0f5f9b7873be9 Mon Sep 17 00:00:00 2001 From: cuiziwei Date: Tue, 11 Jul 2023 10:52:32 +0800 Subject: [PATCH] nuttx/tls: Remove the max key limiatation in task_tls_alloc and pthread_key_create Signed-off-by: cuiziwei --- include/nuttx/tls.h | 29 ++---------------- libs/libc/pthread/pthread_keycreate.c | 27 +++++++++++++--- libs/libc/pthread/pthread_keydelete.c | 5 +-- libs/libc/tls/Kconfig | 4 +-- libs/libc/tls/tls_destruct.c | 14 +++------ sched/task/task_tls_alloc.c | 44 ++++++++++++++++++--------- 6 files changed, 61 insertions(+), 62 deletions(-) diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h index 72fdf7e438..b9e0a2bf92 100644 --- a/include/nuttx/tls.h +++ b/include/nuttx/tls.h @@ -73,38 +73,14 @@ extern "C" #define EXTERN extern #endif -/* type tls_ndxset_t & tls_dtor_t *******************************************/ +/* type tls_dtor_t **********************************************************/ /* Smallest addressable type that can hold the entire configured number of * TLS data indexes. */ #if CONFIG_TLS_NELEM > 0 -# if CONFIG_TLS_NELEM > 64 -# error Too many TLS elements -# elif CONFIG_TLS_NELEM > 32 - typedef uint64_t tls_ndxset_t; -# elif CONFIG_TLS_NELEM > 16 - typedef uint32_t tls_ndxset_t; -# elif CONFIG_TLS_NELEM > 8 - typedef uint16_t tls_ndxset_t; -# else - typedef uint8_t tls_ndxset_t; -# endif -#endif - -#if CONFIG_TLS_TASK_NELEM > 0 -# if CONFIG_TLS_TASK_NELEM > 64 -# error Too many TLS elements -# elif CONFIG_TLS_TASK_NELEM > 32 - typedef uint64_t tls_task_ndxset_t; -# elif CONFIG_TLS_TASK_NELEM > 16 - typedef uint32_t tls_task_ndxset_t; -# elif CONFIG_TLS_TASK_NELEM > 8 - typedef uint16_t tls_task_ndxset_t; -# else - typedef uint8_t tls_task_ndxset_t; -# endif +typedef CODE void (*tls_dtor_t)(FAR void *); #endif typedef CODE void (*tls_dtor_t)(FAR void *); @@ -149,7 +125,6 @@ struct task_info_s uintptr_t ta_telem[CONFIG_TLS_TASK_NELEM]; /* Task local storage elements */ #endif #if CONFIG_TLS_NELEM > 0 - tls_ndxset_t ta_tlsset; /* Set of TLS indexes allocated */ tls_dtor_t ta_tlsdtor[CONFIG_TLS_NELEM]; /* List of TLS destructors */ #endif #ifndef CONFIG_BUILD_KERNEL diff --git a/libs/libc/pthread/pthread_keycreate.c b/libs/libc/pthread/pthread_keycreate.c index 4a70a8a8f7..f4d00e234f 100644 --- a/libs/libc/pthread/pthread_keycreate.c +++ b/libs/libc/pthread/pthread_keycreate.c @@ -33,6 +33,19 @@ #if CONFIG_TLS_NELEM > 0 +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: pthread_destructor + ****************************************************************************/ + +static void pthread_destructor(FAR void *arg) +{ + UNUSED(arg); +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -95,13 +108,19 @@ int pthread_key_create(FAR pthread_key_t *key, { /* Is this candidate index available? */ - tls_ndxset_t mask = (tls_ndxset_t)1 << candidate; - if ((info->ta_tlsset & mask) == 0) + if (info->ta_tlsdtor[candidate] == NULL) { /* Yes.. allocate the index and break out of the loop */ - info->ta_tlsset |= mask; - info->ta_tlsdtor[candidate] = destructor; + if (destructor) + { + info->ta_tlsdtor[candidate] = destructor; + } + else + { + info->ta_tlsdtor[candidate] = pthread_destructor; + } + *key = candidate; ret = OK; break; diff --git a/libs/libc/pthread/pthread_keydelete.c b/libs/libc/pthread/pthread_keydelete.c index c0ac344b69..1ade48412f 100644 --- a/libs/libc/pthread/pthread_keydelete.c +++ b/libs/libc/pthread/pthread_keydelete.c @@ -58,7 +58,6 @@ int pthread_key_delete(pthread_key_t key) { FAR struct task_info_s *info = task_get_info(); - tls_ndxset_t mask; int ret = EINVAL; DEBUGASSERT(info != NULL); @@ -69,12 +68,10 @@ int pthread_key_delete(pthread_key_t key) * modification of the group TLS index set. */ - mask = (tls_ndxset_t)1 << key; ret = nxmutex_lock(&info->ta_lock); if (ret == OK) { - DEBUGASSERT((info->ta_tlsset & mask) != 0); - info->ta_tlsset &= ~mask; + info->ta_tlsdtor[key] = NULL; nxmutex_unlock(&info->ta_lock); } else diff --git a/libs/libc/tls/Kconfig b/libs/libc/tls/Kconfig index 62b16ca078..cf4b6e60d5 100644 --- a/libs/libc/tls/Kconfig +++ b/libs/libc/tls/Kconfig @@ -45,7 +45,7 @@ config TLS_LOG2_MAXSTACK config TLS_NELEM int "Number of TLS elements" default 0 - range 0 64 + range 0 255 ---help--- The number of unique TLS elements. These can be accessed with the user library functions tls_get_value() and tls_set_value() @@ -57,7 +57,7 @@ config TLS_NELEM config TLS_TASK_NELEM int "Number of Task Local Storage elements" default 0 - range 0 64 + range 0 255 ---help--- The number of unique Task Local Storage elements similar with Thread Local Storage. diff --git a/libs/libc/tls/tls_destruct.c b/libs/libc/tls/tls_destruct.c index 10c56c21ae..baa857d4b9 100644 --- a/libs/libc/tls/tls_destruct.c +++ b/libs/libc/tls/tls_destruct.c @@ -54,25 +54,19 @@ void tls_destruct(void) FAR struct tls_info_s *tls = tls_get_info(); FAR void *tls_elem_ptr = NULL; tls_dtor_t destructor; - tls_ndxset_t tlsset; int candidate; DEBUGASSERT(info != NULL); - tlsset = info->ta_tlsset; for (candidate = CONFIG_TLS_NELEM - 1; candidate >= 0; candidate--) { /* Is this candidate index available? */ - tls_ndxset_t mask = (tls_ndxset_t)1 << candidate; - if (tlsset & mask) + tls_elem_ptr = (FAR void *)tls->tl_elem[candidate]; + destructor = info->ta_tlsdtor[candidate]; + if (tls_elem_ptr && destructor) { - tls_elem_ptr = (FAR void *)tls->tl_elem[candidate]; - destructor = info->ta_tlsdtor[candidate]; - if (tls_elem_ptr && destructor) - { - destructor(tls_elem_ptr); - } + destructor(tls_elem_ptr); } tls->tl_elem[candidate] = 0; diff --git a/sched/task/task_tls_alloc.c b/sched/task/task_tls_alloc.c index a26fdecdc0..668726964a 100644 --- a/sched/task/task_tls_alloc.c +++ b/sched/task/task_tls_alloc.c @@ -36,10 +36,22 @@ #if CONFIG_TLS_TASK_NELEM > 0 -static tls_task_ndxset_t g_tlsset; static mutex_t g_tlslock = NXMUTEX_INITIALIZER; static tls_dtor_t g_tlsdtor[CONFIG_TLS_TASK_NELEM]; +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: tls_dtor + ****************************************************************************/ + +static void tls_dtor(FAR void *arg) +{ + UNUSED(arg); +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -73,11 +85,17 @@ int task_tls_alloc(tls_dtor_t dtor) for (candidate = 0; candidate < CONFIG_TLS_TASK_NELEM; candidate++) { - tls_task_ndxset_t mask = (tls_task_ndxset_t)1 << candidate; - if ((g_tlsset & mask) == 0) + if (g_tlsdtor[candidate] == NULL) { - g_tlsset |= mask; - g_tlsdtor[candidate] = dtor; + if (dtor) + { + g_tlsdtor[candidate] = dtor; + } + else + { + g_tlsdtor[candidate] = tls_dtor; + } + ret = candidate; break; } @@ -110,18 +128,14 @@ void task_tls_destruct(void) for (candidate = CONFIG_TLS_TASK_NELEM - 1; candidate >= 0; candidate--) { - tls_task_ndxset_t mask = (tls_task_ndxset_t)1 << candidate; - if ((g_tlsset & mask) != 0) + elem = info->ta_telem[candidate]; + dtor = g_tlsdtor[candidate]; + if (dtor != NULL && elem != 0) { - elem = info->ta_telem[candidate]; - dtor = g_tlsdtor[candidate]; - if (dtor != NULL && elem != 0) - { - dtor((void *)elem); - } - - info->ta_telem[candidate] = 0; + dtor((FAR void *)elem); } + + info->ta_telem[candidate] = 0; } }