[libc-commits] [libc] [libc] Fix callback type in `exit_handlers.cpp` not matching (PR #97642)
Joseph Huber via libc-commits
libc-commits at lists.llvm.org
Wed Jul 3 14:30:53 PDT 2024
https://github.com/jhuber6 created https://github.com/llvm/llvm-project/pull/97642
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.
>From 01fd82e9e923399f0c82f84d7c057d856169c244 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Wed, 3 Jul 2024 16:28:08 -0500
Subject: [PATCH] [libc] Fix callback type in `exit_handlers.cpp` not matching
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.
---
libc/src/stdlib/CMakeLists.txt | 12 ++++-----
libc/src/stdlib/at_quick_exit.cpp | 2 ++
libc/src/stdlib/atexit.cpp | 3 +++
libc/src/stdlib/exit_handler.cpp | 42 -------------------------------
libc/src/stdlib/exit_handler.h | 24 +++++++++++++++---
5 files changed, 32 insertions(+), 51 deletions(-)
delete mode 100644 libc/src/stdlib/exit_handler.cpp
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 677bf358c82c4a..3913db7938fdd5 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 752d67e7fe44dc..ada845633c825d 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 ca3cbfe87a88c5..fabe6524903f97 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 ed41247e4a31df..00000000000000
--- 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 8494c2f2e526e9..97657cf5b3fd61 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
More information about the libc-commits
mailing list