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

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

commit 04e760b1c21b011453e555184975a5013ac0e647
Author: Ville Juven <ville.ju...@unikie.com>
AuthorDate: Fri Apr 4 13:50:56 2025 +0300

    sched/gettid: Move thread ID to TLS
    
    There is no need for a gettid() syscall, as the thread ID is stable through
    the life of the process. It is safe to put a copy of TID to TLS. This way
    a user processes can access TID quickly via its own stack, instead of
    having to use an expensive syscall.
    
    Signed-off-by: Ville Juven <ville.ju...@unikie.com>
---
 include/nuttx/tls.h                                |  1 +
 include/sys/syscall_lookup.h                       |  1 -
 libs/libc/sched/CMakeLists.txt                     |  3 +-
 libs/libc/sched/Make.defs                          |  2 +-
 .../libc/sched/task_gettid.c                       | 49 +++++++---------------
 sched/pthread/pthread_create.c                     | 18 ++++----
 sched/task/task_gettid.c                           | 19 ---------
 sched/task/task_init.c                             | 10 ++---
 sched/tls/tls_initinfo.c                           |  5 +++
 syscall/syscall.csv                                |  1 -
 10 files changed, 37 insertions(+), 72 deletions(-)

diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h
index 3298959a48..4646ba5cef 100644
--- a/include/nuttx/tls.h
+++ b/include/nuttx/tls.h
@@ -223,6 +223,7 @@ struct tls_info_s
 
   uint16_t tl_size;                    /* Actual size with alignments */
   int tl_errno;                        /* Per-thread error number */
+  pid_t tl_tid;                        /* Thread ID */
 };
 
 /****************************************************************************
diff --git a/include/sys/syscall_lookup.h b/include/sys/syscall_lookup.h
index ea66bc0a87..63bfb57b1c 100644
--- a/include/sys/syscall_lookup.h
+++ b/include/sys/syscall_lookup.h
@@ -29,7 +29,6 @@
 SYSCALL_LOOKUP1(_exit,                     1)
 SYSCALL_LOOKUP(_assert,                    4)
 SYSCALL_LOOKUP(getpid,                     0)
-SYSCALL_LOOKUP(gettid,                     0)
 SYSCALL_LOOKUP(prctl,                      2)
 
 #ifdef CONFIG_SCHED_HAVE_PARENT
diff --git a/libs/libc/sched/CMakeLists.txt b/libs/libc/sched/CMakeLists.txt
index 6f1fa76bac..c447e575f2 100644
--- a/libs/libc/sched/CMakeLists.txt
+++ b/libs/libc/sched/CMakeLists.txt
@@ -28,7 +28,8 @@ set(SRCS
     task_cancelpt.c
     task_setcancelstate.c
     task_setcanceltype.c
-    task_testcancel.c)
+    task_testcancel.c
+    task_gettid.c)
 
 if(CONFIG_SMP)
   list(APPEND SRCS sched_cpucount.c)
diff --git a/libs/libc/sched/Make.defs b/libs/libc/sched/Make.defs
index da8588d1fa..aa164356dd 100644
--- a/libs/libc/sched/Make.defs
+++ b/libs/libc/sched/Make.defs
@@ -25,7 +25,7 @@
 CSRCS += sched_getprioritymax.c sched_getprioritymin.c
 CSRCS += clock_getcpuclockid.c clock_getres.c
 CSRCS += task_cancelpt.c task_setcancelstate.c task_setcanceltype.c
-CSRCS += task_testcancel.c
+CSRCS += task_testcancel.c task_gettid.c
 
 ifeq ($(CONFIG_SMP),y)
 CSRCS += sched_cpucount.c
diff --git a/sched/tls/tls_initinfo.c b/libs/libc/sched/task_gettid.c
similarity index 65%
copy from sched/tls/tls_initinfo.c
copy to libs/libc/sched/task_gettid.c
index 62345e6bd2..33abbcc2e3 100644
--- a/sched/tls/tls_initinfo.c
+++ b/libs/libc/sched/task_gettid.c
@@ -1,5 +1,5 @@
 /****************************************************************************
- * sched/tls/tls_initinfo.c
+ * libs/libc/sched/task_gettid.c
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -24,54 +24,33 @@
  * Included Files
  ****************************************************************************/
 
-#include <assert.h>
-#include <errno.h>
+#include <nuttx/config.h>
 
-#include "tls.h"
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <nuttx/tls.h>
 
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
 
 /****************************************************************************
- * Name: tls_init_info
+ * Name: gettid
  *
  * Description:
- *   Allocate and initialize tls_info_s structure.
+ *   Get the thread ID of the currently executing thread.
  *
- * Input Parameters:
- *   - tcb: The TCB of new task
+ * Input parameters:
+ *   None
  *
  * Returned Value:
- *   Zero (OK) on success; a negated errno value on failure.
+ *   On success, returns the thread ID of the calling process.
  *
  ****************************************************************************/
 
-int tls_init_info(FAR struct tcb_s *tcb)
+pid_t gettid(void)
 {
-  FAR struct tls_info_s *info;
-
-  /* Allocate thread local storage */
-
-  info = up_stack_frame(tcb, tls_info_size());
-  if (info == NULL)
-    {
-      return -ENOMEM;
-    }
-
-  DEBUGASSERT(info == tcb->stack_alloc_ptr);
-
-  /* Initialize thread local storage */
-
-  up_tls_initialize(info);
-
-  /* Derive tl_size w/o arch knowledge */
-
-  info->tl_size =
-        (FAR char *)tcb->stack_base_ptr - (FAR char *)tcb->stack_alloc_ptr;
-
-  /* Attach per-task info in group to TLS */
-
-  info->tl_task = tcb->group->tg_info;
-  return OK;
+  FAR struct tls_info_s *tls = tls_get_info();
+  return tls->tl_tid;
 }
diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c
index 80d74fec50..227266cba7 100644
--- a/sched/pthread/pthread_create.c
+++ b/sched/pthread/pthread_create.c
@@ -281,15 +281,6 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR 
pthread_t *thread,
     }
 #endif
 
-  /* Initialize thread local storage */
-
-  ret = tls_init_info(&ptcb->cmn);
-  if (ret != OK)
-    {
-      errcode = -ret;
-      goto errout_with_tcb;
-    }
-
   /* Should we use the priority and scheduler specified in the pthread
    * attributes?  Or should we use the current thread's priority and
    * scheduler?
@@ -397,6 +388,15 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR 
pthread_t *thread,
       goto errout_with_tcb;
     }
 
+  /* Initialize thread local storage */
+
+  ret = tls_init_info(&ptcb->cmn);
+  if (ret != OK)
+    {
+      errcode = -ret;
+      goto errout_with_tcb;
+    }
+
 #ifdef CONFIG_SMP
   /* pthread_setup_scheduler() will set the affinity mask by inheriting the
    * setting from the parent task.  We need to override this setting
diff --git a/sched/task/task_gettid.c b/sched/task/task_gettid.c
index ff1fb663b9..3787de2649 100644
--- a/sched/task/task_gettid.c
+++ b/sched/task/task_gettid.c
@@ -91,22 +91,3 @@ pid_t nxsched_gettid(void)
 
   return 0;
 }
-
-/****************************************************************************
- * Name: gettid
- *
- * Description:
- *   Get the thread ID of the currently executing thread.
- *
- * Input parameters:
- *   None
- *
- * Returned Value:
- *   On success, returns the thread ID of the calling process.
- *
- ****************************************************************************/
-
-pid_t gettid(void)
-{
-  return nxsched_gettid();
-}
diff --git a/sched/task/task_init.c b/sched/task/task_init.c
index 7d338f53b2..0fa96f9972 100644
--- a/sched/task/task_init.c
+++ b/sched/task/task_init.c
@@ -168,18 +168,18 @@ int nxtask_init(FAR struct task_tcb_s *tcb, const char 
*name, int priority,
       goto errout_with_group;
     }
 
-  /* Initialize thread local storage */
+  /* Initialize the task control block */
 
-  ret = tls_init_info(&tcb->cmn);
+  ret = nxtask_setup_scheduler(tcb, priority, nxtask_start,
+                               entry, ttype);
   if (ret < OK)
     {
       goto errout_with_group;
     }
 
-  /* Initialize the task control block */
+  /* Initialize thread local storage */
 
-  ret = nxtask_setup_scheduler(tcb, priority, nxtask_start,
-                               entry, ttype);
+  ret = tls_init_info(&tcb->cmn);
   if (ret < OK)
     {
       goto errout_with_group;
diff --git a/sched/tls/tls_initinfo.c b/sched/tls/tls_initinfo.c
index 62345e6bd2..bf7227fd1a 100644
--- a/sched/tls/tls_initinfo.c
+++ b/sched/tls/tls_initinfo.c
@@ -73,5 +73,10 @@ int tls_init_info(FAR struct tcb_s *tcb)
   /* Attach per-task info in group to TLS */
 
   info->tl_task = tcb->group->tg_info;
+
+  /* Thread ID */
+
+  info->tl_tid = tcb->pid;
+
   return OK;
 }
diff --git a/syscall/syscall.csv b/syscall/syscall.csv
index 91fae362c8..2e3444c132 100644
--- a/syscall/syscall.csv
+++ b/syscall/syscall.csv
@@ -48,7 +48,6 @@
 "getppid","unistd.h","defined(CONFIG_SCHED_HAVE_PARENT)","pid_t"
 "getsockname","sys/socket.h","defined(CONFIG_NET)","int","int","FAR struct 
sockaddr *","FAR socklen_t *"
 "getsockopt","sys/socket.h","defined(CONFIG_NET)","int","int","int","int","FAR 
void *","FAR socklen_t *"
-"gettid","unistd.h","","pid_t"
 "gettimeofday","sys/time.h","","int","FAR struct timeval *","FAR struct 
timezone *"
 "getuid","unistd.h","defined(CONFIG_SCHED_USER_IDENTITY)","uid_t"
 
"inotify_add_watch","sys/inotify.h","defined(CONFIG_FS_NOTIFY)","int","int","FAR
 const char *","uint32_t"

Reply via email to