You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by pk...@apache.org on 2022/06/03 20:56:57 UTC

[incubator-nuttx] branch master updated (c19d37adf0 -> 01c8bebf58)

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

pkarashchenko pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


    from c19d37adf0 libc/wchar: Call mbsnrtowcs in mbrtowc to handle the partial sequence correctly
     new bd76e69f8d tls: Merge tls_xxx into pthread_keyxxx
     new 33a6aa2f48 sched/tls: Don't compensate the tls size to the stack size
     new 3e32b605fe libc/tls: Make tls_get_info as the pulibc function instead up_tls_info
     new 47b707bbf8 sched/tls: Add tls_init_info and tls_dup_info
     new 01c8bebf58 sched/tls: Add task_init_info and task_uninit_info

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 fs/procfs/fs_procfsproc.c                          |   2 +-
 include/nuttx/arch.h                               |   9 --
 include/nuttx/pthread.h                            |   1 +
 include/nuttx/sched.h                              |   6 +-
 include/nuttx/tls.h                                |  89 ++---------------
 libs/libc/errno/lib_errno.c                        |   3 +-
 libs/libc/pthread/pthread_cleanup.c                |   5 +-
 libs/libc/pthread/pthread_exit.c                   |   3 +-
 libs/libc/pthread/pthread_getspecific.c            |  23 ++++-
 libs/libc/pthread/pthread_keycreate.c              |  41 ++++++--
 libs/libc/pthread/pthread_keydelete.c              |  36 ++++++-
 libs/libc/pthread/pthread_setspecific.c            |  21 +++-
 libs/libc/tls/Kconfig                              |   4 +-
 libs/libc/tls/Make.defs                            |   8 +-
 libs/libc/tls/task_getinfo.c                       |   3 +-
 libs/libc/tls/{tls_setvalue.c => task_tls.c}       |  45 +++------
 libs/libc/tls/tls_alloc.c                          | 100 -------------------
 libs/libc/tls/tls_destruct.c                       |   3 +-
 libs/libc/tls/tls_free.c                           |  90 -----------------
 libs/libc/tls/tls_getinfo.c                        |   4 +-
 libs/libc/tls/tls_getvalue.c                       | 110 ---------------------
 sched/Makefile                                     |   1 +
 sched/group/group_create.c                         |  11 +--
 sched/group/group_leave.c                          |   4 +-
 sched/pthread/pthread_cancel.c                     |   2 +-
 sched/pthread/pthread_create.c                     |  20 +---
 sched/task/task_init.c                             |  18 +---
 sched/task/task_setup.c                            |   1 +
 sched/task/task_start.c                            |   1 +
 sched/task/task_vfork.c                            |  13 +--
 {drivers/i2s => sched/tls}/Make.defs               |  14 +--
 .../tls/task_initinfo.c                            |  41 ++++----
 .../lib_getgid.c => sched/tls/task_uninitinfo.c    |  25 ++---
 .../src/rx65n/rx65n_cmtw0.h => sched/tls/tls.h     |  69 ++++++-------
 .../arm_addrenv_perms.c => sched/tls/tls_dupinfo.c |  45 +++++----
 .../tls/tls_initinfo.c                             |  43 ++++----
 36 files changed, 297 insertions(+), 617 deletions(-)
 rename libs/libc/tls/{tls_setvalue.c => task_tls.c} (73%)
 delete mode 100644 libs/libc/tls/tls_alloc.c
 delete mode 100644 libs/libc/tls/tls_free.c
 delete mode 100644 libs/libc/tls/tls_getvalue.c
 copy {drivers/i2s => sched/tls}/Make.defs (85%)
 copy arch/arm/src/armv7-a/arm_addrenv_perms.c => sched/tls/task_initinfo.c (73%)
 copy libs/libc/unistd/lib_getgid.c => sched/tls/task_uninitinfo.c (79%)
 copy arch/renesas/src/rx65n/rx65n_cmtw0.h => sched/tls/tls.h (66%)
 copy arch/arm/src/armv7-a/arm_addrenv_perms.c => sched/tls/tls_dupinfo.c (69%)
 copy arch/arm/src/armv7-a/arm_addrenv_perms.c => sched/tls/tls_initinfo.c (72%)


[incubator-nuttx] 05/05: sched/tls: Add task_init_info and task_uninit_info

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 01c8bebf585102dad56b64491b3bf2b49f2675c4
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sat May 28 04:58:16 2022 +0800

    sched/tls: Add task_init_info and task_uninit_info
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 sched/group/group_create.c             | 11 ++-----
 sched/group/group_leave.c              |  4 +--
 sched/tls/Make.defs                    |  2 +-
 sched/tls/{tls.h => task_initinfo.c}   | 54 ++++++++++++++++++----------------
 sched/tls/{tls.h => task_uninitinfo.c} | 44 ++++++++++-----------------
 sched/tls/tls.h                        | 32 ++++++++++++++++++++
 6 files changed, 82 insertions(+), 65 deletions(-)

diff --git a/sched/group/group_create.c b/sched/group/group_create.c
index e5347ff541..319eadffe3 100644
--- a/sched/group/group_create.c
+++ b/sched/group/group_create.c
@@ -35,10 +35,10 @@
 #include <nuttx/lib/lib.h>
 #include <nuttx/semaphore.h>
 #include <nuttx/sched.h>
-#include <nuttx/tls.h>
 
 #include "sched/sched.h"
 #include "group/group.h"
+#include "tls/tls.h"
 
 /****************************************************************************
  * Pre-processor Definitions
@@ -185,9 +185,8 @@ int group_allocate(FAR struct task_tcb_s *tcb, uint8_t ttype)
 
   /* Alloc task info for group  */
 
-  group->tg_info = (FAR struct task_info_s *)
-    group_zalloc(group, sizeof(struct task_info_s));
-  if (!group->tg_info)
+  ret = task_init_info(group);
+  if (ret < 0)
     {
       goto errout_with_member;
     }
@@ -200,10 +199,6 @@ int group_allocate(FAR struct task_tcb_s *tcb, uint8_t ttype)
 
   group_inherit_identity(group);
 
-  /* Initial user space semaphore */
-
-  nxsem_init(&group->tg_info->ta_sem, 0, 1);
-
   /* Initialize file descriptors for the TCB */
 
   files_initlist(&group->tg_filelist);
diff --git a/sched/group/group_leave.c b/sched/group/group_leave.c
index 935b3cfc91..d4bda7d599 100644
--- a/sched/group/group_leave.c
+++ b/sched/group/group_leave.c
@@ -44,6 +44,7 @@
 #include "pthread/pthread.h"
 #include "mqueue/mqueue.h"
 #include "group/group.h"
+#include "tls/tls.h"
 
 /****************************************************************************
  * Private Functions
@@ -137,8 +138,7 @@ static inline void group_release(FAR struct task_group_s *group)
   task_tls_destruct();
 #endif
 
-  nxsem_destroy(&group->tg_info->ta_sem);
-  group_free(group, group->tg_info);
+  task_uninit_info(group);
 
 #if defined(CONFIG_SCHED_HAVE_PARENT) && defined(CONFIG_SCHED_CHILD_STATUS)
   /* Free all un-reaped child exit status */
diff --git a/sched/tls/Make.defs b/sched/tls/Make.defs
index 10f81dcdef..a5ea535f4d 100644
--- a/sched/tls/Make.defs
+++ b/sched/tls/Make.defs
@@ -18,7 +18,7 @@
 #
 ############################################################################
 
-CSRCS += tls_initinfo.c tls_dupinfo.c
+CSRCS += task_initinfo.c task_uninitinfo.c tls_initinfo.c tls_dupinfo.c
 
 # Include tls build support
 
diff --git a/sched/tls/tls.h b/sched/tls/task_initinfo.c
similarity index 66%
copy from sched/tls/tls.h
copy to sched/tls/task_initinfo.c
index af04963d11..4ccf1e9f7e 100644
--- a/sched/tls/tls.h
+++ b/sched/tls/task_initinfo.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/tls/tls.h
+ * sched/tls/task_initinfo.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -18,50 +18,52 @@
  *
  ****************************************************************************/
 
-#ifndef __SCHED_TLS_TLS_H
-#define __SCHED_TLS_TLS_H
-
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/sched.h>
+#include <errno.h>
+
+#include <nuttx/kmalloc.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/tls.h>
+
+#include "tls.h"
 
 /****************************************************************************
- * Public Function Prototypes
+ * Public Functions
  ****************************************************************************/
 
 /****************************************************************************
- * Name: tls_init_info
+ * Name: task_init_info
  *
  * Description:
- *   Allocate and initilize tls_info_s structure.
+ *   Allocate and initilize task_info_s structure.
  *
  * Input Parameters:
- *   - tcb: The TCB of new task
+ *   - group: The group of new task
  *
  * Returned Value:
  *   Zero (OK) on success; a negated errno value on failure.
  *
  ****************************************************************************/
 
-int tls_init_info(FAR struct tcb_s *tcb);
+int task_init_info(FAR struct task_group_s *group)
+{
+  FAR struct task_info_s *info;
 
-/****************************************************************************
- * Name: tls_dup_info
- *
- * Description:
- *   Allocate and duplicate tls_info_s structure.
- *
- * Input Parameters:
- *   - dst: The TCB of new task
- *   - src: The TCB of source task
- *
- * Returned Value:
- *   Zero (OK) on success; a negated errno value on failure.
- *
- ****************************************************************************/
+  /* Allocate task info for group */
+
+  info = group_zalloc(group, sizeof(struct task_info_s));
+  if (info == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  /* Initialize user space semaphore */
 
-int tls_dup_info(FAR struct tcb_s *dst, FAR struct tcb_s *src);
+  nxsem_init(&info->ta_sem, 0, 1);
+  group->tg_info = info;
 
-#endif /* __SCHED_TLS_TLS_H */
+  return OK;
+}
diff --git a/sched/tls/tls.h b/sched/tls/task_uninitinfo.c
similarity index 64%
copy from sched/tls/tls.h
copy to sched/tls/task_uninitinfo.c
index af04963d11..f4dca8903e 100644
--- a/sched/tls/tls.h
+++ b/sched/tls/task_uninitinfo.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/tls/tls.h
+ * sched/tls/task_uninitinfo.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -18,50 +18,38 @@
  *
  ****************************************************************************/
 
-#ifndef __SCHED_TLS_TLS_H
-#define __SCHED_TLS_TLS_H
-
 /****************************************************************************
  * Included Files
  ****************************************************************************/
 
-#include <nuttx/sched.h>
+#include <nuttx/kmalloc.h>
+#include <nuttx/semaphore.h>
+#include <nuttx/tls.h>
 
-/****************************************************************************
- * Public Function Prototypes
- ****************************************************************************/
+#include "tls.h"
 
 /****************************************************************************
- * Name: tls_init_info
- *
- * Description:
- *   Allocate and initilize tls_info_s structure.
- *
- * Input Parameters:
- *   - tcb: The TCB of new task
- *
- * Returned Value:
- *   Zero (OK) on success; a negated errno value on failure.
- *
+ * Public Functions
  ****************************************************************************/
 
-int tls_init_info(FAR struct tcb_s *tcb);
-
 /****************************************************************************
- * Name: tls_dup_info
+ * Name: task_uninit_info
  *
  * Description:
- *   Allocate and duplicate tls_info_s structure.
+ *   Uninitilize and free task_info_s structure.
  *
  * Input Parameters:
- *   - dst: The TCB of new task
- *   - src: The TCB of source task
+ *   - group: The group of new task
  *
  * Returned Value:
- *   Zero (OK) on success; a negated errno value on failure.
+ *   None.
  *
  ****************************************************************************/
 
-int tls_dup_info(FAR struct tcb_s *dst, FAR struct tcb_s *src);
+void task_uninit_info(FAR struct task_group_s *group)
+{
+  FAR struct task_info_s *info = group->tg_info;
 
-#endif /* __SCHED_TLS_TLS_H */
+  nxsem_destroy(&info->ta_sem);
+  group_free(group, info);
+}
diff --git a/sched/tls/tls.h b/sched/tls/tls.h
index af04963d11..e9adeacda0 100644
--- a/sched/tls/tls.h
+++ b/sched/tls/tls.h
@@ -31,6 +31,38 @@
  * Public Function Prototypes
  ****************************************************************************/
 
+/****************************************************************************
+ * Name: task_init_info
+ *
+ * Description:
+ *   Allocate and initilize task_info_s structure.
+ *
+ * Input Parameters:
+ *   - group: The group of new task
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int task_init_info(FAR struct task_group_s *group);
+
+/****************************************************************************
+ * Name: task_uninit_info
+ *
+ * Description:
+ *   Uninitilize and free task_info_s structure.
+ *
+ * Input Parameters:
+ *   - group: The group of new task
+ *
+ * Returned Value:
+ *   None.
+ *
+ ****************************************************************************/
+
+void task_uninit_info(FAR struct task_group_s *group);
+
 /****************************************************************************
  * Name: tls_init_info
  *


[incubator-nuttx] 03/05: libc/tls: Make tls_get_info as the pulibc function instead up_tls_info

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 3e32b605feb8142b30277fa5f45cdc8eb0e5e2c0
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Fri May 27 10:05:43 2022 +0800

    libc/tls: Make tls_get_info as the pulibc function instead up_tls_info
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 fs/procfs/fs_procfsproc.c               | 2 +-
 include/nuttx/arch.h                    | 9 ---------
 include/nuttx/pthread.h                 | 1 +
 include/nuttx/sched.h                   | 6 +++---
 include/nuttx/tls.h                     | 7 ++++++-
 libs/libc/errno/lib_errno.c             | 3 +--
 libs/libc/pthread/pthread_cleanup.c     | 5 ++---
 libs/libc/pthread/pthread_exit.c        | 3 +--
 libs/libc/pthread/pthread_getspecific.c | 3 +--
 libs/libc/pthread/pthread_setspecific.c | 3 +--
 libs/libc/tls/Kconfig                   | 4 ++--
 libs/libc/tls/task_getinfo.c            | 3 +--
 libs/libc/tls/tls_destruct.c            | 3 +--
 libs/libc/tls/tls_getinfo.c             | 4 ++--
 sched/pthread/pthread_cancel.c          | 2 +-
 sched/task/task_setup.c                 | 1 +
 sched/task/task_start.c                 | 1 +
 17 files changed, 26 insertions(+), 34 deletions(-)

diff --git a/fs/procfs/fs_procfsproc.c b/fs/procfs/fs_procfsproc.c
index 0d88d8e421..a01bfb48b4 100644
--- a/fs/procfs/fs_procfsproc.c
+++ b/fs/procfs/fs_procfsproc.c
@@ -44,7 +44,7 @@
 #endif
 
 #include <nuttx/irq.h>
-#include <nuttx/arch.h>
+#include <nuttx/tls.h>
 #include <nuttx/sched.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/environ.h>
diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h
index 23055d57e2..ca4962ba88 100644
--- a/include/nuttx/arch.h
+++ b/include/nuttx/arch.h
@@ -85,7 +85,6 @@
 #include <nuttx/compiler.h>
 #include <nuttx/cache.h>
 #include <nuttx/sched.h>
-#include <nuttx/tls.h>
 
 /****************************************************************************
  * Pre-processor definitions
@@ -1826,14 +1825,6 @@ int up_timer_start(FAR const struct timespec *ts);
  * implementation provided here assume the arch has a "push down" stack.
  */
 
-#ifndef up_tls_info
-#  if defined(CONFIG_TLS_ALIGNED) && !defined(__KERNEL__)
-#    define up_tls_info() TLS_INFO((uintptr_t)up_getsp())
-#  else
-#    define up_tls_info() tls_get_info()
-#  endif
-#endif
-
 /****************************************************************************
  * Name: up_tls_size
  *
diff --git a/include/nuttx/pthread.h b/include/nuttx/pthread.h
index a9ebf508aa..257db022ec 100644
--- a/include/nuttx/pthread.h
+++ b/include/nuttx/pthread.h
@@ -184,6 +184,7 @@ void nx_pthread_exit(FAR void *exit_value) noreturn_function;
  ****************************************************************************/
 
 #ifdef CONFIG_PTHREAD_CLEANUP
+struct tls_info_s;
 void pthread_cleanup_popall(FAR struct tls_info_s *tls);
 #endif
 
diff --git a/include/nuttx/sched.h b/include/nuttx/sched.h
index 9738724487..99bec382e8 100644
--- a/include/nuttx/sched.h
+++ b/include/nuttx/sched.h
@@ -33,18 +33,16 @@
 #include <sched.h>
 #include <signal.h>
 #include <semaphore.h>
+#include <pthread.h>
 #include <time.h>
 
 #include <nuttx/clock.h>
 #include <nuttx/irq.h>
-#include <nuttx/tls.h>
 #include <nuttx/wdog.h>
 #include <nuttx/mm/shm.h>
 #include <nuttx/fs/fs.h>
 #include <nuttx/net/net.h>
 
-#include <arch/arch.h>
-
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
@@ -393,6 +391,8 @@ struct stackinfo_s
  * the struct task_group_s is free.
  */
 
+struct task_info_s;
+
 #ifndef CONFIG_DISABLE_PTHREAD
 struct join_s;                      /* Forward reference                        */
                                     /* Defined in sched/pthread/pthread.h       */
diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h
index 846e7010e3..33d65ac445 100644
--- a/include/nuttx/tls.h
+++ b/include/nuttx/tls.h
@@ -27,6 +27,7 @@
 
 #include <nuttx/config.h>
 
+#include <nuttx/arch.h>
 #include <nuttx/atexit.h>
 
 #include <sys/types.h>
@@ -301,7 +302,11 @@ uintptr_t task_tls_get_value(int tlsindex);
  *
  ****************************************************************************/
 
-#if !defined(CONFIG_TLS_ALIGNED) || defined(__KERNEL__)
+#if defined(up_tls_info)
+#  define tls_get_info() up_tls_info()
+#elif defined(CONFIG_TLS_ALIGNED)
+#  define tls_get_info() TLS_INFO(up_getsp())
+#else
 FAR struct tls_info_s *tls_get_info(void);
 #endif
 
diff --git a/libs/libc/errno/lib_errno.c b/libs/libc/errno/lib_errno.c
index 87ecb0d5fe..684b2bff92 100644
--- a/libs/libc/errno/lib_errno.c
+++ b/libs/libc/errno/lib_errno.c
@@ -24,7 +24,6 @@
 
 #include <nuttx/config.h>
 
-#include <nuttx/arch.h>
 #include <nuttx/tls.h>
 
 /****************************************************************************
@@ -57,7 +56,7 @@ FAR int *__errno(void)
 {
   /* Get the TLS tls_info_s structure instance for this thread */
 
-  FAR struct tls_info_s *tlsinfo = up_tls_info();
+  FAR struct tls_info_s *tlsinfo = tls_get_info();
 
   /* And return the return refernce to the error number */
 
diff --git a/libs/libc/pthread/pthread_cleanup.c b/libs/libc/pthread/pthread_cleanup.c
index b8aca803ad..2df925e68d 100644
--- a/libs/libc/pthread/pthread_cleanup.c
+++ b/libs/libc/pthread/pthread_cleanup.c
@@ -28,7 +28,6 @@
 #include <sched.h>
 #include <assert.h>
 
-#include <nuttx/arch.h>
 #include <nuttx/sched.h>
 #include <nuttx/tls.h>
 #include <nuttx/pthread.h>
@@ -119,7 +118,7 @@ static void pthread_cleanup_pop_tls(FAR struct tls_info_s *tls, int execute)
 
 void pthread_cleanup_pop(int execute)
 {
-  FAR struct tls_info_s *tls = up_tls_info();
+  FAR struct tls_info_s *tls = tls_get_info();
 
   DEBUGASSERT(tls != NULL);
 
@@ -135,7 +134,7 @@ void pthread_cleanup_pop(int execute)
 
 void pthread_cleanup_push(pthread_cleanup_t routine, FAR void *arg)
 {
-  FAR struct tls_info_s *tls = up_tls_info();
+  FAR struct tls_info_s *tls = tls_get_info();
 
   DEBUGASSERT(tls != NULL);
   DEBUGASSERT(tls->tos < CONFIG_PTHREAD_CLEANUP_STACKSIZE);
diff --git a/libs/libc/pthread/pthread_exit.c b/libs/libc/pthread/pthread_exit.c
index c68ef6a7fa..0c0f0a214b 100644
--- a/libs/libc/pthread/pthread_exit.c
+++ b/libs/libc/pthread/pthread_exit.c
@@ -28,7 +28,6 @@
 #include <debug.h>
 #include <sched.h>
 
-#include <nuttx/arch.h>
 #include <nuttx/pthread.h>
 #include <nuttx/tls.h>
 
@@ -64,7 +63,7 @@ void pthread_exit(FAR void *exit_value)
 #endif
 
 #ifdef CONFIG_PTHREAD_CLEANUP
-  pthread_cleanup_popall(up_tls_info());
+  pthread_cleanup_popall(tls_get_info());
 #endif
 
 #if CONFIG_TLS_NELEM > 0
diff --git a/libs/libc/pthread/pthread_getspecific.c b/libs/libc/pthread/pthread_getspecific.c
index 15bec11e68..318ba01270 100644
--- a/libs/libc/pthread/pthread_getspecific.c
+++ b/libs/libc/pthread/pthread_getspecific.c
@@ -27,7 +27,6 @@
 #include <pthread.h>
 #include <assert.h>
 
-#include <nuttx/arch.h>
 #include <nuttx/tls.h>
 
 #if CONFIG_TLS_NELEM > 0
@@ -71,7 +70,7 @@ FAR void *pthread_getspecific(pthread_key_t key)
     {
       /* Get the TLS info structure from the current threads stack */
 
-      info = up_tls_info();
+      info = tls_get_info();
       DEBUGASSERT(info != NULL);
 
       /* Get the element value from the TLS info. */
diff --git a/libs/libc/pthread/pthread_setspecific.c b/libs/libc/pthread/pthread_setspecific.c
index c40d36a915..53c543a6da 100644
--- a/libs/libc/pthread/pthread_setspecific.c
+++ b/libs/libc/pthread/pthread_setspecific.c
@@ -27,7 +27,6 @@
 #include <pthread.h>
 #include <assert.h>
 
-#include <nuttx/arch.h>
 #include <nuttx/tls.h>
 
 #if CONFIG_TLS_NELEM > 0
@@ -81,7 +80,7 @@ int pthread_setspecific(pthread_key_t key, FAR const void *value)
     {
       /* Get the TLS info structure from the current threads stack */
 
-      info = up_tls_info();
+      info = tls_get_info();
       DEBUGASSERT(info != NULL);
 
       /* Set the element value int the TLS info. */
diff --git a/libs/libc/tls/Kconfig b/libs/libc/tls/Kconfig
index 171605d022..511a3fb2e9 100644
--- a/libs/libc/tls/Kconfig
+++ b/libs/libc/tls/Kconfig
@@ -63,7 +63,7 @@ config TLS_TASK_NELEM
 		The number of unique Task Local Storage elements similar with
 		Thread Local Storage.
 		These can be accessed with task_tls_alloc/task_tls_get_value/task_tls_set_value.
-  		NOTE that the 0 value of CONFIG_SCHED_TLS_NELEM disables these
-  		TLS interfaces.
+		NOTE that the 0 value of CONFIG_SCHED_TLS_NELEM disables these
+		TLS interfaces.
 
 endmenu # Thread Local Storage (TLS)
diff --git a/libs/libc/tls/task_getinfo.c b/libs/libc/tls/task_getinfo.c
index 34800f33f3..4bfad05c6e 100644
--- a/libs/libc/tls/task_getinfo.c
+++ b/libs/libc/tls/task_getinfo.c
@@ -24,7 +24,6 @@
 
 #include <nuttx/config.h>
 
-#include <nuttx/arch.h>
 #include <nuttx/tls.h>
 
 /****************************************************************************
@@ -48,7 +47,7 @@
 
 FAR struct task_info_s *task_get_info(void)
 {
-  FAR struct tls_info_s *info = up_tls_info();
+  FAR struct tls_info_s *info = tls_get_info();
 
   return info->tl_task;
 }
diff --git a/libs/libc/tls/tls_destruct.c b/libs/libc/tls/tls_destruct.c
index dfbfff53d5..b0ffc7bb28 100644
--- a/libs/libc/tls/tls_destruct.c
+++ b/libs/libc/tls/tls_destruct.c
@@ -26,7 +26,6 @@
 
 #include <assert.h>
 
-#include <nuttx/arch.h>
 #include <nuttx/tls.h>
 
 #if CONFIG_TLS_NELEM > 0
@@ -52,7 +51,7 @@
 void tls_destruct(void)
 {
   FAR struct task_info_s *info = task_get_info();
-  FAR struct tls_info_s *tls = up_tls_info();
+  FAR struct tls_info_s *tls = tls_get_info();
   FAR void *tls_elem_ptr = NULL;
   tls_dtor_t destructor;
   tls_ndxset_t tlsset;
diff --git a/libs/libc/tls/tls_getinfo.c b/libs/libc/tls/tls_getinfo.c
index be111ccfcf..01804fefc4 100644
--- a/libs/libc/tls/tls_getinfo.c
+++ b/libs/libc/tls/tls_getinfo.c
@@ -30,7 +30,7 @@
 #include <nuttx/arch.h>
 #include <nuttx/tls.h>
 
-#if !defined(CONFIG_TLS_ALIGNED) || defined(__KERNEL__)
+#if !defined(up_tls_info) && !defined(CONFIG_TLS_ALIGNED)
 
 /****************************************************************************
  * Public Functions
@@ -72,4 +72,4 @@ FAR struct tls_info_s *tls_get_info(void)
   return info;
 }
 
-#endif /* !CONFIG_TLS_ALIGNED || __KERNEL__ */
+#endif /* !defined(up_tls_info) && !defined(CONFIG_TLS_ALIGNED) */
diff --git a/sched/pthread/pthread_cancel.c b/sched/pthread/pthread_cancel.c
index e80fe647b4..368240b7c3 100644
--- a/sched/pthread/pthread_cancel.c
+++ b/sched/pthread/pthread_cancel.c
@@ -89,7 +89,7 @@ int pthread_cancel(pthread_t thread)
       pthread_exit(PTHREAD_CANCELED);
     }
 
-  /* Refer to up_tls_info() */
+  /* Refer to tls_get_info() */
 
 #ifdef CONFIG_PTHREAD_CLEANUP
   pthread_cleanup_popall(tcb->stack_alloc_ptr);
diff --git a/sched/task/task_setup.c b/sched/task/task_setup.c
index 36adfbb364..45965e879b 100644
--- a/sched/task/task_setup.c
+++ b/sched/task/task_setup.c
@@ -35,6 +35,7 @@
 #include <nuttx/arch.h>
 #include <nuttx/sched.h>
 #include <nuttx/signal.h>
+#include <nuttx/tls.h>
 
 #include "sched/sched.h"
 #include "pthread/pthread.h"
diff --git a/sched/task/task_start.c b/sched/task/task_start.c
index 11af95dcd2..745531ab9b 100644
--- a/sched/task/task_start.c
+++ b/sched/task/task_start.c
@@ -32,6 +32,7 @@
 
 #include <nuttx/arch.h>
 #include <nuttx/sched.h>
+#include <nuttx/tls.h>
 
 #include "group/group.h"
 #include "sched/sched.h"


[incubator-nuttx] 04/05: sched/tls: Add tls_init_info and tls_dup_info

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 47b707bbf8e8016e661ec943708416ea06e3ad89
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sat May 28 00:43:49 2022 +0800

    sched/tls: Add tls_init_info and tls_dup_info
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 sched/Makefile                 |  1 +
 sched/pthread/pthread_create.c | 17 +++-------
 sched/task/task_init.c         | 14 ++------
 sched/task/task_vfork.c        | 13 ++------
 sched/tls/Make.defs            | 26 +++++++++++++++
 sched/tls/tls.h                | 67 +++++++++++++++++++++++++++++++++++++
 sched/tls/tls_dupinfo.c        | 75 ++++++++++++++++++++++++++++++++++++++++++
 sched/tls/tls_initinfo.c       | 73 ++++++++++++++++++++++++++++++++++++++++
 8 files changed, 252 insertions(+), 34 deletions(-)

diff --git a/sched/Makefile b/sched/Makefile
index 9f7c7ca477..f79387de7f 100644
--- a/sched/Makefile
+++ b/sched/Makefile
@@ -34,6 +34,7 @@ include semaphore/Make.defs
 include signal/Make.defs
 include task/Make.defs
 include timer/Make.defs
+include tls/Make.defs
 include wdog/Make.defs
 include wqueue/Make.defs
 
diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c
index 9bbabb6e97..66bde29ce9 100644
--- a/sched/pthread/pthread_create.c
+++ b/sched/pthread/pthread_create.c
@@ -40,12 +40,12 @@
 #include <nuttx/semaphore.h>
 #include <nuttx/kmalloc.h>
 #include <nuttx/pthread.h>
-#include <nuttx/tls.h>
 
 #include "sched/sched.h"
 #include "group/group.h"
 #include "clock/clock.h"
 #include "pthread/pthread.h"
+#include "tls/tls.h"
 
 /****************************************************************************
  * Public Data
@@ -215,7 +215,6 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
                       pthread_startroutine_t entry, pthread_addr_t arg)
 {
   FAR struct pthread_tcb_s *ptcb;
-  FAR struct tls_info_s *info;
   FAR struct join_s *pjoin;
   struct sched_param param;
   int policy;
@@ -303,21 +302,13 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
 
   /* Initialize thread local storage */
 
-  info = up_stack_frame(&ptcb->cmn, up_tls_size());
-  if (info == NULL)
+  ret = tls_init_info(&ptcb->cmn);
+  if (ret != OK)
     {
-      errcode = ENOMEM;
+      errcode = -ret;
       goto errout_with_join;
     }
 
-  DEBUGASSERT(info == ptcb->cmn.stack_alloc_ptr);
-
-  up_tls_initialize(info);
-
-  /* Attach per-task info in group to TLS */
-
-  info->tl_task = ptcb->cmn.group->tg_info;
-
   /* Should we use the priority and scheduler specified in the pthread
    * attributes?  Or should we use the current thread's priority and
    * scheduler?
diff --git a/sched/task/task_init.c b/sched/task/task_init.c
index 270b858893..52bbdde774 100644
--- a/sched/task/task_init.c
+++ b/sched/task/task_init.c
@@ -33,12 +33,12 @@
 
 #include <nuttx/arch.h>
 #include <nuttx/sched.h>
-#include <nuttx/tls.h>
 
 #include "sched/sched.h"
 #include "environ/environ.h"
 #include "group/group.h"
 #include "task/task.h"
+#include "tls/tls.h"
 
 /****************************************************************************
  * Public Functions
@@ -88,7 +88,6 @@ int nxtask_init(FAR struct task_tcb_s *tcb, const char *name, int priority,
                 FAR char * const envp[])
 {
   uint8_t ttype = tcb->cmn.flags & TCB_FLAG_TTYPE_MASK;
-  FAR struct tls_info_s *info;
   int ret;
 
 #ifndef CONFIG_DISABLE_PTHREAD
@@ -141,19 +140,12 @@ int nxtask_init(FAR struct task_tcb_s *tcb, const char *name, int priority,
 
   /* Initialize thread local storage */
 
-  info = up_stack_frame(&tcb->cmn, up_tls_size());
-  if (info == NULL)
+  ret = tls_init_info(&tcb->cmn);
+  if (ret < OK)
     {
-      ret = -ENOMEM;
       goto errout_with_group;
     }
 
-  DEBUGASSERT(info == tcb->cmn.stack_alloc_ptr);
-
-  info->tl_task = tcb->cmn.group->tg_info;
-
-  up_tls_initialize(info);
-
   /* Initialize the task control block */
 
   ret = nxtask_setup_scheduler(tcb, priority, nxtask_start,
diff --git a/sched/task/task_vfork.c b/sched/task/task_vfork.c
index f535614c62..a56f8c195d 100644
--- a/sched/task/task_vfork.c
+++ b/sched/task/task_vfork.c
@@ -40,6 +40,7 @@
 #include "environ/environ.h"
 #include "group/group.h"
 #include "task/task.h"
+#include "tls/tls.h"
 
 /* vfork() requires architecture-specific support as well as waipid(). */
 
@@ -97,7 +98,6 @@ FAR struct task_tcb_s *nxtask_setup_vfork(start_t retaddr)
   FAR struct tcb_s *ptcb = this_task();
   FAR struct tcb_s *parent;
   FAR struct task_tcb_s *child;
-  FAR struct tls_info_s *info;
   size_t stack_size;
   uint8_t ttype;
   int priority;
@@ -181,19 +181,12 @@ FAR struct task_tcb_s *nxtask_setup_vfork(start_t retaddr)
 
   /* Setup thread local storage */
 
-  info = up_stack_frame(&child->cmn, up_tls_size());
-  if (info == NULL)
+  ret = tls_dup_info(&child->cmn, parent);
+  if (ret < OK)
     {
-      ret = -ENOMEM;
       goto errout_with_tcb;
     }
 
-  DEBUGASSERT(info == child->cmn.stack_alloc_ptr);
-  memcpy(info, parent->stack_alloc_ptr, sizeof(struct tls_info_s));
-  info->tl_task = child->cmn.group->tg_info;
-
-  up_tls_initialize(info);
-
   /* Get the priority of the parent task */
 
 #ifdef CONFIG_PRIORITY_INHERITANCE
diff --git a/sched/tls/Make.defs b/sched/tls/Make.defs
new file mode 100644
index 0000000000..10f81dcdef
--- /dev/null
+++ b/sched/tls/Make.defs
@@ -0,0 +1,26 @@
+############################################################################
+# sched/tls/Make.defs
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+############################################################################
+
+CSRCS += tls_initinfo.c tls_dupinfo.c
+
+# Include tls build support
+
+DEPPATH += --dep-path tls
+VPATH += :tls
diff --git a/sched/tls/tls.h b/sched/tls/tls.h
new file mode 100644
index 0000000000..af04963d11
--- /dev/null
+++ b/sched/tls/tls.h
@@ -0,0 +1,67 @@
+/****************************************************************************
+ * sched/tls/tls.h
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+#ifndef __SCHED_TLS_TLS_H
+#define __SCHED_TLS_TLS_H
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/sched.h>
+
+/****************************************************************************
+ * Public Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tls_init_info
+ *
+ * Description:
+ *   Allocate and initilize tls_info_s structure.
+ *
+ * Input Parameters:
+ *   - tcb: The TCB of new task
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int tls_init_info(FAR struct tcb_s *tcb);
+
+/****************************************************************************
+ * Name: tls_dup_info
+ *
+ * Description:
+ *   Allocate and duplicate tls_info_s structure.
+ *
+ * Input Parameters:
+ *   - dst: The TCB of new task
+ *   - src: The TCB of source task
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int tls_dup_info(FAR struct tcb_s *dst, FAR struct tcb_s *src);
+
+#endif /* __SCHED_TLS_TLS_H */
diff --git a/sched/tls/tls_dupinfo.c b/sched/tls/tls_dupinfo.c
new file mode 100644
index 0000000000..a22efd3d0c
--- /dev/null
+++ b/sched/tls/tls_dupinfo.c
@@ -0,0 +1,75 @@
+/****************************************************************************
+ * sched/tls/tls_dupinfo.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <assert.h>
+#include <errno.h>
+#include <string.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/tls.h>
+
+#include "tls.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tls_dup_info
+ *
+ * Description:
+ *   Allocate and duplicate tls_info_s structure.
+ *
+ * Input Parameters:
+ *   - dst: The TCB of new task
+ *   - src: The TCB of source task
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int tls_dup_info(FAR struct tcb_s *dst, FAR struct tcb_s *src)
+{
+  FAR struct tls_info_s *info;
+
+  /* Allocate thread local storage */
+
+  info = up_stack_frame(dst, up_tls_size());
+  if (info == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  DEBUGASSERT(info == dst->stack_alloc_ptr);
+
+  /* Copy thread local storage */
+
+  memcpy(info, src->stack_alloc_ptr, sizeof(struct tls_info_s));
+
+  /* Attach per-task info in group to TLS */
+
+  info->tl_task = dst->group->tg_info;
+  return OK;
+}
diff --git a/sched/tls/tls_initinfo.c b/sched/tls/tls_initinfo.c
new file mode 100644
index 0000000000..0b7e08ab99
--- /dev/null
+++ b/sched/tls/tls_initinfo.c
@@ -0,0 +1,73 @@
+/****************************************************************************
+ * sched/tls/tls_initinfo.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <assert.h>
+#include <errno.h>
+
+#include <nuttx/arch.h>
+#include <nuttx/tls.h>
+
+#include "tls.h"
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tls_init_info
+ *
+ * Description:
+ *   Allocate and initilize tls_info_s structure.
+ *
+ * Input Parameters:
+ *   - tcb: The TCB of new task
+ *
+ * Returned Value:
+ *   Zero (OK) on success; a negated errno value on failure.
+ *
+ ****************************************************************************/
+
+int tls_init_info(FAR struct tcb_s *tcb)
+{
+  FAR struct tls_info_s *info;
+
+  /* Allocate thread local storage */
+
+  info = up_stack_frame(tcb, up_tls_size());
+  if (info == NULL)
+    {
+      return -ENOMEM;
+    }
+
+  DEBUGASSERT(info == tcb->stack_alloc_ptr);
+
+  /* Initialize thread local storage */
+
+  up_tls_initialize(info);
+
+  /* Attach per-task info in group to TLS */
+
+  info->tl_task = tcb->group->tg_info;
+  return OK;
+}


[incubator-nuttx] 02/05: sched/tls: Don't compensate the tls size to the stack size

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 33a6aa2f48a9ade5398f28d830ab435c41a304dd
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun May 29 05:38:37 2022 +0800

    sched/tls: Don't compensate the tls size to the stack size
    
    it isn't good to change the stack size passed by caller
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 sched/pthread/pthread_create.c | 3 +--
 sched/task/task_init.c         | 4 +---
 2 files changed, 2 insertions(+), 5 deletions(-)

diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c
index c0fd3dd5aa..9bbabb6e97 100644
--- a/sched/pthread/pthread_create.c
+++ b/sched/pthread/pthread_create.c
@@ -291,8 +291,7 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
     {
       /* Allocate the stack for the TCB */
 
-      ret = up_create_stack((FAR struct tcb_s *)ptcb,
-                            up_tls_size() + attr->stacksize,
+      ret = up_create_stack((FAR struct tcb_s *)ptcb, attr->stacksize,
                             TCB_FLAG_TTYPE_PTHREAD);
     }
 
diff --git a/sched/task/task_init.c b/sched/task/task_init.c
index 5cbb774fba..270b858893 100644
--- a/sched/task/task_init.c
+++ b/sched/task/task_init.c
@@ -131,9 +131,7 @@ int nxtask_init(FAR struct task_tcb_s *tcb, const char *name, int priority,
     {
       /* Allocate the stack for the TCB */
 
-      ret = up_create_stack(&tcb->cmn,
-                            up_tls_size() + stack_size,
-                            ttype);
+      ret = up_create_stack(&tcb->cmn, stack_size, ttype);
     }
 
   if (ret < OK)


[incubator-nuttx] 01/05: tls: Merge tls_xxx into pthread_keyxxx

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit bd76e69f8df302b07cf5feee6bdbef0bf1b0091e
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Fri May 27 04:55:42 2022 +0800

    tls: Merge tls_xxx into pthread_keyxxx
    
    it's always better to provide the standard api directly
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 include/nuttx/tls.h                          |  82 --------------------
 libs/libc/pthread/pthread_getspecific.c      |  24 +++++-
 libs/libc/pthread/pthread_keycreate.c        |  41 ++++++++--
 libs/libc/pthread/pthread_keydelete.c        |  36 ++++++++-
 libs/libc/pthread/pthread_setspecific.c      |  22 +++++-
 libs/libc/tls/Make.defs                      |   8 +-
 libs/libc/tls/{tls_setvalue.c => task_tls.c} |  45 ++++-------
 libs/libc/tls/tls_alloc.c                    | 100 ------------------------
 libs/libc/tls/tls_free.c                     |  90 ----------------------
 libs/libc/tls/tls_getvalue.c                 | 110 ---------------------------
 10 files changed, 127 insertions(+), 431 deletions(-)

diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h
index 27e031e3e1..846e7010e3 100644
--- a/include/nuttx/tls.h
+++ b/include/nuttx/tls.h
@@ -209,88 +209,6 @@ struct tls_info_s
  * Public Function Prototypes
  ****************************************************************************/
 
-/****************************************************************************
- * Name: tls_alloc
- *
- * Description:
- *   Allocate a group-unique TLS data index
- *
- * Input Parameters:
- *   dtor     - The destructor of TLS data element
- *
- * Returned Value:
- *   A TLS index that is unique for use within this task group.
- *
- ****************************************************************************/
-
-#if CONFIG_TLS_NELEM > 0
-int tls_alloc(CODE void (*dtor)(FAR void *));
-#endif
-
-/****************************************************************************
- * Name: tls_free
- *
- * Description:
- *   Release a group-unique TLS data index previous obtained by tls_alloc()
- *
- * Input Parameters:
- *   tlsindex - The previously allocated TLS index to be freed
- *
- * Returned Value:
- *   OK is returned on success; a negated errno value will be returned on
- *   failure:
- *
- *     -EINVAL - the index to be freed is out of range.
- *
- ****************************************************************************/
-
-#if CONFIG_TLS_NELEM > 0
-int tls_free(int tlsindex);
-#endif
-
-/****************************************************************************
- * Name: tls_get_value
- *
- * Description:
- *   Return an the TLS data value associated with the 'tlsindx'
- *
- * Input Parameters:
- *   tlsindex - Index of TLS data element to return
- *
- * Returned Value:
- *   The value of TLS element associated with 'tlsindex'. Errors are not
- *   reported.  Zero is returned in the event of an error, but zero may also
- *   be valid value and returned when there is no error.  The only possible
- *   error would be if tlsindex < 0 or tlsindex >=CONFIG_TLS_NELEM.
- *
- ****************************************************************************/
-
-#if CONFIG_TLS_NELEM > 0
-uintptr_t tls_get_value(int tlsindex);
-#endif
-
-/****************************************************************************
- * Name: tls_set_value
- *
- * Description:
- *   Set the TLS element associated with the 'tlsindex' to 'tlsvalue'
- *
- * Input Parameters:
- *   tlsindex - Index of TLS data element to set
- *   tlsvalue - The new value of the TLS data element
- *
- * Returned Value:
- *   Zero is returned on success, a negated errno value is return on
- *   failure:
- *
- *     EINVAL - tlsindex is not in range.
- *
- ****************************************************************************/
-
-#if CONFIG_TLS_NELEM > 0
-int tls_set_value(int tlsindex, uintptr_t tlsvalue);
-#endif
-
 #if CONFIG_TLS_TASK_NELEM > 0
 
 /****************************************************************************
diff --git a/libs/libc/pthread/pthread_getspecific.c b/libs/libc/pthread/pthread_getspecific.c
index def22b9129..15bec11e68 100644
--- a/libs/libc/pthread/pthread_getspecific.c
+++ b/libs/libc/pthread/pthread_getspecific.c
@@ -25,9 +25,13 @@
 #include <nuttx/config.h>
 
 #include <pthread.h>
+#include <assert.h>
 
+#include <nuttx/arch.h>
 #include <nuttx/tls.h>
 
+#if CONFIG_TLS_NELEM > 0
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -59,5 +63,23 @@
 
 FAR void *pthread_getspecific(pthread_key_t key)
 {
-  return (FAR void *)tls_get_value((int)key);
+  FAR struct tls_info_s *info;
+  FAR void *ret = NULL;
+
+  DEBUGASSERT(key >= 0 && key < CONFIG_TLS_NELEM);
+  if (key >= 0 && key < CONFIG_TLS_NELEM)
+    {
+      /* Get the TLS info structure from the current threads stack */
+
+      info = up_tls_info();
+      DEBUGASSERT(info != NULL);
+
+      /* Get the element value from the TLS info. */
+
+      ret = (FAR void *)info->tl_elem[key];
+    }
+
+  return ret;
 }
+
+#endif /* CONFIG_TLS_NELEM */
diff --git a/libs/libc/pthread/pthread_keycreate.c b/libs/libc/pthread/pthread_keycreate.c
index fb9e458b8d..478dedec1f 100644
--- a/libs/libc/pthread/pthread_keycreate.c
+++ b/libs/libc/pthread/pthread_keycreate.c
@@ -26,7 +26,9 @@
 
 #include <pthread.h>
 #include <assert.h>
+#include <errno.h>
 
+#include <nuttx/semaphore.h>
 #include <nuttx/tls.h>
 
 #if CONFIG_TLS_NELEM > 0
@@ -70,23 +72,46 @@
 int pthread_key_create(FAR pthread_key_t *key,
                        CODE void (*destructor)(FAR void *))
 {
-  int tlsindex;
+  FAR struct task_info_s *info = task_get_info();
+  int candidate;
+  int ret;
 
   DEBUGASSERT(key != NULL);
+  DEBUGASSERT(info != NULL);
 
-  /* Allocate a TLS index */
+  /* Search for an unused index.  This is done in a critical section here to
+   * avoid concurrent modification of the group TLS index set.
+   */
 
-  tlsindex = tls_alloc(destructor);
+  ret = _SEM_WAIT(&info->ta_sem);
 
-  /* Check if found a TLS index. */
+  if (ret < 0)
+    {
+      ret = _SEM_ERRNO(ret);
+      return ret;
+    }
+
+  ret = EAGAIN;
 
-  if (tlsindex >= 0)
+  for (candidate = 0; candidate < CONFIG_TLS_NELEM; candidate++)
     {
-      *key = tlsindex;
-      return OK;
+      /* Is this candidate index available? */
+
+      tls_ndxset_t mask = (tls_ndxset_t)1 << candidate;
+      if ((info->ta_tlsset & mask) == 0)
+        {
+          /* Yes.. allocate the index and break out of the loop */
+
+          info->ta_tlsset |= mask;
+          info->ta_tlsdtor[candidate] = destructor;
+          *key = candidate;
+          ret = OK;
+          break;
+        }
     }
 
-  return -tlsindex;
+  _SEM_POST(&info->ta_sem);
+  return ret;
 }
 
 #endif /* CONFIG_TLS_NELEM */
diff --git a/libs/libc/pthread/pthread_keydelete.c b/libs/libc/pthread/pthread_keydelete.c
index ad5ab06af4..51693a4337 100644
--- a/libs/libc/pthread/pthread_keydelete.c
+++ b/libs/libc/pthread/pthread_keydelete.c
@@ -25,9 +25,14 @@
 #include <nuttx/config.h>
 
 #include <pthread.h>
+#include <assert.h>
+#include <errno.h>
 
+#include <nuttx/semaphore.h>
 #include <nuttx/tls.h>
 
+#if CONFIG_TLS_NELEM > 0
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -52,8 +57,33 @@
 
 int pthread_key_delete(pthread_key_t key)
 {
-  /* Free the TLS index */
+  FAR struct task_info_s *info = task_get_info();
+  tls_ndxset_t mask;
+  int ret = EINVAL;
+
+  DEBUGASSERT(info != NULL);
+  DEBUGASSERT(key >= 0 && key < CONFIG_TLS_NELEM);
+  if (key >= 0 && key < CONFIG_TLS_NELEM)
+    {
+      /* This is done while holding a semaphore here to avoid concurrent
+       * modification of the group TLS index set.
+       */
 
-  int ret = tls_free((int)key);
-  return ret < 0 ? -ret : 0;
+      mask = (tls_ndxset_t)1 << key;
+      ret = _SEM_WAIT(&info->ta_sem);
+      if (ret == OK)
+        {
+          DEBUGASSERT((info->ta_tlsset & mask) != 0);
+          info->ta_tlsset &= ~mask;
+          _SEM_POST(&info->ta_sem);
+        }
+      else
+        {
+          ret = _SEM_ERRNO(ret);
+        }
+    }
+
+  return ret;
 }
+
+#endif /* CONFIG_TLS_NELEM */
diff --git a/libs/libc/pthread/pthread_setspecific.c b/libs/libc/pthread/pthread_setspecific.c
index 40ed44270f..c40d36a915 100644
--- a/libs/libc/pthread/pthread_setspecific.c
+++ b/libs/libc/pthread/pthread_setspecific.c
@@ -25,7 +25,9 @@
 #include <nuttx/config.h>
 
 #include <pthread.h>
+#include <assert.h>
 
+#include <nuttx/arch.h>
 #include <nuttx/tls.h>
 
 #if CONFIG_TLS_NELEM > 0
@@ -72,9 +74,23 @@
 
 int pthread_setspecific(pthread_key_t key, FAR const void *value)
 {
-  int ret = tls_set_value((int)key, (uintptr_t)value);
-  return ret < 0 ? -ret : 0;
+  FAR struct tls_info_s *info;
+
+  DEBUGASSERT(key >= 0 && key < CONFIG_TLS_NELEM);
+  if (key >= 0 && key < CONFIG_TLS_NELEM)
+    {
+      /* Get the TLS info structure from the current threads stack */
+
+      info = up_tls_info();
+      DEBUGASSERT(info != NULL);
+
+      /* Set the element value int the TLS info. */
+
+      info->tl_elem[key] = (uintptr_t)value;
+      return OK;
+    }
+
+  return EINVAL;
 }
 
 #endif /* CONFIG_TLS_NELEM */
-
diff --git a/libs/libc/tls/Make.defs b/libs/libc/tls/Make.defs
index 5cb0a7e54f..f1e929b8e8 100644
--- a/libs/libc/tls/Make.defs
+++ b/libs/libc/tls/Make.defs
@@ -20,13 +20,15 @@
 
 CSRCS += task_getinfo.c tls_getinfo.c
 
+ifneq ($(CONFIG_TLS_TASK_NELEM),0)
+CSRCS += task_tls.c
+endif
+
 ifneq ($(CONFIG_TLS_NELEM),0)
-CSRCS += tls_alloc.c tls_free.c
-CSRCS += tls_setvalue.c tls_getvalue.c tls_destruct.c
+CSRCS += tls_destruct.c
 endif
 
 # Include tls build support
 
 DEPPATH += --dep-path tls
 VPATH += :tls
-
diff --git a/libs/libc/tls/tls_setvalue.c b/libs/libc/tls/task_tls.c
similarity index 73%
rename from libs/libc/tls/tls_setvalue.c
rename to libs/libc/tls/task_tls.c
index 6cf775df15..9c115eef55 100644
--- a/libs/libc/tls/tls_setvalue.c
+++ b/libs/libc/tls/task_tls.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * libs/libc/tls/tls_setvalue.c
+ * libs/libc/tls/task_tls.c
  *
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -24,61 +24,44 @@
 
 #include <nuttx/config.h>
 
-#include <assert.h>
 #include <errno.h>
-
-#include <nuttx/arch.h>
 #include <nuttx/tls.h>
 
-#if CONFIG_TLS_NELEM > 0
+#if CONFIG_TLS_TASK_NELEM > 0
 
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
 
 /****************************************************************************
- * Name: tls_set_value
+ * Name: task_tls_get_value
  *
  * Description:
- *   Set the TLS element associated with the 'tlsindex' to 'tlsvalue'
+ *   Return an the task local storage data value associated with 'tlsindx'
  *
  * Input Parameters:
- *   tlsindex - Index of TLS data element to set
- *   tlsvalue - The new value of the TLS data element
+ *   tlsindex - Index of task local storage data element to return
  *
  * Returned Value:
- *   Zero is returned on success, a negated errno value is return on
- *   failure:
- *
- *     EINVAL - tlsindex is not in range.
+ *   The value of TLS element associated with 'tlsindex'. Errors are not
+ *   reported.  Zero is returned in the event of an error, but zero may also
+ *   be valid value and returned when there is no error.  The only possible
+ *   error would be if tlsindex < 0 or tlsindex >=CONFIG_TLS_TASK_NELEM.
  *
  ****************************************************************************/
 
-int tls_set_value(int tlsindex, uintptr_t tlsvalue)
+uintptr_t task_tls_get_value(int tlsindex)
 {
-  FAR struct tls_info_s *info;
+  FAR struct task_info_s *info = task_get_info();
 
-  DEBUGASSERT(tlsindex >= 0 && tlsindex < CONFIG_TLS_NELEM);
-  if (tlsindex >= 0 && tlsindex < CONFIG_TLS_NELEM)
+  if (tlsindex >= 0 && tlsindex < CONFIG_TLS_TASK_NELEM)
     {
-      /* Get the TLS info structure from the current threads stack */
-
-      info = up_tls_info();
-      DEBUGASSERT(info != NULL);
-
-      /* Set the element value int the TLS info. */
-
-      info->tl_elem[tlsindex] = tlsvalue;
-      return OK;
+      return info->ta_telem[tlsindex];
     }
 
-  return -EINVAL;
+  return 0;
 }
 
-#endif /* CONFIG_TLS_NELEM > 0 */
-
-#if CONFIG_TLS_TASK_NELEM > 0
-
 /****************************************************************************
  * Name: task_tls_set_value
  *
diff --git a/libs/libc/tls/tls_alloc.c b/libs/libc/tls/tls_alloc.c
deleted file mode 100644
index 46860d0ece..0000000000
--- a/libs/libc/tls/tls_alloc.c
+++ /dev/null
@@ -1,100 +0,0 @@
-/****************************************************************************
- * libs/libc/tls/tls_alloc.c
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.  The
- * ASF licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- ****************************************************************************/
-
-/****************************************************************************
- * Included Files
- ****************************************************************************/
-
-#include <nuttx/config.h>
-
-#include <sched.h>
-#include <errno.h>
-#include <assert.h>
-#include <debug.h>
-
-#include <nuttx/spinlock.h>
-#include <nuttx/tls.h>
-#include <nuttx/sched.h>
-
-#if CONFIG_TLS_NELEM > 0
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: tls_alloc
- *
- * Description:
- *   Allocate a group-unique TLS data index
- *
- * Input Parameters:
- *   None
- *
- * Returned Value:
- *   A TLS index that is unique for use within this task group.
- *   If unsuccessful, an errno value will be returned and set to errno.
- *
- ****************************************************************************/
-
-int tls_alloc(CODE void (*dtor)(FAR void *))
-{
-  FAR struct task_info_s *info = task_get_info();
-  int candidate;
-  int ret;
-
-  DEBUGASSERT(info);
-
-  /* Search for an unused index.  This is done in a critical section here to
-   * avoid concurrent modification of the group TLS index set.
-   */
-
-  ret = _SEM_WAIT(&info->ta_sem);
-
-  if (ret < 0)
-    {
-      ret = _SEM_ERRVAL(ret);
-      return ret;
-    }
-
-  ret = -EAGAIN;
-
-  for (candidate = 0; candidate < CONFIG_TLS_NELEM; candidate++)
-    {
-      /* Is this candidate index available? */
-
-      tls_ndxset_t mask = (tls_ndxset_t)1 << candidate;
-      if ((info->ta_tlsset & mask) == 0)
-        {
-          /* Yes.. allocate the index and break out of the loop */
-
-          info->ta_tlsset |= mask;
-          info->ta_tlsdtor[candidate] = dtor;
-          ret = candidate;
-          break;
-        }
-    }
-
-  _SEM_POST(&info->ta_sem);
-
-  return ret;
-}
-
-#endif /* CONFIG_TLS_NELEM > 0 */
diff --git a/libs/libc/tls/tls_free.c b/libs/libc/tls/tls_free.c
deleted file mode 100644
index e9f7eb576f..0000000000
--- a/libs/libc/tls/tls_free.c
+++ /dev/null
@@ -1,90 +0,0 @@
-/****************************************************************************
- * libs/libc/tls/tls_free.c
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.  The
- * ASF licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- ****************************************************************************/
-
-/****************************************************************************
- * Included Files
- ****************************************************************************/
-
-#include <nuttx/config.h>
-
-#include <sched.h>
-#include <errno.h>
-#include <assert.h>
-
-#include <nuttx/spinlock.h>
-#include <nuttx/tls.h>
-#include <nuttx/sched.h>
-
-#if CONFIG_TLS_NELEM > 0
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: tls_free
- *
- * Description:
- *   Release a group-unique TLS data index previous obtained by tls_alloc()
- *
- * Input Parameters:
- *   tlsindex - The previously allocated TLS index to be freed
- *
- * Returned Value:
- *   OK is returned on success;
- *   If unsuccessful an errno value will be returned and set to errno.
- *     -EINVAL    - the index to be freed is out of range.
- *     -EINTR     - the wait operation interrupted by signal
- *     -ECANCELED - the thread was canceled during waiting
- *
- ****************************************************************************/
-
-int tls_free(int tlsindex)
-{
-  FAR struct task_info_s *info = task_get_info();
-  tls_ndxset_t mask;
-  int ret = -EINVAL;
-
-  DEBUGASSERT((unsigned)tlsindex < CONFIG_TLS_NELEM && info != NULL);
-  if ((unsigned)tlsindex < CONFIG_TLS_NELEM)
-    {
-      /* This is done while holding a semaphore here to avoid concurrent
-       * modification of the group TLS index set.
-       */
-
-      mask  = (1 << tlsindex);
-
-      ret = _SEM_WAIT(&info->ta_sem);
-      if (ret == OK)
-        {
-          DEBUGASSERT((info->ta_tlsset & mask) != 0);
-          info->ta_tlsset &= ~mask;
-          _SEM_POST(&info->ta_sem);
-        }
-      else
-        {
-          ret = _SEM_ERRVAL(ret);
-        }
-    }
-
-  return ret;
-}
-
-#endif /* CONFIG_TLS_NELEM > 0 */
diff --git a/libs/libc/tls/tls_getvalue.c b/libs/libc/tls/tls_getvalue.c
deleted file mode 100644
index d6204ebd89..0000000000
--- a/libs/libc/tls/tls_getvalue.c
+++ /dev/null
@@ -1,110 +0,0 @@
-/****************************************************************************
- * libs/libc/tls/tls_getvalue.c
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.  The
- * ASF licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- ****************************************************************************/
-
-/****************************************************************************
- * Included Files
- ****************************************************************************/
-
-#include <nuttx/config.h>
-
-#include <assert.h>
-#include <errno.h>
-
-#include <nuttx/arch.h>
-#include <nuttx/tls.h>
-
-#if CONFIG_TLS_NELEM > 0
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: tls_get_value
- *
- * Description:
- *   Return an the TLS data value associated with the 'tlsindx'
- *
- * Input Parameters:
- *   tlsindex - Index of TLS data element to return
- *
- * Returned Value:
- *   The value of TLS element associated with 'tlsindex'. Errors are not
- *   reported.  Zero is returned in the event of an error, but zero may also
- *   be valid value and returned when there is no error.  The only possible
- *   error would be if tlsindex < 0 or tlsindex >=CONFIG_TLS_NELEM.
- *
- ****************************************************************************/
-
-uintptr_t tls_get_value(int tlsindex)
-{
-  FAR struct tls_info_s *info;
-  uintptr_t ret = 0;
-
-  DEBUGASSERT(tlsindex >= 0 && tlsindex < CONFIG_TLS_NELEM);
-  if (tlsindex >= 0 && tlsindex < CONFIG_TLS_NELEM)
-    {
-      /* Get the TLS info structure from the current threads stack */
-
-      info = up_tls_info();
-      DEBUGASSERT(info != NULL);
-
-      /* Get the element value from the TLS info. */
-
-      ret = info->tl_elem[tlsindex];
-    }
-
-  return ret;
-}
-
-#endif /* CONFIG_TLS_NELEM > 0 */
-
-#if CONFIG_TLS_TASK_NELEM > 0
-
-/****************************************************************************
- * Name: task_tls_get_value
- *
- * Description:
- *   Return an the task local storage data value associated with 'tlsindx'
- *
- * Input Parameters:
- *   tlsindex - Index of task local storage data element to return
- *
- * Returned Value:
- *   The value of TLS element associated with 'tlsindex'. Errors are not
- *   reported.  Zero is returned in the event of an error, but zero may also
- *   be valid value and returned when there is no error.  The only possible
- *   error would be if tlsindex < 0 or tlsindex >=CONFIG_TLS_TASK_NELEM.
- *
- ****************************************************************************/
-
-uintptr_t task_tls_get_value(int tlsindex)
-{
-  FAR struct task_info_s *info = task_get_info();
-
-  if (tlsindex >= 0 && tlsindex < CONFIG_TLS_TASK_NELEM)
-    {
-      return info->ta_telem[tlsindex];
-    }
-
-  return 0;
-}
-
-#endif