Skip to content

Commit 9487cf9

Browse files
authored
[Support] Restrict ManagedStatic ThreadPoolExecutor to Windows
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. Pull Request: #102989
1 parent 5bb379f commit 9487cf9

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

llvm/lib/Support/Parallel.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class ThreadPoolExecutor : public Executor {
156156
};
157157

158158
Executor *Executor::getDefaultExecutor() {
159+
#ifdef _WIN32
159160
// The ManagedStatic enables the ThreadPoolExecutor to be stopped via
160161
// llvm_shutdown() which allows a "clean" fast exit, e.g. via _exit(). This
161162
// stops the thread pool and waits for any worker thread creation to complete
@@ -179,6 +180,14 @@ Executor *Executor::getDefaultExecutor() {
179180
ManagedExec;
180181
static std::unique_ptr<ThreadPoolExecutor> Exec(&(*ManagedExec));
181182
return Exec.get();
183+
#else
184+
// ManagedStatic is not desired on other platforms. When `Exec` is destroyed
185+
// by llvm_shutdown(), worker threads will clean up and invoke TLS
186+
// destructors. This can lead to race conditions if other threads attempt to
187+
// access TLS objects that have already been destroyed.
188+
static ThreadPoolExecutor Exec(strategy);
189+
return &Exec;
190+
#endif
182191
}
183192
} // namespace
184193
} // namespace detail

0 commit comments

Comments
 (0)