[libc-commits] [libc] [libc] Define _POSIX_THREADS and support _SC_THREADS in sysconf (PR #201091)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Tue Jun 2 04:00:00 PDT 2026


https://github.com/kaladron created https://github.com/llvm/llvm-project/pull/201091

Defined the _POSIX_THREADS macro to 200809L in unistd-macros.h for Linux to signal POSIX thread support.

Also implemented runtime support for _SC_THREADS in sysconf, returning _POSIX_THREADS, and added _SC_THREADS and _POSIX_THREADS to the public unistd.yaml specification.

Added compile-time and runtime tests to verify _POSIX_THREADS definition and sysconf(_SC_THREADS) behavior. Standardized header comments in unistd-macros.h to conform to LLVM style.

Assisted-by: Automated tooling, human reviewed.

>From 8b14b62041b394fb048f24ec6597282dc59704b5 Mon Sep 17 00:00:00 2001
From: Jeff Bailey <jbailey at raspberryginger.com>
Date: Tue, 2 Jun 2026 11:40:02 +0100
Subject: [PATCH] [libc] Define _POSIX_THREADS and support _SC_THREADS in
 sysconf

Defined the _POSIX_THREADS macro to 200809L in unistd-macros.h for Linux
to signal POSIX thread support.

Also implemented runtime support for _SC_THREADS in sysconf, returning
_POSIX_THREADS, and added _SC_THREADS and _POSIX_THREADS to the public
unistd.yaml specification.

Added compile-time and runtime tests to verify _POSIX_THREADS definition
and sysconf(_SC_THREADS) behavior. Standardized header comments in
unistd-macros.h to conform to LLVM style.

Assisted-by: Automated tooling, human reviewed.
---
 .../llvm-libc-macros/linux/unistd-macros.h    |  9 +++++-
 libc/include/unistd.yaml                      |  4 +++
 libc/src/unistd/linux/sysconf.cpp             |  3 ++
 libc/test/include/CMakeLists.txt              | 11 ++++++++
 libc/test/include/unistd_macros_test.cpp      | 28 +++++++++++++++++++
 libc/test/src/unistd/sysconf_test.cpp         |  5 ++++
 6 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 libc/test/include/unistd_macros_test.cpp

diff --git a/libc/include/llvm-libc-macros/linux/unistd-macros.h b/libc/include/llvm-libc-macros/linux/unistd-macros.h
index ba1ae48f17b78..b04298ff60828 100644
--- a/libc/include/llvm-libc-macros/linux/unistd-macros.h
+++ b/libc/include/llvm-libc-macros/linux/unistd-macros.h
@@ -1,10 +1,15 @@
-//===-- Definition of macros from unistd.h --------------------------------===//
+//===----------------------------------------------------------------------===//
 //
 // 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
+/// Linux specific declarations of macros from unistd.h.
+///
+//===----------------------------------------------------------------------===//
 
 #ifndef LLVM_LIBC_MACROS_LINUX_UNISTD_MACROS_H
 #define LLVM_LIBC_MACROS_LINUX_UNISTD_MACROS_H
@@ -19,6 +24,7 @@
 #define _SC_PAGE_SIZE _SC_PAGESIZE
 #define _SC_NPROCESSORS_CONF 83
 #define _SC_NPROCESSORS_ONLN 84
+#define _SC_THREADS 67
 
 #define _PC_FILESIZEBITS 0
 #define _PC_LINK_MAX 1
@@ -43,6 +49,7 @@
 
 // TODO: Move these limit macros to a separate file
 #define _POSIX_CHOWN_RESTRICTED 1
+#define _POSIX_THREADS 200809L
 #define _POSIX_PIPE_BUF 512
 #define _POSIX_NO_TRUNC 1
 #define _POSIX_VDISABLE '\0'
diff --git a/libc/include/unistd.yaml b/libc/include/unistd.yaml
index fd35432418473..308b2a4b20f2b 100644
--- a/libc/include/unistd.yaml
+++ b/libc/include/unistd.yaml
@@ -22,6 +22,10 @@ macros:
     macro_header: unistd-macros.h
   - macro_name: _SC_NPROCESSORS_ONLN
     macro_header: unistd-macros.h
+  - macro_name: _SC_THREADS
+    macro_header: unistd-macros.h
+  - macro_name: _POSIX_THREADS
+    macro_header: unistd-macros.h
 types:
   - type_name: uid_t
   - type_name: gid_t
diff --git a/libc/src/unistd/linux/sysconf.cpp b/libc/src/unistd/linux/sysconf.cpp
index 0578d3a6c319c..dff64cb8140d3 100644
--- a/libc/src/unistd/linux/sysconf.cpp
+++ b/libc/src/unistd/linux/sysconf.cpp
@@ -36,6 +36,9 @@ LLVM_LIBC_FUNCTION(long, sysconf, (int name)) {
     return static_cast<long>(
         sysinfo::parse_nproc_with_fallback_from(sysinfo::ONLINE_NPROC_PATH));
 
+  if (name == _SC_THREADS)
+    return _POSIX_THREADS;
+
   // TODO: Complete the rest of the sysconf options.
   libc_errno = EINVAL;
   return -1;
diff --git a/libc/test/include/CMakeLists.txt b/libc/test/include/CMakeLists.txt
index 85915f5a2fbda..41d198824b385 100644
--- a/libc/test/include/CMakeLists.txt
+++ b/libc/test/include/CMakeLists.txt
@@ -21,6 +21,17 @@ add_libc_test(
     libc.include.llvm-libc-macros.complex_macros
 )
 
+add_libc_test(
+  unistd_macros_test
+  SUITE
+    libc_include_tests
+  SRCS
+    unistd_macros_test.cpp
+  DEPENDS
+    libc.include.llvm-libc-macros.unistd_macros
+)
+
+
 add_libc_test(
   sys_queue_test
   SUITE
diff --git a/libc/test/include/unistd_macros_test.cpp b/libc/test/include/unistd_macros_test.cpp
new file mode 100644
index 0000000000000..986b175a772e1
--- /dev/null
+++ b/libc/test/include/unistd_macros_test.cpp
@@ -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
+/// Unit tests for unistd-macros.
+///
+//===----------------------------------------------------------------------===//
+
+#include "include/llvm-libc-macros/unistd-macros.h"
+#include "test/UnitTest/Test.h"
+
+namespace LIBC_NAMESPACE {
+
+TEST(LlvmLibcUnistdMacrosTest, VersionMacros) {
+#ifdef __linux__
+#ifndef _POSIX_THREADS
+#error "_POSIX_THREADS is not defined on Linux"
+#endif
+  EXPECT_EQ(_POSIX_THREADS, 200809L);
+#endif
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/test/src/unistd/sysconf_test.cpp b/libc/test/src/unistd/sysconf_test.cpp
index e780e2c515585..0c37eef13b283 100644
--- a/libc/test/src/unistd/sysconf_test.cpp
+++ b/libc/test/src/unistd/sysconf_test.cpp
@@ -25,3 +25,8 @@ TEST(LlvmLibcSysconfTest, NprocessorsOnlnTest) {
   long sysconf_count = LIBC_NAMESPACE::sysconf(_SC_NPROCESSORS_ONLN);
   ASSERT_GT(sysconf_count, 0l);
 }
+
+TEST(LlvmLibcSysconfTest, ThreadsTest) {
+  long threads = LIBC_NAMESPACE::sysconf(_SC_THREADS);
+  ASSERT_EQ(threads, _POSIX_THREADS);
+}



More information about the libc-commits mailing list