[libc-commits] [libc] [libc] Fix callback type in `exit_handlers.cpp` not matching (PR #97642)
via libc-commits
libc-commits at lists.llvm.org
Wed Jul 3 14:31:25 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Joseph Huber (jhuber6)
<details>
<summary>Changes</summary>
Summary:
This file is an object library, but uses the `LIBC_COPT_PUBLIC_PACKAING`
option. This will always be undefined which leads to a type mismatch
when uses actually try to link against it. This patch simply removes
this and turns it into a header only library. This means that the
implementations of the callback lists and the mutexes need to live in
their respective files. The result is that `atexit` needs to be defined
for `at_quick_exit` to be valid.
---
Full diff: https://github.com/llvm/llvm-project/pull/97642.diff
5 Files Affected:
- (modified) libc/src/stdlib/CMakeLists.txt (+6-6)
- (modified) libc/src/stdlib/at_quick_exit.cpp (+2)
- (modified) libc/src/stdlib/atexit.cpp (+3)
- (removed) libc/src/stdlib/exit_handler.cpp (-42)
- (modified) libc/src/stdlib/exit_handler.h (+21-3)
``````````diff
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 677bf358c82c4..3913db7938fdd 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -458,15 +458,11 @@ add_entrypoint_object(
# TODO: Move all exit functions to linux specific
-if (TARGET libc.src.__support.threads.mutex)
-add_object_library(
+if(TARGET libc.src.__support.threads.mutex)
+add_header_library(
exit_handler
- SRCS
- exit_handler.cpp
HDRS
exit_handler.h
- CXX_STANDARD
- 20 # For constinit
DEPENDS
libc.src.__support.CPP.mutex
libc.src.__support.CPP.new
@@ -483,6 +479,8 @@ add_entrypoint_object(
atexit.cpp
HDRS
atexit.h
+ CXX_STANDARD
+ 20 # For constinit
DEPENDS
.exit_handler
)
@@ -493,6 +491,8 @@ add_entrypoint_object(
at_quick_exit.cpp
HDRS
at_quick_exit.h
+ CXX_STANDARD
+ 20 # For constinit
DEPENDS
.exit_handler
)
diff --git a/libc/src/stdlib/at_quick_exit.cpp b/libc/src/stdlib/at_quick_exit.cpp
index 752d67e7fe44d..ada845633c825 100644
--- a/libc/src/stdlib/at_quick_exit.cpp
+++ b/libc/src/stdlib/at_quick_exit.cpp
@@ -13,6 +13,8 @@
namespace LIBC_NAMESPACE {
+constinit ExitCallbackList at_quick_exit_callbacks;
+
LLVM_LIBC_FUNCTION(int, at_quick_exit, (__atexithandler_t callback)) {
return add_atexit_unit(
at_quick_exit_callbacks,
diff --git a/libc/src/stdlib/atexit.cpp b/libc/src/stdlib/atexit.cpp
index ca3cbfe87a88c..fabe6524903f9 100644
--- a/libc/src/stdlib/atexit.cpp
+++ b/libc/src/stdlib/atexit.cpp
@@ -13,6 +13,9 @@
namespace LIBC_NAMESPACE {
+constinit ExitCallbackList atexit_callbacks;
+Mutex handler_list_mtx(false, false, false, false);
+
extern "C" {
int __cxa_atexit(AtExitCallback *callback, void *payload, void *) {
diff --git a/libc/src/stdlib/exit_handler.cpp b/libc/src/stdlib/exit_handler.cpp
deleted file mode 100644
index ed41247e4a31d..0000000000000
--- a/libc/src/stdlib/exit_handler.cpp
+++ /dev/null
@@ -1,42 +0,0 @@
-//===--- Implementation of exit_handler------------------------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#include "src/stdlib/exit_handler.h"
-#include "src/__support/CPP/mutex.h" // lock_guard
-
-namespace LIBC_NAMESPACE {
-
-constinit ExitCallbackList at_quick_exit_callbacks;
-constinit ExitCallbackList atexit_callbacks;
-
-Mutex handler_list_mtx(false, false, false, false);
-
-void stdc_at_exit_func(void *payload) {
- reinterpret_cast<StdCAtExitCallback *>(payload)();
-}
-
-void call_exit_callbacks(ExitCallbackList &callbacks) {
- handler_list_mtx.lock();
- while (!callbacks.empty()) {
- AtExitUnit &unit = callbacks.back();
- callbacks.pop_back();
- handler_list_mtx.unlock();
- unit.callback(unit.payload);
- handler_list_mtx.lock();
- }
- ExitCallbackList::destroy(&callbacks);
-}
-
-int add_atexit_unit(ExitCallbackList &callbacks, const AtExitUnit &unit) {
- cpp::lock_guard lock(handler_list_mtx);
- if (callbacks.push_back(unit))
- return 0;
- return -1;
-}
-
-} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdlib/exit_handler.h b/libc/src/stdlib/exit_handler.h
index 8494c2f2e526e..97657cf5b3fd6 100644
--- a/libc/src/stdlib/exit_handler.h
+++ b/libc/src/stdlib/exit_handler.h
@@ -42,11 +42,29 @@ extern ExitCallbackList at_quick_exit_callbacks;
extern Mutex handler_list_mtx;
-void stdc_at_exit_func(void *payload);
+LIBC_INLINE void stdc_at_exit_func(void *payload) {
+ reinterpret_cast<StdCAtExitCallback *>(payload)();
+}
-void call_exit_callbacks(ExitCallbackList &callbacks);
+LIBC_INLINE void call_exit_callbacks(ExitCallbackList &callbacks) {
+ handler_list_mtx.lock();
+ while (!callbacks.empty()) {
+ AtExitUnit &unit = callbacks.back();
+ callbacks.pop_back();
+ handler_list_mtx.unlock();
+ unit.callback(unit.payload);
+ handler_list_mtx.lock();
+ }
+ ExitCallbackList::destroy(&callbacks);
+}
-int add_atexit_unit(ExitCallbackList &callbacks, const AtExitUnit &unit);
+LIBC_INLINE int add_atexit_unit(ExitCallbackList &callbacks,
+ const AtExitUnit &unit) {
+ cpp::lock_guard lock(handler_list_mtx);
+ if (callbacks.push_back(unit))
+ return 0;
+ return -1;
+}
} // namespace LIBC_NAMESPACE
``````````
</details>
https://github.com/llvm/llvm-project/pull/97642
More information about the libc-commits
mailing list