diff options
author | Tim Northover <t.p.northover@gmail.com> | 2021-05-26 11:25:11 +0100 |
---|---|---|
committer | Tim Northover <t.p.northover@gmail.com> | 2021-07-08 16:22:26 +0100 |
commit | 48c68a630e06666101c16aa371f9202a4a53438b (patch) | |
tree | 365a41ea82b3a16034ed2fd0bc0b497ce2e98e18 /llvm/lib/Support/Threading.cpp | |
parent | b5113bff461b87fa214fa8fafdffd8eaaf8cf0e7 (diff) | |
download | llvm-48c68a630e06666101c16aa371f9202a4a53438b.zip llvm-48c68a630e06666101c16aa371f9202a4a53438b.tar.gz llvm-48c68a630e06666101c16aa371f9202a4a53438b.tar.bz2 |
Recommit: Support: add llvm::thread class that supports specifying stack size.
This adds a new llvm::thread class with the same interface as std::thread
except there is an extra constructor that allows us to set the new thread's
stack size. On Darwin even the default size is boosted to 8MB to match the main
thread.
It also switches all users of the older C-style `llvm_execute_on_thread` API
family over to `llvm::thread` followed by either a `detach` or `join` call and
removes the old API.
Moved definition of DefaultStackSize into the .cpp file to hopefully
fix the build on some (GCC-6?) machines.
Diffstat (limited to 'llvm/lib/Support/Threading.cpp')
-rw-r--r-- | llvm/lib/Support/Threading.cpp | 61 |
1 files changed, 10 insertions, 51 deletions
diff --git a/llvm/lib/Support/Threading.cpp b/llvm/lib/Support/Threading.cpp index 61f8ee5..ba92a3e 100644 --- a/llvm/lib/Support/Threading.cpp +++ b/llvm/lib/Support/Threading.cpp @@ -15,6 +15,7 @@ #include "llvm/ADT/Optional.h" #include "llvm/Config/config.h" #include "llvm/Support/Host.h" +#include "llvm/Support/thread.h" #include <cassert> #include <errno.h> @@ -38,13 +39,6 @@ bool llvm::llvm_is_multithreaded() { #if LLVM_ENABLE_THREADS == 0 || \ (!defined(_WIN32) && !defined(HAVE_PTHREAD_H)) -// Support for non-Win32, non-pthread implementation. -void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData, - llvm::Optional<unsigned> StackSizeInBytes) { - (void)StackSizeInBytes; - Fn(UserData); -} - uint64_t llvm::get_threadid() { return 0; } uint32_t llvm::get_max_thread_name_length() { return 0; } @@ -60,25 +54,6 @@ unsigned llvm::ThreadPoolStrategy::compute_thread_count() const { return 1; } -#if LLVM_ENABLE_THREADS == 0 -void llvm::llvm_execute_on_thread_async( - llvm::unique_function<void()> Func, - llvm::Optional<unsigned> StackSizeInBytes) { - (void)Func; - (void)StackSizeInBytes; - report_fatal_error("Spawning a detached thread doesn't make sense with no " - "threading support"); -} -#else -// Support for non-Win32, non-pthread implementation. -void llvm::llvm_execute_on_thread_async( - llvm::unique_function<void()> Func, - llvm::Optional<unsigned> StackSizeInBytes) { - (void)StackSizeInBytes; - std::thread(std::move(Func)).detach(); -} -#endif - #else int computeHostNumHardwareThreads(); @@ -95,17 +70,6 @@ unsigned llvm::ThreadPoolStrategy::compute_thread_count() const { return std::min((unsigned)MaxThreadCount, ThreadsRequested); } -namespace { -struct SyncThreadInfo { - void (*UserFn)(void *); - void *UserData; -}; - -using AsyncThreadInfo = llvm::unique_function<void()>; - -enum class JoiningPolicy { Join, Detach }; -} // namespace - // Include the platform-specific parts of this class. #ifdef LLVM_ON_UNIX #include "Unix/Threading.inc" @@ -114,21 +78,16 @@ enum class JoiningPolicy { Join, Detach }; #include "Windows/Threading.inc" #endif -void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData, - llvm::Optional<unsigned> StackSizeInBytes) { - - SyncThreadInfo Info = {Fn, UserData}; - llvm_execute_on_thread_impl(threadFuncSync, &Info, StackSizeInBytes, - JoiningPolicy::Join); -} +#if defined(__APPLE__) + // Darwin's default stack size for threads except the main one is only 512KB, + // which is not enough for some/many normal LLVM compilations. This implements + // the same interface as std::thread but requests the same stack size as the + // main thread (8MB) before creation. +const llvm::Optional<unsigned> llvm::thread::DefaultStackSize = 8 * 1024 * 1024; +#else +const llvm::Optional<unsigned> llvm::thread::DefaultStackSize = None; +#endif -void llvm::llvm_execute_on_thread_async( - llvm::unique_function<void()> Func, - llvm::Optional<unsigned> StackSizeInBytes) { - llvm_execute_on_thread_impl(&threadFuncAsync, - new AsyncThreadInfo(std::move(Func)), - StackSizeInBytes, JoiningPolicy::Detach); -} #endif |