[llvm] [Support] Restrict ManagedStatic ThreadPoolExecutor to Windows (PR #102989)
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 12 17:32:08 PDT 2024
https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/102989
https://reviews.llvm.org/D70447 switched to `ManagedStatic` to work
around race conditions in MSVC runtimes and the MinGW runtime.
However, `ManagedStatic` is not suitable for other platforms.
However, this workaround is not suitable for other platforms (#66974).
lld::fatal calls exitLld(1), which calls `llvm_shutdown` to destroy
`ManagedStatic` objects. The worker threads will finish and invoke TLS
destructors (glibc `__call_tls_dtors`). This can lead to race conditions
if other threads attempt to access TLS objects that have already been
destroyed.
While lld's early exit mechanism needs more work, I believe Parallel.cpp
should avoid this pitfall as well.
>From 096f5ca2e30af10ae2dfeab31c1fd2c6c872f1d7 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Mon, 12 Aug 2024 17:31:56 -0700
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
=?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.5-bogner
---
llvm/lib/Support/Parallel.cpp | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/llvm/lib/Support/Parallel.cpp b/llvm/lib/Support/Parallel.cpp
index af35947192c0db..849041f0e21505 100644
--- a/llvm/lib/Support/Parallel.cpp
+++ b/llvm/lib/Support/Parallel.cpp
@@ -156,6 +156,7 @@ class ThreadPoolExecutor : public Executor {
};
Executor *Executor::getDefaultExecutor() {
+#ifdef _WIN32
// The ManagedStatic enables the ThreadPoolExecutor to be stopped via
// llvm_shutdown() which allows a "clean" fast exit, e.g. via _exit(). This
// stops the thread pool and waits for any worker thread creation to complete
@@ -179,6 +180,14 @@ Executor *Executor::getDefaultExecutor() {
ManagedExec;
static std::unique_ptr<ThreadPoolExecutor> Exec(&(*ManagedExec));
return Exec.get();
+#else
+ // ManagedStatic is not desired on other platforms. When `Exec` is terminated
+ // by llvm_shutdown(), worker threads will clean up and invoke TLS
+ // destructors. This can lead to race conditions if other threads attempt to
+ // access TLS objects that have already been destroyed.
+ static ThreadPoolExecutor Exec(strategy);
+ return &Exec;
+#endif
}
} // namespace
} // namespace detail
More information about the llvm-commits
mailing list