[libc-commits] [libc] [llvm] [libc] Use a syscall wrapper for `sched_setaffinity` (PR #232277)
Connector Switch via libc-commits
libc-commits at lists.llvm.org
Thu Oct 15 04:18:49 PDT 2026
https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/232277
This PR move `sched_setaffinity` behind the centralized syscall wrappers, following `libc/docs/dev/syscall_wrapper_refactor.md`.
>From b1f6bab13c823840706425689a3daf472ef1005d Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Thu, 15 Oct 2026 19:14:08 +0800
Subject: [PATCH] [libc] Use a syscall wrapper for sched_setaffinity
---
.../linux/syscall_wrappers/CMakeLists.txt | 14 ++++++
.../syscall_wrappers/sched_setaffinity.h | 38 +++++++++++++++
libc/src/sched/linux/CMakeLists.txt | 6 ++-
libc/src/sched/linux/sched_setaffinity.cpp | 13 ++---
.../llvm-project-overlay/libc/BUILD.bazel | 48 +++++++++++++++++++
5 files changed, 112 insertions(+), 7 deletions(-)
create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/sched_setaffinity.h
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index b65945c357a301..8c3c1811b2fab3 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 00000000000000..ca3ac8496922d2
--- /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 e28ffbe0604092..6ce385c034ea5a 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 3c7ed918b473fe..5b908c6cdb0e55 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 6ef8ae99aafe53..457de6f7048762 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"],
More information about the libc-commits
mailing list