[libc-commits] [libc] [llvm] [libc] Moves __cxa_thread_atexit_impl into proper entrypoint (PR #229347)
via libc-commits
libc-commits at lists.llvm.org
Tue Oct 6 02:05:00 PDT 2026
https://github.com/shri-acha created https://github.com/llvm/llvm-project/pull/229347
This PR resolves (#224664).
>From 289b2bd0f907beb3a00aa85698c91116e50097ea Mon Sep 17 00:00:00 2001
From: Shriharsh Acharya <happy.irhs at gmail.com>
Date: Tue, 6 Oct 2026 14:46:39 +0545
Subject: [PATCH] feat: introduce __cxa_thread_atexit_impl into as a libc entry
point
---
CMakeFiles/Progress/count.txt | 1 +
libc/config/linux/x86_64/entrypoints.txt | 1 +
libc/src/__support/threads/thread.cpp | 8 ++---
libc/src/__support/threads/thread.h | 8 +++--
libc/src/threads/CMakeLists.txt | 10 ++++++
libc/src/threads/__cxa_thread_atexit_impl.cpp | 33 +++++++++++++++++++
libc/src/threads/__cxa_thread_atexit_impl.h | 22 +++++++++++++
7 files changed, 76 insertions(+), 7 deletions(-)
create mode 100644 CMakeFiles/Progress/count.txt
create mode 100644 libc/src/threads/__cxa_thread_atexit_impl.cpp
create mode 100644 libc/src/threads/__cxa_thread_atexit_impl.h
diff --git a/CMakeFiles/Progress/count.txt b/CMakeFiles/Progress/count.txt
new file mode 100644
index 0000000000000..38b10c1b2badd
--- /dev/null
+++ b/CMakeFiles/Progress/count.txt
@@ -0,0 +1 @@
+68
diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 7928a35a6284a..48326462a1d99 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -1592,6 +1592,7 @@ if(LLVM_LIBC_FULL_BUILD)
libc.src.search.twalk_r
# threads.h entrypoints
+ libc.src.threads.__cxa_thread_atexit_impl
libc.src.threads.call_once
libc.src.threads.cnd_broadcast
libc.src.threads.cnd_destroy
diff --git a/libc/src/__support/threads/thread.cpp b/libc/src/__support/threads/thread.cpp
index 950dea837d0f9..9644c50c9793a 100644
--- a/libc/src/__support/threads/thread.cpp
+++ b/libc/src/__support/threads/thread.cpp
@@ -139,17 +139,15 @@ class ThreadAtExitCallbackMgr {
static LIBC_THREAD_LOCAL ThreadAtExitCallbackMgr atexit_callback_mgr;
+int add_thread_atexit_callback(AtExitCallback *callback, void *obj) {
+ return atexit_callback_mgr.add_callback(callback, obj);
+}
// The function __cxa_thread_atexit is provided by C++ runtimes like libcxxabi.
// It is used by thread local object runtime to register destructor calls. To
// actually register destructor call with the threading library, it calls
// __cxa_thread_atexit_impl, which is to be provided by the threading library.
// The semantics are very similar to the __cxa_atexit function except for the
// fact that the registered callback is thread specific.
-extern "C" int __cxa_thread_atexit_impl(AtExitCallback *callback, void *obj,
- void *) {
- return atexit_callback_mgr.add_callback(callback, obj);
-}
-
namespace internal {
void call_atexit_callbacks() {
diff --git a/libc/src/__support/threads/thread.h b/libc/src/__support/threads/thread.h
index 56cca8e3c915c..746e1dcda13a7 100644
--- a/libc/src/__support/threads/thread.h
+++ b/libc/src/__support/threads/thread.h
@@ -175,11 +175,15 @@ namespace internal {
// implementing the thread_exit function.
void call_atexit_callbacks();
-} // namespace internal
+}//internal namespace
+
+using AtExitCallback = void(void *);
+
+int add_thread_atexit_callback(AtExitCallback *callback, void *obj);
LIBC_INLINE Thread current_thread() {
return Thread(get_current_thread_attrib());
-}
+ }
} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/threads/CMakeLists.txt b/libc/src/threads/CMakeLists.txt
index 56247204c30f9..90ddbaadd5190 100644
--- a/libc/src/threads/CMakeLists.txt
+++ b/libc/src/threads/CMakeLists.txt
@@ -179,6 +179,16 @@ add_entrypoint_object(
libc.src.__support.threads.mutex
)
+add_entrypoint_object(
+ __cxa_thread_atexit_impl
+ SRCS
+ __cxa_thread_atexit_impl.cpp
+ HDRS
+ __cxa_thread_atexit_impl.h
+ DEPENDS
+ libc.src.__support.threads.thread
+)
+
add_entrypoint_object(
cnd_init
ALIAS
diff --git a/libc/src/threads/__cxa_thread_atexit_impl.cpp b/libc/src/threads/__cxa_thread_atexit_impl.cpp
new file mode 100644
index 0000000000000..67b387ab76fdb
--- /dev/null
+++ b/libc/src/threads/__cxa_thread_atexit_impl.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 __cxa_thread_atexit_impl
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/threads/__cxa_thread_atexit_impl.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/threads/thread.h"
+
+#include <threads.h>
+
+namespace LIBC_NAMESPACE_DECL {
+
+// The function __cxa_thread_atexit is provided by C++ runtimes like libcxxabi.
+// It is used by thread local object runtime to register destructor calls. To
+// actually register destructor call with the threading library, it calls
+// __cxa_thread_atexit_impl, which is to be provided by the threading library.
+// The semantics are very similar to the __cxa_atexit function except for the
+// fact that the registered callback is thread specific.
+LLVM_LIBC_FUNCTION(int, __cxa_thread_atexit_impl,
+ (AtExitCallback* callback, void *obj, void *)) {
+ return add_thread_atexit_callback(callback, obj);
+}
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/threads/__cxa_thread_atexit_impl.h b/libc/src/threads/__cxa_thread_atexit_impl.h
new file mode 100644
index 0000000000000..729498b409a82
--- /dev/null
+++ b/libc/src/threads/__cxa_thread_atexit_impl.h
@@ -0,0 +1,22 @@
+//===-- Implementation header for __cxa_thread_atexit_impl function ------------*- 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___THREADS_CXA_THREAD_ATEXIT_IMPL_H
+#define LLVM_LIBC_SRC___THREADS_CXA_THREAD_ATEXIT_IMPL_H
+
+#include "src/__support/macros/config.h"
+#include "src/__support/threads/thread.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int __cxa_thread_atexit_impl(AtExitCallback *callback, void *obj,
+ void *);
+}// namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC_THREADS_CXA_THREAD_ATEXIT_IMPL_H
+
More information about the libc-commits
mailing list