[PATCH] D122067: [libSupport] make CallBacksToRun static local

Tal Kedar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 19 07:46:38 PDT 2022


tal.kedar created this revision.
tal.kedar added a reviewer: mehdi_amini.
Herald added subscribers: dexonsmith, hiraditya.
Herald added a project: All.
tal.kedar requested review of this revision.
Herald added a project: LLVM.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122067

Files:
  llvm/lib/Support/Signals.cpp


Index: llvm/lib/Support/Signals.cpp
===================================================================
--- llvm/lib/Support/Signals.cpp
+++ 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 @@
   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 @@
 // 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))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122067.416696.patch
Type: text/x-patch
Size: 1757 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220319/ecb5da12/attachment.bin>


More information about the llvm-commits mailing list