[libc-commits] [libc] [libc] Implement pthread_getattr_np (PR #221231)
Pavel Labath via libc-commits
libc-commits at lists.llvm.org
Mon Sep 7 02:12:07 PDT 2026
================
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 the pthread_getattr_np function (GNU extension).
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/pthread/pthread_getattr_np.h"
+#include "hdr/pthread_macros.h"
+#include "hdr/types/pthread_attr_t.h"
+#include "hdr/types/pthread_t.h"
+#include "src/__support/CPP/atomic.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+#include "src/__support/threads/thread.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+static_assert(sizeof(pthread_t) == sizeof(LIBC_NAMESPACE::Thread),
+ "Mismatch between pthread_t and internal Thread.");
+
+LLVM_LIBC_FUNCTION(int, pthread_getattr_np,
+ (pthread_t th, pthread_attr_t *attr)) {
+ LIBC_CRASH_ON_NULLPTR(attr);
+ auto *thread = reinterpret_cast<Thread *>(&th);
+
+ uint32_t detach_state =
+ thread->attrib->detach_state.load(cpp::MemoryOrder::RELAXED);
+ attr->__detachstate =
+ (detach_state == static_cast<uint32_t>(DetachState::DETACHED))
----------------
labath wrote:
Thanks for pointing this out. The code is more subtle than I realized. For an EXITING thread, we just don't know whether it was detached or not before it started exiting. Although that is something one might want to know (and we could conceivably provide it by storing the pre-exiting state in another member), the bigger problem is that there's no way to read that variable. As a part of exit, the ThreadAttributes object is going to get destroyed, so if this code was just a little slower, it would crash instead of seeing the EXITING state.
With that in mind, I'm going to change this bit to make the states more explicit and make the exiting state __builtin_unreachable().
https://github.com/llvm/llvm-project/pull/221231
More information about the libc-commits
mailing list