[libc-commits] [libc] bdf664c - [libc] Implement pthread_attr_[gs]etschedparam (#222991)

via libc-commits libc-commits at lists.llvm.org
Sun Sep 13 23:34:14 PDT 2026


Author: Pavel Labath
Date: 2026-09-14T08:34:09+02:00
New Revision: bdf664cf900fcf7aa0d30b730c7e0b752bcd3c89

URL: https://github.com/llvm/llvm-project/commit/bdf664cf900fcf7aa0d30b730c7e0b752bcd3c89
DIFF: https://github.com/llvm/llvm-project/commit/bdf664cf900fcf7aa0d30b730c7e0b752bcd3c89.diff

LOG: [libc] Implement pthread_attr_[gs]etschedparam (#222991)

This patch implements pthread_attr_setschedparam and
pthread_attr_getschedparam.

Like the schedpolicy patch, this commit only operates on the
pthread_attr_t object and does not attempt to install scheduling
parameters when creating a new thread.

I also don't attempt to validate the priority value here. The valid
priority range depends on the scheduling policy (which may not even be
set yet or could change later), and we can let the kernel validate the
parameters when creating the thread or setting scheduler settings.

While in there:
- update the file headers to the modern llvm format
- use proxy headers instead of direct pthread.h inclusion

Assisted-by: Gemini

Added: 
    

Modified: 
    libc/config/linux/aarch64/entrypoints.txt
    libc/config/linux/riscv/entrypoints.txt
    libc/config/linux/x86_64/entrypoints.txt
    libc/include/llvm-libc-types/CMakeLists.txt
    libc/include/llvm-libc-types/pthread_attr_t.h
    libc/src/pthread/CMakeLists.txt
    libc/src/pthread/pthread_attr_getschedparam.cpp
    libc/src/pthread/pthread_attr_getschedparam.h
    libc/src/pthread/pthread_attr_init.cpp
    libc/src/pthread/pthread_attr_setschedparam.cpp
    libc/src/pthread/pthread_attr_setschedparam.h
    libc/test/src/pthread/CMakeLists.txt
    libc/test/src/pthread/pthread_attr_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index abf928394e591..a96057944af75 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1119,6 +1119,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.pthread.pthread_attr_destroy
     libc.src.pthread.pthread_attr_getdetachstate
     libc.src.pthread.pthread_attr_getguardsize
+    libc.src.pthread.pthread_attr_getschedparam
     libc.src.pthread.pthread_attr_getschedpolicy
     libc.src.pthread.pthread_attr_getscope
     libc.src.pthread.pthread_attr_getstack
@@ -1126,6 +1127,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.pthread.pthread_attr_init
     libc.src.pthread.pthread_attr_setdetachstate
     libc.src.pthread.pthread_attr_setguardsize
+    libc.src.pthread.pthread_attr_setschedparam
     libc.src.pthread.pthread_attr_setschedpolicy
     libc.src.pthread.pthread_attr_setscope
     libc.src.pthread.pthread_attr_setstack

diff  --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index c05f58413eac3..4b2f6e2e72120 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1312,6 +1312,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.pthread.pthread_attr_destroy
     libc.src.pthread.pthread_attr_getdetachstate
     libc.src.pthread.pthread_attr_getguardsize
+    libc.src.pthread.pthread_attr_getschedparam
     libc.src.pthread.pthread_attr_getschedpolicy
     libc.src.pthread.pthread_attr_getscope
     libc.src.pthread.pthread_attr_getstack
@@ -1319,6 +1320,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.pthread.pthread_attr_init
     libc.src.pthread.pthread_attr_setdetachstate
     libc.src.pthread.pthread_attr_setguardsize
+    libc.src.pthread.pthread_attr_setschedparam
     libc.src.pthread.pthread_attr_setschedpolicy
     libc.src.pthread.pthread_attr_setscope
     libc.src.pthread.pthread_attr_setstack

diff  --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 5cc68f9ef507b..c4856700e9ea8 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1323,6 +1323,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.pthread.pthread_attr_destroy
     libc.src.pthread.pthread_attr_getdetachstate
     libc.src.pthread.pthread_attr_getguardsize
+    libc.src.pthread.pthread_attr_getschedparam
     libc.src.pthread.pthread_attr_getschedpolicy
     libc.src.pthread.pthread_attr_getscope
     libc.src.pthread.pthread_attr_getstack
@@ -1330,6 +1331,7 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.pthread.pthread_attr_init
     libc.src.pthread.pthread_attr_setdetachstate
     libc.src.pthread.pthread_attr_setguardsize
+    libc.src.pthread.pthread_attr_setschedparam
     libc.src.pthread.pthread_attr_setschedpolicy
     libc.src.pthread.pthread_attr_setscope
     libc.src.pthread.pthread_attr_setstack

diff  --git a/libc/include/llvm-libc-types/CMakeLists.txt b/libc/include/llvm-libc-types/CMakeLists.txt
index 8a944c9a5a573..9e678300e3bfd 100644
--- a/libc/include/llvm-libc-types/CMakeLists.txt
+++ b/libc/include/llvm-libc-types/CMakeLists.txt
@@ -137,7 +137,8 @@ add_header(once_flag HDR once_flag.h DEPENDS .__futex_word)
 add_header(posix_spawn_file_actions_t HDR posix_spawn_file_actions_t.h)
 add_header(posix_spawnattr_t HDR posix_spawnattr_t.h)
 add_header(posix_tnode HDR posix_tnode.h)
-add_header(pthread_attr_t HDR pthread_attr_t.h DEPENDS .size_t)
+add_header(struct_sched_param HDR struct_sched_param.h)
+add_header(pthread_attr_t HDR pthread_attr_t.h DEPENDS .size_t .struct_sched_param)
 add_header(pthread_cond_t HDR pthread_cond_t.h DEPENDS .__futex_word .size_t)
 add_header(pthread_condattr_t HDR pthread_condattr_t.h DEPENDS .clockid_t)
 add_header(pthread_key_t HDR pthread_key_t.h)
@@ -190,7 +191,6 @@ add_header(
 )
 add_header(struct_pollfd HDR struct_pollfd.h)
 add_header(struct_rlimit HDR struct_rlimit.h DEPENDS .rlim_t)
-add_header(struct_sched_param HDR struct_sched_param.h)
 add_header(struct_sembuf HDR struct_sembuf.h)
 add_header(struct_seminfo HDR struct_seminfo.h)
 add_header(

diff  --git a/libc/include/llvm-libc-types/pthread_attr_t.h b/libc/include/llvm-libc-types/pthread_attr_t.h
index e974e2d218f03..0e42c0d04fdf7 100644
--- a/libc/include/llvm-libc-types/pthread_attr_t.h
+++ b/libc/include/llvm-libc-types/pthread_attr_t.h
@@ -10,10 +10,12 @@
 #define LLVM_LIBC_TYPES_PTHREAD_ATTR_T_H
 
 #include "size_t.h"
+#include "struct_sched_param.h"
 
 typedef struct {
   int __detachstate;
   int __schedpolicy;
+  struct sched_param __schedparam;
   void *__stack;
   size_t __stacksize;
   size_t __guardsize;

diff  --git a/libc/src/pthread/CMakeLists.txt b/libc/src/pthread/CMakeLists.txt
index 9bcc1d1d85b79..7eb081a64f78a 100644
--- a/libc/src/pthread/CMakeLists.txt
+++ b/libc/src/pthread/CMakeLists.txt
@@ -68,7 +68,11 @@ add_entrypoint_object(
   HDRS
     pthread_attr_getschedparam.h
   DEPENDS
-    libc.include.pthread
+    libc.hdr.types.pthread_attr_t
+    libc.hdr.types.struct_sched_param
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.macros.null_check
 )
 
 add_entrypoint_object(
@@ -105,7 +109,11 @@ add_entrypoint_object(
   HDRS
     pthread_attr_setschedparam.h
   DEPENDS
-    libc.include.pthread
+    libc.hdr.types.pthread_attr_t
+    libc.hdr.types.struct_sched_param
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.macros.null_check
 )
 
 add_entrypoint_object(

diff  --git a/libc/src/pthread/pthread_attr_getschedparam.cpp b/libc/src/pthread/pthread_attr_getschedparam.cpp
index 51fddea672af0..71e0446c1238d 100644
--- a/libc/src/pthread/pthread_attr_getschedparam.cpp
+++ b/libc/src/pthread/pthread_attr_getschedparam.cpp
@@ -1,25 +1,31 @@
-//===-- Implementation of the pthread_attr_getschedparam -----------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
+/// Implementation of pthread_attr_getschedparam.
+///
+//===----------------------------------------------------------------------===//
 
 #include "pthread_attr_getschedparam.h"
-
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
-
-#include "hdr/errno_macros.h"
-#include <pthread.h>
+#include "src/__support/macros/null_check.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, pthread_attr_getschedparam,
-                   (const pthread_attr_t *attr,
-                    struct sched_param *schedparam)) {
-  return ENOTSUP;
+                   (const pthread_attr_t *__restrict attr,
+                    struct sched_param *__restrict param)) {
+  LIBC_CRASH_ON_NULLPTR(attr);
+  LIBC_CRASH_ON_NULLPTR(param);
+
+  *param = attr->__schedparam;
+  return 0;
 }
 
 } // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/pthread/pthread_attr_getschedparam.h b/libc/src/pthread/pthread_attr_getschedparam.h
index ee118d77c1c02..008af002843cd 100644
--- a/libc/src/pthread/pthread_attr_getschedparam.h
+++ b/libc/src/pthread/pthread_attr_getschedparam.h
@@ -1,21 +1,27 @@
-//===-- Implementation header for pthread_attr_getschedparam -*- C++ -*-===//
+//===----------------------------------------------------------------------===//
 //
 // 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
+/// Implementation header for pthread_attr_getschedparam.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_GETSCHEDPARAM_H
 #define LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_GETSCHEDPARAM_H
 
+#include "hdr/types/pthread_attr_t.h"
+#include "hdr/types/struct_sched_param.h"
 #include "src/__support/macros/config.h"
-#include <pthread.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
 int pthread_attr_getschedparam(const pthread_attr_t *__restrict attr,
-                               struct sched_param *__restrict schedparam);
+                               struct sched_param *__restrict param);
 
 } // namespace LIBC_NAMESPACE_DECL
 

diff  --git a/libc/src/pthread/pthread_attr_init.cpp b/libc/src/pthread/pthread_attr_init.cpp
index 843dca69adc3c..9ae7acebec391 100644
--- a/libc/src/pthread/pthread_attr_init.cpp
+++ b/libc/src/pthread/pthread_attr_init.cpp
@@ -21,6 +21,7 @@ LLVM_LIBC_FUNCTION(int, pthread_attr_init, (pthread_attr_t * attr)) {
   *attr = pthread_attr_t{
       PTHREAD_CREATE_JOINABLE,   // Not detached
       SCHED_OTHER,               // Default scheduling policy
+      {},                        // Default scheduling parameters
       nullptr,                   // Let the thread manage its stack
       Thread::DEFAULT_STACKSIZE, // stack size.
       Thread::DEFAULT_GUARDSIZE, // Default page size for the guard size.

diff  --git a/libc/src/pthread/pthread_attr_setschedparam.cpp b/libc/src/pthread/pthread_attr_setschedparam.cpp
index 76b2e5177676c..18094439cf1ea 100644
--- a/libc/src/pthread/pthread_attr_setschedparam.cpp
+++ b/libc/src/pthread/pthread_attr_setschedparam.cpp
@@ -1,25 +1,31 @@
-//===-- Implementation of the pthread_attr_setschedparam -----------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
+/// Implementation of pthread_attr_setschedparam.
+///
+//===----------------------------------------------------------------------===//
 
 #include "pthread_attr_setschedparam.h"
-
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
-
-#include "hdr/errno_macros.h"
-#include <pthread.h>
+#include "src/__support/macros/null_check.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
 LLVM_LIBC_FUNCTION(int, pthread_attr_setschedparam,
-                   (pthread_attr_t * attr,
-                    const struct sched_param *schedparam)) {
-  return ENOTSUP;
+                   (pthread_attr_t *__restrict attr,
+                    const struct sched_param *__restrict param)) {
+  LIBC_CRASH_ON_NULLPTR(attr);
+  LIBC_CRASH_ON_NULLPTR(param);
+
+  attr->__schedparam = *param;
+  return 0;
 }
 
 } // namespace LIBC_NAMESPACE_DECL

diff  --git a/libc/src/pthread/pthread_attr_setschedparam.h b/libc/src/pthread/pthread_attr_setschedparam.h
index cd8787f591a52..2d915f9f1d780 100644
--- a/libc/src/pthread/pthread_attr_setschedparam.h
+++ b/libc/src/pthread/pthread_attr_setschedparam.h
@@ -1,21 +1,27 @@
-//===-- Implementation header for pthread_attr_setschedparam -*- C++ -*-===//
+//===----------------------------------------------------------------------===//
 //
 // 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
+/// Implementation header for pthread_attr_setschedparam.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_SETSCHEDPARAM_H
 #define LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_SETSCHEDPARAM_H
 
+#include "hdr/types/pthread_attr_t.h"
+#include "hdr/types/struct_sched_param.h"
 #include "src/__support/macros/config.h"
-#include <pthread.h>
 
 namespace LIBC_NAMESPACE_DECL {
 
 int pthread_attr_setschedparam(pthread_attr_t *__restrict attr,
-                               const struct sched_param *__restrict schedparam);
+                               const struct sched_param *__restrict param);
 
 } // namespace LIBC_NAMESPACE_DECL
 

diff  --git a/libc/test/src/pthread/CMakeLists.txt b/libc/test/src/pthread/CMakeLists.txt
index 966f990aa06f3..d6dd107b74ef6 100644
--- a/libc/test/src/pthread/CMakeLists.txt
+++ b/libc/test/src/pthread/CMakeLists.txt
@@ -18,6 +18,8 @@ add_libc_test(
     libc.src.pthread.pthread_attr_setguardsize
     libc.src.pthread.pthread_attr_setstacksize
     libc.src.pthread.pthread_attr_setstack
+    libc.src.pthread.pthread_attr_getschedparam
+    libc.src.pthread.pthread_attr_setschedparam
     libc.src.pthread.pthread_attr_getschedpolicy
     libc.src.pthread.pthread_attr_setschedpolicy
     libc.src.pthread.pthread_attr_getscope
@@ -25,6 +27,7 @@ add_libc_test(
     libc.hdr.errno_macros
     libc.hdr.pthread_macros
     libc.hdr.sched_macros
+    libc.hdr.types.struct_sched_param
 )
 
 add_libc_test(

diff  --git a/libc/test/src/pthread/pthread_attr_test.cpp b/libc/test/src/pthread/pthread_attr_test.cpp
index a178068ebf015..d371705948dfd 100644
--- a/libc/test/src/pthread/pthread_attr_test.cpp
+++ b/libc/test/src/pthread/pthread_attr_test.cpp
@@ -14,9 +14,11 @@
 #include "hdr/errno_macros.h"
 #include "hdr/pthread_macros.h"
 #include "hdr/sched_macros.h"
+#include "hdr/types/struct_sched_param.h"
 #include "src/pthread/pthread_attr_destroy.h"
 #include "src/pthread/pthread_attr_getdetachstate.h"
 #include "src/pthread/pthread_attr_getguardsize.h"
+#include "src/pthread/pthread_attr_getschedparam.h"
 #include "src/pthread/pthread_attr_getschedpolicy.h"
 #include "src/pthread/pthread_attr_getscope.h"
 #include "src/pthread/pthread_attr_getstack.h"
@@ -24,6 +26,7 @@
 #include "src/pthread/pthread_attr_init.h"
 #include "src/pthread/pthread_attr_setdetachstate.h"
 #include "src/pthread/pthread_attr_setguardsize.h"
+#include "src/pthread/pthread_attr_setschedparam.h"
 #include "src/pthread/pthread_attr_setschedpolicy.h"
 #include "src/pthread/pthread_attr_setscope.h"
 #include "src/pthread/pthread_attr_setstack.h"
@@ -129,6 +132,31 @@ TEST(LlvmLibcPThreadattrTest, SetAndGetStack) {
   ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0);
 }
 
+TEST(LlvmLibcPThreadattrTest, SetAndGetSchedParam) {
+  pthread_attr_t attr;
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr), 0);
+
+  struct sched_param param;
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getschedparam(&attr, &param), 0);
+  ASSERT_EQ(param.sched_priority, 0);
+
+  param.sched_priority = 42;
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setschedparam(&attr, &param), 0);
+  param.sched_priority = 0;
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getschedparam(&attr, &param), 0);
+  ASSERT_EQ(param.sched_priority, 42);
+
+  // We do not attempt to validate scheduling parameters here. The OS will do
+  // that when starting a thread.
+  param.sched_priority = -1;
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setschedparam(&attr, &param), 0);
+  param.sched_priority = 0;
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getschedparam(&attr, &param), 0);
+  ASSERT_EQ(param.sched_priority, -1);
+
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0);
+}
+
 TEST(LlvmLibcPThreadattrTest, SetAndGetSchedPolicy) {
   pthread_attr_t attr;
   ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr), 0);


        


More information about the libc-commits mailing list