[llvm] 129311a - [libSupport] make CallBacksToRun static local
Mehdi Amini via llvm-commits
llvm-commits at lists.llvm.org
Sat Mar 19 13:36:03 PDT 2022
Author: Tal Kedar
Date: 2022-03-19T20:31:04Z
New Revision: 129311ac0bbbea78d980cb44565c0daeab924511
URL: https://github.com/llvm/llvm-project/commit/129311ac0bbbea78d980cb44565c0daeab924511
DIFF: https://github.com/llvm/llvm-project/commit/129311ac0bbbea78d980cb44565c0daeab924511.diff
LOG: [libSupport] make CallBacksToRun static local
In order to allow compiling with -Werror=global-constructors with c++20 and above.
Discussion: https://discourse.llvm.org/t/llvm-lib-support-signals-cpp-fails-to-compile-due-to-werror-global-constructors/61070
Reviewed By: mehdi_amini
Differential Revision: https://reviews.llvm.org/D122067
Added:
Modified:
llvm/lib/Support/Signals.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Support/Signals.cpp b/llvm/lib/Support/Signals.cpp
index f10551df4baa5..a6fd845da8690 100644
--- a/llvm/lib/Support/Signals.cpp
+++ b/llvm/lib/Support/Signals.cpp
@@ -29,6 +29,7 @@
#include "llvm/Support/Program.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/raw_ostream.h"
+#include <array>
#include <vector>
//===----------------------------------------------------------------------===//
@@ -80,12 +81,20 @@ struct CallbackAndCookie {
enum class Status { Empty, Initializing, Initialized, Executing };
std::atomic<Status> Flag;
};
+
static constexpr size_t MaxSignalHandlerCallbacks = 8;
-static CallbackAndCookie CallBacksToRun[MaxSignalHandlerCallbacks];
+
+// A global array of CallbackAndCookie may not compile with
+// -Werror=global-constructors in c++20 and above
+static std::array<CallbackAndCookie, MaxSignalHandlerCallbacks> &
+CallBacksToRun() {
+ static std::array<CallbackAndCookie, MaxSignalHandlerCallbacks> callbacks;
+ return callbacks;
+}
// Signal-safe.
void sys::RunSignalHandlers() {
- for (CallbackAndCookie &RunMe : CallBacksToRun) {
+ for (CallbackAndCookie &RunMe : CallBacksToRun()) {
auto Expected = CallbackAndCookie::Status::Initialized;
auto Desired = CallbackAndCookie::Status::Executing;
if (!RunMe.Flag.compare_exchange_strong(Expected, Desired))
@@ -100,7 +109,7 @@ void sys::RunSignalHandlers() {
// Signal-safe.
static void insertSignalHandler(sys::SignalHandlerCallback FnPtr,
void *Cookie) {
- for (CallbackAndCookie &SetMe : CallBacksToRun) {
+ for (CallbackAndCookie &SetMe : CallBacksToRun()) {
auto Expected = CallbackAndCookie::Status::Empty;
auto Desired = CallbackAndCookie::Status::Initializing;
if (!SetMe.Flag.compare_exchange_strong(Expected, Desired))
More information about the llvm-commits
mailing list