This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 124aac52cd2374e854b10844ff41f4c9d2723bc0
Author: v-zhangxiaomeng5 <[email protected]>
AuthorDate: Fri Apr 17 23:05:48 2026 +0800

    tls: add dedicated fields for tls per-thread and per-group data
    
    Add storage hooks in NuttX TLS structures for libcxx/libcxxabi cascaded
    thread-local support, avoiding dependency on CONFIG_TLS_TASK_NELEM or
    pthread_key:
    
    tls_info_s (per-thread):
    - Add tl_eh_globals and tl_eh_free for per-thread __cxa_eh_globals
    - Free tl_eh_globals in tls_destruct() on thread exit
    - Extend tls_destruct() compile guard to cover CONFIG_CXX_EXCEPTION
    
    task_info_s (per-task-group):
    - Add ta_thread_local_data and ta_thread_local_dtor for libcxx 
__thread_specific_ptr
    - Free ta_thread_local_data in task_tls_destruct() on task group exit
    
    Assisted-by: Claude:claude-opus-5.5
    Signed-off-by: v-zhangxiaomeng5 <[email protected]>
---
 include/nuttx/tls.h               | 9 +++++++++
 libs/libc/tls/task_tls_destruct.c | 9 +++++++++
 libs/libc/tls/tls_destruct.c      | 9 +++++++++
 3 files changed, 27 insertions(+)

diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h
index 6762c6ac766..7cf9c077c21 100644
--- a/include/nuttx/tls.h
+++ b/include/nuttx/tls.h
@@ -164,6 +164,10 @@ struct task_info_s
 #if defined(CONFIG_TLS_NELEM) && CONFIG_TLS_NELEM > 0
   tls_dtor_t      ta_tlsdtor[CONFIG_TLS_NELEM]; /* List of TLS destructors     
 */
 #endif
+#if defined(CONFIG_LIBCXX) && !defined(CONFIG_DISABLE_PTHREAD)
+  FAR void       *ta_thread_local_data;  /* C++ thread local  */
+  void          (*ta_thread_local_dtor)(FAR void *);
+#endif
 #ifndef CONFIG_BUILD_KERNEL
   struct getopt_s ta_getopt; /* Globals used by getopt() */
 #  ifdef CONFIG_LIBC_LOCALE
@@ -288,6 +292,11 @@ struct tls_info_s
 #if !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_PTHREAD_MUTEX_UNSAFE)
   FAR struct pthread_mutex_s *tl_mhead;   /* List of mutexes held by thread  */
 #endif
+
+#if defined(CONFIG_CXX_EXCEPTION) && !defined(CONFIG_SCHED_THREAD_LOCAL)
+  FAR void                   *tl_eh_globals;           /* __cxa_eh_globals */
+  void                      (*tl_eh_free)(FAR void *); /* Destructor       */
+#endif
 };
 
 /****************************************************************************
diff --git a/libs/libc/tls/task_tls_destruct.c 
b/libs/libc/tls/task_tls_destruct.c
index 1c14f2cfc23..367592cee45 100644
--- a/libs/libc/tls/task_tls_destruct.c
+++ b/libs/libc/tls/task_tls_destruct.c
@@ -143,6 +143,15 @@ void task_tls_destruct(void)
 
       info->ta_telem[candidate] = 0;
     }
+
+#if defined(CONFIG_LIBCXX) && !defined(CONFIG_DISABLE_PTHREAD)
+  if (info->ta_thread_local_data && info->ta_thread_local_dtor)
+    {
+      info->ta_thread_local_dtor(info->ta_thread_local_data);
+      info->ta_thread_local_data = NULL;
+      info->ta_thread_local_dtor = NULL;
+    }
+#endif
 }
 
 #endif
diff --git a/libs/libc/tls/tls_destruct.c b/libs/libc/tls/tls_destruct.c
index dc8815aa7bb..ab408c487ff 100644
--- a/libs/libc/tls/tls_destruct.c
+++ b/libs/libc/tls/tls_destruct.c
@@ -89,6 +89,15 @@ void tls_destruct(void)
     {
       tls->tl_elem[candidate] = 0;
     }
+
+#if defined(CONFIG_CXX_EXCEPTION) && !defined(CONFIG_SCHED_THREAD_LOCAL)
+  if (tls->tl_eh_globals && tls->tl_eh_free)
+    {
+      tls->tl_eh_free(tls->tl_eh_globals);
+      tls->tl_eh_globals = NULL;
+      tls->tl_eh_free = NULL;
+    }
+#endif
 }
 
 #endif /* CONFIG_TLS_NELEM > 0 */

Reply via email to