[libc-commits] [libc] [libc] Implement pthread_getattr_np (PR #221231)

Alexey Samsonov via libc-commits libc-commits at lists.llvm.org
Fri Sep 4 09:04:51 PDT 2026


================
@@ -0,0 +1,358 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+/// Integration tests for pthread_getattr_np.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/pthread_macros.h"
+#include "hdr/stdint_proxy.h"
+#include "hdr/sys_mman_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_getstack.h"
+#include "src/pthread/pthread_attr_init.h"
+#include "src/pthread/pthread_attr_setdetachstate.h"
+#include "src/pthread/pthread_attr_setstack.h"
+#include "src/pthread/pthread_barrier_destroy.h"
+#include "src/pthread/pthread_barrier_init.h"
+#include "src/pthread/pthread_barrier_wait.h"
+#include "src/pthread/pthread_create.h"
+#include "src/pthread/pthread_detach.h"
+#include "src/pthread/pthread_getattr_np.h"
+#include "src/pthread/pthread_join.h"
+#include "src/pthread/pthread_self.h"
+#include "src/sys/mman/mmap.h"
+#include "src/sys/mman/munmap.h"
+#include "src/unistd/sysconf.h"
+#include "test/IntegrationTest/test.h"
+
+static void check_readable(const void *start, size_t size) {
+  size_t pagesize = LIBC_NAMESPACE::sysconf(_SC_PAGESIZE);
+  auto *bytes = static_cast<const volatile char *>(start);
+  for (size_t offset = 0; offset < size; offset += pagesize)
+    (void)bytes[offset];
+  if (size > 0)
+    (void)bytes[size - 1];
+}
+
+static void wait_barrier(pthread_barrier_t &barrier) {
+  int res = LIBC_NAMESPACE::pthread_barrier_wait(&barrier);
+  ASSERT_TRUE(res == 0 || res == PTHREAD_BARRIER_SERIAL_THREAD);
+}
+
+// Test 1: Main thread attributes
+// Verifies that pthread_getattr_np on the main thread reports a detached state,
+// a dynamic stack size (PTHREAD_STACK_DYNAMIC_NP), and a zero guard size.
+static void test_main_thread() {
+  pthread_attr_t attr;
+  ASSERT_EQ(
+      LIBC_NAMESPACE::pthread_getattr_np(LIBC_NAMESPACE::pthread_self(), &attr),
+      0);
+
+  int detachstate = -1;
+  ASSERT_EQ(LIBC_NAMESPACE::pthread_attr_getdetachstate(&attr, &detachstate),
+            0);
+  ASSERT_EQ(detachstate, static_cast<int>(PTHREAD_CREATE_DETACHED));
+
+  void *stackaddr = nullptr;
+  size_t stacksize = 1234;
+  ASSERT_EQ(
+      LIBC_NAMESPACE::pthread_attr_getstack(&attr, &stackaddr, &stacksize), 0);
+  ASSERT_NE(stackaddr, static_cast<void *>(nullptr));
+  ASSERT_EQ(stacksize, static_cast<size_t>(PTHREAD_STACK_DYNAMIC_NP));
+  ASSERT_EQ(reinterpret_cast<uintptr_t>(stackaddr) %
+                LIBC_NAMESPACE::sysconf(_SC_PAGESIZE),
----------------
vonosmas wrote:

Can we have a global variable with the system-wide page size value?

https://github.com/llvm/llvm-project/pull/221231


More information about the libc-commits mailing list