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

Connector Switch via libc-commits libc-commits at lists.llvm.org
Thu Oct 15 05:59:08 PDT 2026


https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/232310

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

>From a6dd402edf7f5513ae949e0597561d441d155e7e Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Thu, 15 Oct 2026 20:56:36 +0800
Subject: [PATCH] [libc] Use a syscall wrapper for sched_rr_get_interval

---
 .../linux/syscall_wrappers/CMakeLists.txt     | 14 +++++
 .../syscall_wrappers/sched_rr_get_interval.h  | 52 +++++++++++++++++++
 libc/src/sched/linux/CMakeLists.txt           |  6 ++-
 .../src/sched/linux/sched_rr_get_interval.cpp | 23 ++------
 .../llvm-project-overlay/libc/BUILD.bazel     | 32 ++++++++++++
 5 files changed, 106 insertions(+), 21 deletions(-)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/sched_rr_get_interval.h

diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 8c3c1811b2fab3..eaa9115551103b 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_rr_get_interval
+  HDRS
+    sched_rr_get_interval.h
+  DEPENDS
+    libc.hdr.types.pid_t
+    libc.hdr.types.struct_timespec
+    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_setaffinity
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/sched_rr_get_interval.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/sched_rr_get_interval.h
new file mode 100644
index 00000000000000..b22d5507d58ff5
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/sched_rr_get_interval.h
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_rr_get_interval.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SCHED_RR_GET_INTERVAL_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SCHED_RR_GET_INTERVAL_H
+
+#include "hdr/types/pid_t.h"
+#include "hdr/types/struct_timespec.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_rr_get_interval(pid_t tid, struct timespec *tp) {
+#if defined(SYS_sched_rr_get_interval_time64)
+  static_assert(
+      sizeof(time_t) == sizeof(int64_t),
+      "SYS_sched_rr_get_interval_time64 requires struct timespec with 64-bit "
+      "members.");
+  return syscall_checked<int>(SYS_sched_rr_get_interval_time64, tid, tp);
+#elif defined(SYS_sched_rr_get_interval)
+  static_assert(
+      sizeof(timespec::tv_nsec) == sizeof(long),
+      "This legacy syscall fallback is only safe on platforms where tv_nsec "
+      "matches the register size (long). It is unsafe on 32-bit platforms "
+      "with 64-bit tv_nsec.");
+  return syscall_checked<int>(SYS_sched_rr_get_interval, tid, tp);
+#else
+#error                                                                         \
+    "SYS_sched_rr_get_interval and SYS_sched_rr_get_interval_time64 syscalls not available."
+#endif
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SCHED_RR_GET_INTERVAL_H
diff --git a/libc/src/sched/linux/CMakeLists.txt b/libc/src/sched/linux/CMakeLists.txt
index 6ce385c034ea5a..1a2db35f426f70 100644
--- a/libc/src/sched/linux/CMakeLists.txt
+++ b/libc/src/sched/linux/CMakeLists.txt
@@ -190,8 +190,10 @@ add_entrypoint_object(
   DEPENDS
     libc.hdr.types.pid_t
     libc.hdr.types.struct_timespec
-    libc.include.sys_syscall
-    libc.src.__support.OSUtil.osutil
+    libc.src.__support.OSUtil.linux.syscall_wrappers.sched_rr_get_interval
+    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_rr_get_interval.cpp b/libc/src/sched/linux/sched_rr_get_interval.cpp
index c87d1808bc902b..c47b82d02b3967 100644
--- a/libc/src/sched/linux/sched_rr_get_interval.cpp
+++ b/libc/src/sched/linux/sched_rr_get_interval.cpp
@@ -8,36 +8,21 @@
 
 #include "src/sched/sched_rr_get_interval.h"
 
-#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
+#include "src/__support/OSUtil/linux/syscall_wrappers/sched_rr_get_interval.h"
 #include "src/__support/common.h"
 #include "src/__support/libc_errno.h"
 #include "src/__support/macros/config.h"
 
 #include "hdr/types/pid_t.h"
 #include "hdr/types/struct_timespec.h"
-#include <sys/syscall.h> // For syscall numbers.
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, sched_rr_get_interval,
                    (pid_t tid, struct timespec *tp)) {
-#if defined(SYS_sched_rr_get_interval_time64)
-  int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_sched_rr_get_interval_time64,
-                                              tid, tp);
-#elif defined(SYS_sched_rr_get_interval)
-  static_assert(
-      sizeof(timespec::tv_nsec) == sizeof(long),
-      "This legacy syscall fallback is only safe on platforms where tv_nsec "
-      "matches the register size (long). It is unsafe on 32-bit platforms "
-      "with 64-bit tv_nsec.");
-  int ret =
-      LIBC_NAMESPACE::syscall_impl<int>(SYS_sched_rr_get_interval, tid, tp);
-#else
-#error                                                                         \
-    "sched_rr_get_interval and sched_rr_get_interval_time64 syscalls not available."
-#endif
-  if (ret < 0) {
-    libc_errno = -ret;
+  auto result = linux_syscalls::sched_rr_get_interval(tid, tp);
+  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 457de6f7048762..f9b7a5b9c4accc 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2746,6 +2746,23 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_osutil_linux_syscall_wrappers_sched_rr_get_interval",
+    hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/sched_rr_get_interval.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        ":__support_common",
+        ":__support_error_or",
+        ":__support_macros_config",
+        ":__support_osutil_syscall",
+        ":types_pid_t",
+        ":types_struct_timespec",
+    ],
+)
+
 libc_support_library(
     name = "__support_osutil_linux_syscall_wrappers_sched_setaffinity",
     hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/sched_setaffinity.h"],
@@ -16939,6 +16956,21 @@ libc_function(
     ],
 )
 
+libc_function(
+    name = "sched_rr_get_interval",
+    srcs = ["src/sched/linux/sched_rr_get_interval.cpp"],
+    hdrs = ["src/sched/sched_rr_get_interval.h"],
+    deps = [
+        ":__support_common",
+        ":__support_libc_errno",
+        ":__support_macros_config",
+        ":__support_osutil_linux_syscall_wrappers_sched_rr_get_interval",
+        ":errno",
+        ":types_pid_t",
+        ":types_struct_timespec",
+    ],
+)
+
 libc_function(
     name = "sched_setaffinity",
     srcs = ["src/sched/linux/sched_setaffinity.cpp"],



More information about the libc-commits mailing list