[libc-commits] [libc] [llvm] [libc] Use a syscall wrapper for `sched_setaffinity` (PR #232277)

via libc-commits libc-commits at lists.llvm.org
Thu Oct 15 04:19:26 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Connector Switch (c8ef)

<details>
<summary>Changes</summary>

This PR move `sched_setaffinity` behind the centralized syscall wrappers, following `libc/docs/dev/syscall_wrapper_refactor.md`.

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


5 Files Affected:

- (modified) libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt (+14) 
- (added) libc/src/__support/OSUtil/linux/syscall_wrappers/sched_setaffinity.h (+38) 
- (modified) libc/src/sched/linux/CMakeLists.txt (+5-1) 
- (modified) libc/src/sched/linux/sched_setaffinity.cpp (+7-6) 
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+48) 


``````````diff
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index b65945c357a30..8c3c1811b2fab 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -79,6 +79,20 @@ add_header_library(
     libc.include.sys_syscall
 )
 
+add_header_library(
+  sched_setaffinity
+  HDRS
+    sched_setaffinity.h
+  DEPENDS
+    libc.hdr.types.pid_t
+    libc.src.__support.CPP.span
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.include.sys_syscall
+)
+
 add_header_library(
   sched_setparam
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/sched_setaffinity.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/sched_setaffinity.h
new file mode 100644
index 0000000000000..ca3ac8496922d
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/sched_setaffinity.h
@@ -0,0 +1,38 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 sched_setaffinity.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SCHED_SETAFFINITY_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SCHED_SETAFFINITY_H
+
+#include "hdr/types/pid_t.h"
+#include "src/__support/CPP/span.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int>
+sched_setaffinity(pid_t tid, cpp::span<const unsigned char> mask) {
+  return syscall_checked<int>(SYS_sched_setaffinity, tid, mask.size(),
+                              mask.data());
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SCHED_SETAFFINITY_H
diff --git a/libc/src/sched/linux/CMakeLists.txt b/libc/src/sched/linux/CMakeLists.txt
index e28ffbe060409..6ce385c034ea5 100644
--- a/libc/src/sched/linux/CMakeLists.txt
+++ b/libc/src/sched/linux/CMakeLists.txt
@@ -64,7 +64,11 @@ add_entrypoint_object(
     libc.hdr.types.cpu_set_t
     libc.hdr.types.pid_t
     libc.hdr.types.size_t
-    libc.src.__support.OSUtil.osutil
+    libc.src.__support.CPP.span
+    libc.src.__support.OSUtil.linux.syscall_wrappers.sched_setaffinity
+    libc.src.__support.common
+    libc.src.__support.libc_errno
+    libc.src.__support.macros.config
     libc.src.errno.errno
 )
 
diff --git a/libc/src/sched/linux/sched_setaffinity.cpp b/libc/src/sched/linux/sched_setaffinity.cpp
index 3c7ed918b473f..5b908c6cdb0e5 100644
--- a/libc/src/sched/linux/sched_setaffinity.cpp
+++ b/libc/src/sched/linux/sched_setaffinity.cpp
@@ -8,7 +8,8 @@
 
 #include "src/sched/sched_setaffinity.h"
 
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/CPP/span.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/sched_setaffinity.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
@@ -16,16 +17,16 @@
 #include "hdr/types/cpu_set_t.h"
 #include "hdr/types/pid_t.h"
 #include "hdr/types/size_t.h"
-#include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, sched_setaffinity,
                    (pid_t tid, size_t cpuset_size, const cpu_set_t *mask)) {
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_sched_setaffinity, tid,
-                                              cpuset_size, mask);
-  if (ret < 0) {
-    libc_errno = -ret;
+  auto result = linux_syscalls::sched_setaffinity(
+      tid, cpp::span<const unsigned char>(
+               reinterpret_cast<const unsigned char *>(mask), cpuset_size));
+  if (!result) {
+    libc_errno = result.error();
     return -1;
   }
   return 0;
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 6ef8ae99aafe5..457de6f704876 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -559,6 +559,11 @@ libc_support_library(
     deps = [":llvm_libc_macros_complex_macros"],
 )
 
+libc_support_library(
+    name = "llvm_libc_types_cpu_set_t",
+    hdrs = ["include/llvm-libc-types/cpu_set_t.h"],
+)
+
 libc_support_library(
     name = "llvm_libc_types_mode_t",
     hdrs = ["include/llvm-libc-types/mode_t.h"],
@@ -1161,6 +1166,15 @@ libc_support_library(
     }),
 )
 
+libc_support_library(
+    name = "types_cpu_set_t",
+    hdrs = ["hdr/types/cpu_set_t.h"],
+    deps = select({
+        ":full_build": [":llvm_libc_types_cpu_set_t"],
+        "//conditions:default": [],
+    }),
+)
+
 libc_support_library(
     name = "types_div_t",
     hdrs = ["hdr/types/div_t.h"],
@@ -2732,6 +2746,23 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_osutil_linux_syscall_wrappers_sched_setaffinity",
+    hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/sched_setaffinity.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        ":__support_common",
+        ":__support_cpp_span",
+        ":__support_error_or",
+        ":__support_macros_config",
+        ":__support_osutil_syscall",
+        ":types_pid_t",
+    ],
+)
+
 libc_support_library(
     name = "__support_osutil_linux_syscall_wrappers_sched_setparam",
     hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/sched_setparam.h"],
@@ -16908,6 +16939,23 @@ libc_function(
     ],
 )
 
+libc_function(
+    name = "sched_setaffinity",
+    srcs = ["src/sched/linux/sched_setaffinity.cpp"],
+    hdrs = ["src/sched/sched_setaffinity.h"],
+    deps = [
+        ":__support_common",
+        ":__support_cpp_span",
+        ":__support_libc_errno",
+        ":__support_macros_config",
+        ":__support_osutil_linux_syscall_wrappers_sched_setaffinity",
+        ":errno",
+        ":types_cpu_set_t",
+        ":types_pid_t",
+        ":types_size_t",
+    ],
+)
+
 libc_function(
     name = "sched_setparam",
     srcs = ["src/sched/linux/sched_setparam.cpp"],

``````````

</details>


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


More information about the libc-commits mailing list