| From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| From: PJ Reiniger <pj.reiniger@gmail.com> |
| Date: Sat, 7 May 2022 22:17:19 -0400 |
| Subject: [PATCH 04/31] Threading updates |
| |
| - Remove guards for threads and exception |
| - Prefer scope gaurd over lock gaurd |
| --- |
| llvm/include/llvm/Support/Compiler.h | 6 ----- |
| llvm/lib/Support/ErrorHandling.cpp | 38 +++++----------------------- |
| llvm/lib/Support/ManagedStatic.cpp | 10 ++++---- |
| 3 files changed, 11 insertions(+), 43 deletions(-) |
| |
| diff --git a/llvm/include/llvm/Support/Compiler.h b/llvm/include/llvm/Support/Compiler.h |
| index 92376629c607461061bc60597a47aed1e535af52..2662839b27bf368cd5da0668099c4b44cbc6435d 100644 |
| --- a/llvm/include/llvm/Support/Compiler.h |
| +++ b/llvm/include/llvm/Support/Compiler.h |
| @@ -530,7 +530,6 @@ void AnnotateIgnoreWritesEnd(const char *file, int line); |
| /// initialize to some constant value. In almost all circumstances this is most |
| /// appropriate for use with a pointer, integer, or small aggregation of |
| /// pointers and integers. |
| -#if LLVM_ENABLE_THREADS |
| #if __has_feature(cxx_thread_local) || defined(_MSC_VER) |
| #define LLVM_THREAD_LOCAL thread_local |
| #else |
| @@ -538,11 +537,6 @@ void AnnotateIgnoreWritesEnd(const char *file, int line); |
| // we only need the restricted functionality that provides. |
| #define LLVM_THREAD_LOCAL __thread |
| #endif |
| -#else // !LLVM_ENABLE_THREADS |
| -// If threading is disabled entirely, this compiles to nothing and you get |
| -// a normal global variable. |
| -#define LLVM_THREAD_LOCAL |
| -#endif |
| |
| /// \macro LLVM_ENABLE_EXCEPTIONS |
| /// Whether LLVM is built with exception support. |
| diff --git a/llvm/lib/Support/ErrorHandling.cpp b/llvm/lib/Support/ErrorHandling.cpp |
| index 0aa13a0f78eb370b2a673ca4a773f26820575052..637b669a7d0dae69ef4b34955f21a9fb8ba1276e 100644 |
| --- a/llvm/lib/Support/ErrorHandling.cpp |
| +++ b/llvm/lib/Support/ErrorHandling.cpp |
| @@ -44,7 +44,6 @@ static void *ErrorHandlerUserData = nullptr; |
| static fatal_error_handler_t BadAllocErrorHandler = nullptr; |
| static void *BadAllocErrorHandlerUserData = nullptr; |
| |
| -#if LLVM_ENABLE_THREADS == 1 |
| // Mutexes to synchronize installing error handlers and calling error handlers. |
| // Do not use ManagedStatic, or that may allocate memory while attempting to |
| // report an OOM. |
| @@ -58,22 +57,17 @@ static void *BadAllocErrorHandlerUserData = nullptr; |
| // builds. We can remove these ifdefs if that script goes away. |
| static std::mutex ErrorHandlerMutex; |
| static std::mutex BadAllocErrorHandlerMutex; |
| -#endif |
| |
| void llvm::install_fatal_error_handler(fatal_error_handler_t handler, |
| void *user_data) { |
| -#if LLVM_ENABLE_THREADS == 1 |
| - std::lock_guard<std::mutex> Lock(ErrorHandlerMutex); |
| -#endif |
| + std::scoped_lock Lock(ErrorHandlerMutex); |
| assert(!ErrorHandler && "Error handler already registered!\n"); |
| ErrorHandler = handler; |
| ErrorHandlerUserData = user_data; |
| } |
| |
| void llvm::remove_fatal_error_handler() { |
| -#if LLVM_ENABLE_THREADS == 1 |
| - std::lock_guard<std::mutex> Lock(ErrorHandlerMutex); |
| -#endif |
| + std::scoped_lock Lock(ErrorHandlerMutex); |
| ErrorHandler = nullptr; |
| ErrorHandlerUserData = nullptr; |
| } |
| @@ -92,9 +86,7 @@ void llvm::report_fatal_error(std::string_view Reason, bool GenCrashDiag) { |
| { |
| // Only acquire the mutex while reading the handler, so as not to invoke a |
| // user-supplied callback under a lock. |
| -#if LLVM_ENABLE_THREADS == 1 |
| - std::lock_guard<std::mutex> Lock(ErrorHandlerMutex); |
| -#endif |
| + std::scoped_lock Lock(ErrorHandlerMutex); |
| handler = ErrorHandler; |
| handlerData = ErrorHandlerUserData; |
| } |
| @@ -126,18 +118,14 @@ void llvm::report_fatal_error(std::string_view Reason, bool GenCrashDiag) { |
| |
| void llvm::install_bad_alloc_error_handler(fatal_error_handler_t handler, |
| void *user_data) { |
| -#if LLVM_ENABLE_THREADS == 1 |
| - std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex); |
| -#endif |
| + std::scoped_lock Lock(BadAllocErrorHandlerMutex); |
| assert(!ErrorHandler && "Bad alloc error handler already registered!\n"); |
| BadAllocErrorHandler = handler; |
| BadAllocErrorHandlerUserData = user_data; |
| } |
| |
| void llvm::remove_bad_alloc_error_handler() { |
| -#if LLVM_ENABLE_THREADS == 1 |
| - std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex); |
| -#endif |
| + std::scoped_lock Lock(BadAllocErrorHandlerMutex); |
| BadAllocErrorHandler = nullptr; |
| BadAllocErrorHandlerUserData = nullptr; |
| } |
| @@ -148,9 +136,7 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) { |
| { |
| // Only acquire the mutex while reading the handler, so as not to invoke a |
| // user-supplied callback under a lock. |
| -#if LLVM_ENABLE_THREADS == 1 |
| - std::lock_guard<std::mutex> Lock(BadAllocErrorHandlerMutex); |
| -#endif |
| + std::scoped_lock Lock(BadAllocErrorHandlerMutex); |
| Handler = BadAllocErrorHandler; |
| HandlerData = BadAllocErrorHandlerUserData; |
| } |
| @@ -160,10 +146,6 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) { |
| llvm_unreachable("bad alloc handler should not return"); |
| } |
| |
| -#ifdef LLVM_ENABLE_EXCEPTIONS |
| - // If exceptions are enabled, make OOM in malloc look like OOM in new. |
| - throw std::bad_alloc(); |
| -#else |
| // Don't call the normal error handler. It may allocate memory. Directly write |
| // an OOM to stderr and abort. |
| const char *OOMMessage = "LLVM ERROR: out of memory\n"; |
| @@ -172,15 +154,8 @@ void llvm::report_bad_alloc_error(const char *Reason, bool GenCrashDiag) { |
| (void)!::write(2, Reason, strlen(Reason)); |
| (void)!::write(2, Newline, strlen(Newline)); |
| abort(); |
| -#endif |
| } |
| |
| -#ifdef LLVM_ENABLE_EXCEPTIONS |
| -// Do not set custom new handler if exceptions are enabled. In this case OOM |
| -// errors are handled by throwing 'std::bad_alloc'. |
| -void llvm::install_out_of_memory_new_handler() { |
| -} |
| -#else |
| // Causes crash on allocation failure. It is called prior to the handler set by |
| // 'install_bad_alloc_error_handler'. |
| static void out_of_memory_new_handler() { |
| @@ -195,7 +170,6 @@ void llvm::install_out_of_memory_new_handler() { |
| assert((old == nullptr || old == out_of_memory_new_handler) && |
| "new-handler already installed"); |
| } |
| -#endif |
| |
| void llvm::llvm_unreachable_internal(const char *msg, const char *file, |
| unsigned line) { |
| diff --git a/llvm/lib/Support/ManagedStatic.cpp b/llvm/lib/Support/ManagedStatic.cpp |
| index a6ae67066ea0423334e8ee52106f220cd456e25e..fc798b7ec1b788e232c7374b9968dc71d4f506f0 100644 |
| --- a/llvm/lib/Support/ManagedStatic.cpp |
| +++ b/llvm/lib/Support/ManagedStatic.cpp |
| @@ -12,23 +12,23 @@ |
| |
| #include "llvm/Support/ManagedStatic.h" |
| #include "llvm/Config/config.h" |
| -#include "llvm/Support/Threading.h" |
| +#include "llvm/Support/mutex.h" |
| #include <cassert> |
| #include <mutex> |
| using namespace llvm; |
| |
| static const ManagedStaticBase *StaticList = nullptr; |
| |
| -static std::recursive_mutex *getManagedStaticMutex() { |
| - static std::recursive_mutex m; |
| +static llvm::mutex *getManagedStaticMutex() { |
| + static llvm::mutex m; |
| return &m; |
| } |
| |
| void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(), |
| void (*Deleter)(void*)) const { |
| assert(Creator); |
| - if (llvm_is_multithreaded()) { |
| - std::lock_guard<std::recursive_mutex> Lock(*getManagedStaticMutex()); |
| + if (1) { |
| + std::scoped_lock Lock(*getManagedStaticMutex()); |
| |
| if (!Ptr.load(std::memory_order_relaxed)) { |
| void *Tmp = Creator(); |