[libc-commits] [libc] [llvm] [libc] Move the fork gettid call into a syscall wrapper (PR #242606)

via libc-commits libc-commits at lists.llvm.org
Fri Nov 13 23:48:20 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Connector Switch (c8ef)

<details>
<summary>Changes</summary>

Move the uncached gettid syscall used by fork() to refresh the child TID into linux_syscalls::gettid. Register the wrapper in CMake and Bazel.

Keep the existing SYS_fork/SYS_clone selection, callbacks, and TID-cache behavior unchanged. The switch to clone3 is a separate change in #<!-- -->242253.

Validated parent/child IDs and callbacks with both SYS_fork and the SYS_clone fallback.


---
Full diff: https://github.com/llvm/llvm-project/pull/242606.diff


6 Files Affected:

- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+12) 
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/gettid.h (+31) 
- (modified) libc/src/unistd/linux/CMakeLists.txt (+1) 
- (modified) libc/src/unistd/linux/fork.cpp (+3-2) 
- (modified) utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel (+15) 
- (modified) utils/bazel/llvm-project-overlay/libc/src/unistd/BUILD.bazel (+1) 


``````````diff
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 4a417280c4c568..322795b34ab298 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -100,6 +100,18 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  gettid
+  HDRS
+    gettid.h
+  DEPENDS
+    libc.hdr.types.pid_t
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   getuid
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/gettid.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/gettid.h
new file mode 100644
index 00000000000000..42f7862326f6ee
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/gettid.h
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for gettid.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETTID_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETTID_H
+
+#include "hdr/types/pid_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers.
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE pid_t gettid() { return syscall_impl<pid_t>(SYS_gettid); }
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_GETTID_H
diff --git a/libc/src/unistd/linux/CMakeLists.txt b/libc/src/unistd/linux/CMakeLists.txt
index f179b86543e5d3..67313890cf1565 100644
--- a/libc/src/unistd/linux/CMakeLists.txt
+++ b/libc/src/unistd/linux/CMakeLists.txt
@@ -215,6 +215,7 @@ add_entrypoint_object(
     libc.include.unistd
     libc.include.sys_syscall
     libc.src.__support.threads.fork_callbacks
+    libc.src.__support.OSUtil.linux.syscall_wrappers.gettid
     libc.src.__support.OSUtil.osutil
     libc.src.__support.threads.thread
     libc.src.errno.errno
diff --git a/libc/src/unistd/linux/fork.cpp b/libc/src/unistd/linux/fork.cpp
index a7529415fa873e..a8482592d6ea8d 100644
--- a/libc/src/unistd/linux/fork.cpp
+++ b/libc/src/unistd/linux/fork.cpp
@@ -7,7 +7,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/unistd/fork.h"
-#include "hdr/signal_macros.h"            // For SIGCHLD
+#include "hdr/signal_macros.h" // For SIGCHLD
+#include "src/__support/OSUtil/linux/syscall_wrappers/gettid.h"
 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
@@ -43,7 +44,7 @@ LLVM_LIBC_FUNCTION(pid_t, fork, (void)) {
     // The child is created with a single thread whose self object will be a
     // copy of parent process' thread which called fork. So, we have to fix up
     // the child process' self object with the new process' tid.
-    internal::force_set_tid(syscall_impl<pid_t>(SYS_gettid));
+    internal::force_set_tid(linux_syscalls::gettid());
     invoke_child_callbacks();
     return 0;
   }
diff --git a/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel
index 482e9e4bf11f38..449da567cedc42 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/__support/OSUtil/linux/syscall_wrappers/BUILD.bazel
@@ -990,6 +990,21 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "gettid",
+    hdrs = ["gettid.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        "//libc/hdr/types:pid_t",
+        "//libc/src/__support:common",
+        "//libc/src/__support/OSUtil:syscall",
+        "//libc/src/__support/macros:config",
+    ],
+)
+
 libc_support_library(
     name = "getuid",
     hdrs = ["getuid.h"],
diff --git a/utils/bazel/llvm-project-overlay/libc/src/unistd/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/src/unistd/BUILD.bazel
index 572162a076bcdf..7331e237da6252 100644
--- a/utils/bazel/llvm-project-overlay/libc/src/unistd/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/src/unistd/BUILD.bazel
@@ -304,6 +304,7 @@ libc_function(
         "//libc/src/__support:common",
         "//libc/src/__support:libc_errno",
         "//libc/src/__support/OSUtil:syscall",
+        "//libc/src/__support/OSUtil/linux/syscall_wrappers:gettid",
         "//libc/src/__support/macros:config",
         "//libc/src/__support/threads:fork_callbacks",
         "//libc/src/__support/threads:identifier",

``````````

</details>


https://github.com/llvm/llvm-project/pull/242606


More information about the libc-commits mailing list