[libc-commits] [libc] [libc] Implement pthread_attr_[gs]etscope (PR #222956)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Fri Sep 11 07:31:58 PDT 2026


https://github.com/labath created https://github.com/llvm/llvm-project/pull/222956

This patch implements pthread_attr_getscope and pthread_attr_setscope.

Same as other libraries, we only support the system contention scope (PTHREAD_SCOPE_SYSTEM). Process contention scope (PTHREAD_SCOPE_PROCESS) is not supported, and would basically require a userspace N:M threading implementation. Since this value cannot be checked in the kernel, I reject it directly during the attribute set operation, and don't even bother storing it in the attribute object.

Assisted-by: Gemini

>From fe86122ea65b452e38fc8b5b8ed40f4f9b6b45f1 Mon Sep 17 00:00:00 2001
From: Pavel Labath <pavel at labath.sk>
Date: Fri, 11 Sep 2026 11:10:44 +0000
Subject: [PATCH] [libc] Implement pthread_attr_[gs]etscope

This patch implements pthread_attr_getscope and pthread_attr_setscope.

Same as other libraries, we only support the system contention scope
(PTHREAD_SCOPE_SYSTEM). Process contention scope (PTHREAD_SCOPE_PROCESS)
is not supported, and would basically require a userspace N:M threading
implementation. Since this value cannot be checked in the kernel, I
reject it directly during the attribute set operation, and don't even
bother storing it in the attribute object.

Assisted-by: Gemini
---
 libc/config/linux/aarch64/entrypoints.txt     |  2 +
 libc/config/linux/riscv/entrypoints.txt       |  2 +
 libc/config/linux/x86_64/entrypoints.txt      |  2 +
 .../include/llvm-libc-macros/pthread-macros.h |  3 ++
 libc/include/pthread.yaml                     | 18 +++++++++
 libc/src/pthread/CMakeLists.txt               | 29 ++++++++++++++
 libc/src/pthread/pthread_attr_getscope.cpp    | 33 ++++++++++++++++
 libc/src/pthread/pthread_attr_getscope.h      | 28 +++++++++++++
 libc/src/pthread/pthread_attr_setscope.cpp    | 39 +++++++++++++++++++
 libc/src/pthread/pthread_attr_setscope.h      | 27 +++++++++++++
 libc/test/src/pthread/CMakeLists.txt          |  3 ++
 libc/test/src/pthread/pthread_attr_test.cpp   | 39 ++++++++++++++++++-
 12 files changed, 224 insertions(+), 1 deletion(-)
 create mode 100644 libc/src/pthread/pthread_attr_getscope.cpp
 create mode 100644 libc/src/pthread/pthread_attr_getscope.h
 create mode 100644 libc/src/pthread/pthread_attr_setscope.cpp
 create mode 100644 libc/src/pthread/pthread_attr_setscope.h

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index f7b2c3059df43..95143f08d89ae 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -1120,12 +1120,14 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.pthread.pthread_attr_getdetachstate
     libc.src.pthread.pthread_attr_getguardsize
     libc.src.pthread.pthread_attr_getschedpolicy
+    libc.src.pthread.pthread_attr_getscope
     libc.src.pthread.pthread_attr_getstack
     libc.src.pthread.pthread_attr_getstacksize
     libc.src.pthread.pthread_attr_init
     libc.src.pthread.pthread_attr_setdetachstate
     libc.src.pthread.pthread_attr_setguardsize
     libc.src.pthread.pthread_attr_setschedpolicy
+    libc.src.pthread.pthread_attr_setscope
     libc.src.pthread.pthread_attr_setstack
     libc.src.pthread.pthread_attr_setstacksize
     libc.src.pthread.pthread_condattr_destroy
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 2cabbc7031f22..6eb4b6372fdd7 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -1306,12 +1306,14 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.pthread.pthread_attr_getdetachstate
     libc.src.pthread.pthread_attr_getguardsize
     libc.src.pthread.pthread_attr_getschedpolicy
+    libc.src.pthread.pthread_attr_getscope
     libc.src.pthread.pthread_attr_getstack
     libc.src.pthread.pthread_attr_getstacksize
     libc.src.pthread.pthread_attr_init
     libc.src.pthread.pthread_attr_setdetachstate
     libc.src.pthread.pthread_attr_setguardsize
     libc.src.pthread.pthread_attr_setschedpolicy
+    libc.src.pthread.pthread_attr_setscope
     libc.src.pthread.pthread_attr_setstack
     libc.src.pthread.pthread_attr_setstacksize
     libc.src.pthread.pthread_condattr_destroy
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index bba74cf8907f9..983ec921080dd 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1317,12 +1317,14 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.pthread.pthread_attr_getdetachstate
     libc.src.pthread.pthread_attr_getguardsize
     libc.src.pthread.pthread_attr_getschedpolicy
+    libc.src.pthread.pthread_attr_getscope
     libc.src.pthread.pthread_attr_getstack
     libc.src.pthread.pthread_attr_getstacksize
     libc.src.pthread.pthread_attr_init
     libc.src.pthread.pthread_attr_setdetachstate
     libc.src.pthread.pthread_attr_setguardsize
     libc.src.pthread.pthread_attr_setschedpolicy
+    libc.src.pthread.pthread_attr_setscope
     libc.src.pthread.pthread_attr_setstack
     libc.src.pthread.pthread_attr_setstacksize
     libc.src.pthread.pthread_condattr_destroy
diff --git a/libc/include/llvm-libc-macros/pthread-macros.h b/libc/include/llvm-libc-macros/pthread-macros.h
index 58717200b5fd6..1d79383c8cbf9 100644
--- a/libc/include/llvm-libc-macros/pthread-macros.h
+++ b/libc/include/llvm-libc-macros/pthread-macros.h
@@ -29,6 +29,9 @@
 #define PTHREAD_PROCESS_PRIVATE 0
 #define PTHREAD_PROCESS_SHARED 1
 
+#define PTHREAD_SCOPE_SYSTEM 0
+#define PTHREAD_SCOPE_PROCESS 1
+
 #ifdef __linux__
 #define PTHREAD_MUTEX_INITIALIZER                                              \
   {                                                                            \
diff --git a/libc/include/pthread.yaml b/libc/include/pthread.yaml
index 0059eab00f2f7..a13a7b400ca1a 100644
--- a/libc/include/pthread.yaml
+++ b/libc/include/pthread.yaml
@@ -28,6 +28,10 @@ macros:
     macro_header: pthread-macros.h
   - macro_name: "PTHREAD_PROCESS_SHARED"
     macro_header: pthread-macros.h
+  - macro_name: "PTHREAD_SCOPE_SYSTEM"
+    macro_header: pthread-macros.h
+  - macro_name: "PTHREAD_SCOPE_PROCESS"
+    macro_header: pthread-macros.h
   - macro_name: "PTHREAD_MUTEX_INITIALIZER"
     macro_header: pthread-macros.h
   - macro_name: "PTHREAD_COND_INITIALIZER"
@@ -102,6 +106,13 @@ functions:
     arguments:
       - type: const pthread_attr_t *__restrict
       - type: int *__restrict
+  - name: pthread_attr_getscope
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: const pthread_attr_t *__restrict
+      - type: int *__restrict
   - name: pthread_attr_getstack
     return_type: int
     arguments:
@@ -137,6 +148,13 @@ functions:
     arguments:
       - type: pthread_attr_t *
       - type: int
+  - name: pthread_attr_setscope
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: pthread_attr_t *
+      - type: int
   - name: pthread_attr_setstack
     return_type: int
     arguments:
diff --git a/libc/src/pthread/CMakeLists.txt b/libc/src/pthread/CMakeLists.txt
index 06bea3bb9887f..9bcc1d1d85b79 100644
--- a/libc/src/pthread/CMakeLists.txt
+++ b/libc/src/pthread/CMakeLists.txt
@@ -84,6 +84,20 @@ add_entrypoint_object(
     libc.src.__support.macros.null_check
 )
 
+add_entrypoint_object(
+  pthread_attr_getscope
+  SRCS
+    pthread_attr_getscope.cpp
+  HDRS
+    pthread_attr_getscope.h
+  DEPENDS
+    libc.hdr.pthread_macros
+    libc.hdr.types.pthread_attr_t
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.macros.null_check
+)
+
 add_entrypoint_object(
   pthread_attr_setschedparam
   SRCS
@@ -107,6 +121,21 @@ add_entrypoint_object(
     libc.src.__support.macros.null_check
 )
 
+add_entrypoint_object(
+  pthread_attr_setscope
+  SRCS
+    pthread_attr_setscope.cpp
+  HDRS
+    pthread_attr_setscope.h
+  DEPENDS
+    libc.hdr.errno_macros
+    libc.hdr.pthread_macros
+    libc.hdr.types.pthread_attr_t
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.__support.macros.null_check
+)
+
 add_entrypoint_object(
   pthread_attr_getstacksize
   SRCS
diff --git a/libc/src/pthread/pthread_attr_getscope.cpp b/libc/src/pthread/pthread_attr_getscope.cpp
new file mode 100644
index 0000000000000..8fc22f9e0fa6b
--- /dev/null
+++ b/libc/src/pthread/pthread_attr_getscope.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_getscope.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/pthread/pthread_attr_getscope.h"
+#include "hdr/pthread_macros.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, pthread_attr_getscope,
+                   ([[maybe_unused]] const pthread_attr_t *__restrict attr,
+                    int *__restrict contentionscope)) {
+  LIBC_CRASH_ON_NULLPTR(attr);
+  LIBC_CRASH_ON_NULLPTR(contentionscope);
+
+  *contentionscope = PTHREAD_SCOPE_SYSTEM;
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
diff --git a/libc/src/pthread/pthread_attr_getscope.h b/libc/src/pthread/pthread_attr_getscope.h
new file mode 100644
index 0000000000000..11123c07ccfa1
--- /dev/null
+++ b/libc/src/pthread/pthread_attr_getscope.h
@@ -0,0 +1,28 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_getscope.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_GETSCOPE_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_GETSCOPE_H
+
+#include "hdr/types/pthread_attr_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int pthread_attr_getscope(const pthread_attr_t *__restrict attr,
+                          int *__restrict contentionscope);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_GETSCOPE_H
+
diff --git a/libc/src/pthread/pthread_attr_setscope.cpp b/libc/src/pthread/pthread_attr_setscope.cpp
new file mode 100644
index 0000000000000..6e30d45023089
--- /dev/null
+++ b/libc/src/pthread/pthread_attr_setscope.cpp
@@ -0,0 +1,39 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_setscope.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/pthread/pthread_attr_setscope.h"
+#include "hdr/errno_macros.h"
+#include "hdr/pthread_macros.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, pthread_attr_setscope,
+                   ([[maybe_unused]] pthread_attr_t *attr,
+                    int contentionscope)) {
+  LIBC_CRASH_ON_NULLPTR(attr);
+
+  switch (contentionscope) {
+  case PTHREAD_SCOPE_SYSTEM:
+    return 0;
+  case PTHREAD_SCOPE_PROCESS:
+    return ENOTSUP;
+  default:
+    return EINVAL;
+  }
+}
+
+} // namespace LIBC_NAMESPACE_DECL
+
diff --git a/libc/src/pthread/pthread_attr_setscope.h b/libc/src/pthread/pthread_attr_setscope.h
new file mode 100644
index 0000000000000..cbd057ee8ba5c
--- /dev/null
+++ b/libc/src/pthread/pthread_attr_setscope.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// 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_setscope.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_SETSCOPE_H
+#define LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_SETSCOPE_H
+
+#include "hdr/types/pthread_attr_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_PTHREAD_PTHREAD_ATTR_SETSCOPE_H
+
diff --git a/libc/test/src/pthread/CMakeLists.txt b/libc/test/src/pthread/CMakeLists.txt
index 391a634036226..966f990aa06f3 100644
--- a/libc/test/src/pthread/CMakeLists.txt
+++ b/libc/test/src/pthread/CMakeLists.txt
@@ -20,7 +20,10 @@ add_libc_test(
     libc.src.pthread.pthread_attr_setstack
     libc.src.pthread.pthread_attr_getschedpolicy
     libc.src.pthread.pthread_attr_setschedpolicy
+    libc.src.pthread.pthread_attr_getscope
+    libc.src.pthread.pthread_attr_setscope
     libc.hdr.errno_macros
+    libc.hdr.pthread_macros
     libc.hdr.sched_macros
 )
 
diff --git a/libc/test/src/pthread/pthread_attr_test.cpp b/libc/test/src/pthread/pthread_attr_test.cpp
index db1dcc0a4866c..a178068ebf015 100644
--- a/libc/test/src/pthread/pthread_attr_test.cpp
+++ b/libc/test/src/pthread/pthread_attr_test.cpp
@@ -1,23 +1,31 @@
-//===-- Unittests for pthread_attr_t --------------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
+/// Unittests for pthread_attr_t.
+///
+//===----------------------------------------------------------------------===//
 
 #include "hdr/errno_macros.h"
+#include "hdr/pthread_macros.h"
 #include "hdr/sched_macros.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_getschedpolicy.h"
+#include "src/pthread/pthread_attr_getscope.h"
 #include "src/pthread/pthread_attr_getstack.h"
 #include "src/pthread/pthread_attr_getstacksize.h"
 #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_setschedpolicy.h"
+#include "src/pthread/pthread_attr_setscope.h"
 #include "src/pthread/pthread_attr_setstack.h"
 #include "src/pthread/pthread_attr_setstacksize.h"
 
@@ -153,3 +161,32 @@ TEST(LlvmLibcPThreadattrTest, SetAndGetSchedPolicy) {
 
   ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0);
 }
+
+TEST(LlvmLibcPThreadAttrTest, SetAndGetScope) {
+  pthread_attr_t attr;
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_init(&attr), 0);
+
+  int scope;
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getscope(&attr, &scope), 0);
+  ASSERT_EQ(scope, PTHREAD_SCOPE_SYSTEM);
+
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM),
+            0);
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getscope(&attr, &scope), 0);
+  ASSERT_EQ(scope, PTHREAD_SCOPE_SYSTEM);
+
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS),
+            ENOTSUP);
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getscope(&attr, &scope), 0);
+  ASSERT_EQ(scope, PTHREAD_SCOPE_SYSTEM);
+
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setscope(&attr, 0xBAD), EINVAL);
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getscope(&attr, &scope), 0);
+  ASSERT_EQ(scope, PTHREAD_SCOPE_SYSTEM);
+
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_setscope(&attr, -1), EINVAL);
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getscope(&attr, &scope), 0);
+  ASSERT_EQ(scope, PTHREAD_SCOPE_SYSTEM);
+
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_destroy(&attr), 0);
+}



More information about the libc-commits mailing list