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 8bb675849061c4bda7294b075e6f982e81436b19
Author: hujun5 <[email protected]>
AuthorDate: Thu Jun 5 21:11:42 2025 +0800

    sched: move process ID from kernel to TLS for faster access
    
    Add pid field to task_info_s and move getpid() implementation to user
    space TLS access. Remove getpid from syscall interface as it now returns
    cached PID from thread local storage instead of kernel lookup.
    
    Signed-off-by: hujun5 <[email protected]>
---
 include/nuttx/tls.h            |  1 +
 include/sys/syscall_lookup.h   |  1 -
 libs/libc/sched/CMakeLists.txt |  3 ++-
 libs/libc/sched/Make.defs      |  2 +-
 libs/libc/sched/task_getpid.c  | 58 ++++++++++++++++++++++++++++++++++++++++++
 sched/group/group_create.c     |  2 ++
 sched/task/task_getpid.c       | 22 ----------------
 syscall/syscall.csv            |  1 -
 8 files changed, 64 insertions(+), 26 deletions(-)

diff --git a/include/nuttx/tls.h b/include/nuttx/tls.h
index 4159035d000..f971afe96f6 100644
--- a/include/nuttx/tls.h
+++ b/include/nuttx/tls.h
@@ -149,6 +149,7 @@ struct task_info_s
 #ifdef CONFIG_PTHREAD_ATFORK
   struct list_node ta_atfork; /* Holds the pthread_atfork_s list */
 #endif
+  pid_t ta_pid; /* Process ID */
 };
 
 /* struct tls_cleanup_s *****************************************************/
diff --git a/include/sys/syscall_lookup.h b/include/sys/syscall_lookup.h
index 39ef26c16c4..60fd08dced1 100644
--- a/include/sys/syscall_lookup.h
+++ b/include/sys/syscall_lookup.h
@@ -28,7 +28,6 @@
 
 SYSCALL_LOOKUP1(_exit,                     1)
 SYSCALL_LOOKUP(_assert,                    4)
-SYSCALL_LOOKUP(getpid,                     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 8297eebce77..70f76c1e014 100644
--- a/libs/libc/sched/CMakeLists.txt
+++ b/libs/libc/sched/CMakeLists.txt
@@ -28,7 +28,8 @@ set(SRCS
     task_setcancelstate.c
     task_setcanceltype.c
     task_testcancel.c
-    task_gettid.c)
+    task_gettid.c
+    task_getpid.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 23aa8c0b2d5..24793634708 100644
--- a/libs/libc/sched/Make.defs
+++ b/libs/libc/sched/Make.defs
@@ -24,7 +24,7 @@
 
 CSRCS += sched_getprioritymax.c sched_getprioritymin.c
 CSRCS += task_cancelpt.c task_setcancelstate.c task_setcanceltype.c
-CSRCS += task_testcancel.c task_gettid.c
+CSRCS += task_testcancel.c task_gettid.c task_getpid.c
 CSRCS += clock_getcpuclockid.c
 
 ifeq ($(CONFIG_SMP),y)
diff --git a/libs/libc/sched/task_getpid.c b/libs/libc/sched/task_getpid.c
new file mode 100644
index 00000000000..bda6fcf6239
--- /dev/null
+++ b/libs/libc/sched/task_getpid.c
@@ -0,0 +1,58 @@
+/****************************************************************************
+ * libs/libc/sched/task_getpid.c
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * 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 <sys/types.h>
+#include <unistd.h>
+
+#include <nuttx/tls.h>
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: getpid
+ *
+ * Description:
+ *   Get the process ID of the currently executing thread.
+ *
+ * Input parameters:
+ *   None
+ *
+ * Returned Value:
+ *   On success, returns the thread ID of the calling process.
+ *
+ ****************************************************************************/
+
+pid_t getpid(void)
+{
+  FAR struct task_info_s *info = task_get_info();
+
+  DEBUGASSERT(info != NULL);
+  return info->ta_pid;
+}
diff --git a/sched/group/group_create.c b/sched/group/group_create.c
index 336a7a64f1a..122a25d06d6 100644
--- a/sched/group/group_create.c
+++ b/sched/group/group_create.c
@@ -254,4 +254,6 @@ void group_initialize(FAR struct tcb_s *tcb)
     {
       group->tg_pid = tcb->pid;
     }
+
+  group->tg_info->ta_pid = group->tg_pid;
 }
diff --git a/sched/task/task_getpid.c b/sched/task/task_getpid.c
index 76c63e9fcbe..0cf46d8ac21 100644
--- a/sched/task/task_getpid.c
+++ b/sched/task/task_getpid.c
@@ -80,25 +80,3 @@ pid_t nxsched_getpid(void)
 
   return IDLE_PROCESS_ID;
 }
-
-/****************************************************************************
- * Name: getpid
- *
- * Description:
- *   Get the Process ID of the currently executing task.
- *
- * Input parameters:
- *   None
- *
- * Returned Value:
- *   Normally when called from user applications, getpid() will return the
- *   Process ID of the currently executing task. that is, the main task
- *   for the task groups. There is no specification for any errors
- *   returned from getpid().
- *
- ****************************************************************************/
-
-pid_t getpid(void)
-{
-  return nxsched_getpid();
-}
diff --git a/syscall/syscall.csv b/syscall/syscall.csv
index 531154b0c50..4cf841bc3a4 100644
--- a/syscall/syscall.csv
+++ b/syscall/syscall.csv
@@ -45,7 +45,6 @@
 "gethostname","unistd.h","","int","FAR char *","size_t"
 
"getitimer","sys/time.h","!defined(CONFIG_DISABLE_POSIX_TIMERS)","int","int","FAR
 struct itimerval *"
 "getpeername","sys/socket.h","defined(CONFIG_NET)","int","int","FAR struct 
sockaddr *","FAR socklen_t *"
-"getpid","unistd.h","","pid_t"
 "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 *"

Reply via email to