summaryrefslogtreecommitdiff
path: root/llvm/lib
diff options
context:
space:
mode:
authorTim Northover <t.p.northover@gmail.com>2021-07-08 14:59:47 +0100
committerTim Northover <t.p.northover@gmail.com>2021-07-08 14:59:47 +0100
commit2bf5e8d953ededbc208bd4c116c9d6331d73f0f0 (patch)
tree6f0e1d1dce203f49fd943933ea271edbb56bee75 /llvm/lib
parent727e1c9be3a5b20c6b502f056d74a681689049d7 (diff)
downloadllvm-2bf5e8d953ededbc208bd4c116c9d6331d73f0f0.tar.gz
Revert "Support: add llvm::thread class that supports specifying stack size."
It's causing build failures because DefaultStackSize isn't defined everywhere it should be and I need time to investigate.
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Support/CrashRecoveryContext.cpp11
-rw-r--r--llvm/lib/Support/ThreadPool.cpp4
-rw-r--r--llvm/lib/Support/Threading.cpp54
-rw-r--r--llvm/lib/Support/Unix/Threading.inc52
-rw-r--r--llvm/lib/Support/Windows/Threading.inc41
5 files changed, 104 insertions, 58 deletions
diff --git a/llvm/lib/Support/CrashRecoveryContext.cpp b/llvm/lib/Support/CrashRecoveryContext.cpp
index 433d99df5932..3ee6a4fd36f4 100644
--- a/llvm/lib/Support/CrashRecoveryContext.cpp
+++ b/llvm/lib/Support/CrashRecoveryContext.cpp
@@ -13,7 +13,6 @@
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/ThreadLocal.h"
-#include "llvm/Support/thread.h"
#include <mutex>
#include <setjmp.h>
@@ -501,12 +500,10 @@ bool CrashRecoveryContext::RunSafelyOnThread(function_ref<void()> Fn,
unsigned RequestedStackSize) {
bool UseBackgroundPriority = hasThreadBackgroundPriority();
RunSafelyOnThreadInfo Info = { Fn, this, UseBackgroundPriority, false };
- llvm::thread Thread(RequestedStackSize == 0
- ? llvm::None
- : llvm::Optional<unsigned>(RequestedStackSize),
- RunSafelyOnThread_Dispatch, &Info);
- Thread.join();
-
+ llvm_execute_on_thread(RunSafelyOnThread_Dispatch, &Info,
+ RequestedStackSize == 0
+ ? llvm::None
+ : llvm::Optional<unsigned>(RequestedStackSize));
if (CrashRecoveryContextImpl *CRC = (CrashRecoveryContextImpl *)Impl)
CRC->setSwitchedThread();
return Info.Result;
diff --git a/llvm/lib/Support/ThreadPool.cpp b/llvm/lib/Support/ThreadPool.cpp
index 81926d8071b2..f442b3b0bc98 100644
--- a/llvm/lib/Support/ThreadPool.cpp
+++ b/llvm/lib/Support/ThreadPool.cpp
@@ -73,8 +73,8 @@ void ThreadPool::wait() {
}
bool ThreadPool::isWorkerThread() const {
- llvm::thread::id CurrentThreadId = llvm::this_thread::get_id();
- for (const llvm::thread &Thread : Threads)
+ std::thread::id CurrentThreadId = std::this_thread::get_id();
+ for (const std::thread &Thread : Threads)
if (CurrentThreadId == Thread.get_id())
return true;
return false;
diff --git a/llvm/lib/Support/Threading.cpp b/llvm/lib/Support/Threading.cpp
index 55e97d60af7a..61f8ee5be5b3 100644
--- a/llvm/lib/Support/Threading.cpp
+++ b/llvm/lib/Support/Threading.cpp
@@ -15,7 +15,6 @@
#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>
@@ -39,6 +38,13 @@ 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; }
@@ -54,6 +60,25 @@ 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();
@@ -70,6 +95,17 @@ 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"
@@ -78,6 +114,22 @@ unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
#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);
+}
+
+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
Optional<ThreadPoolStrategy>
diff --git a/llvm/lib/Support/Unix/Threading.inc b/llvm/lib/Support/Unix/Threading.inc
index 2131defadbb4..667d023fc134 100644
--- a/llvm/lib/Support/Unix/Threading.inc
+++ b/llvm/lib/Support/Unix/Threading.inc
@@ -48,9 +48,22 @@
#include <unistd.h> // For syscall()
#endif
-pthread_t
-llvm::llvm_execute_on_thread_impl(void *(*ThreadFunc)(void *), void *Arg,
- llvm::Optional<unsigned> StackSizeInBytes) {
+static void *threadFuncSync(void *Arg) {
+ SyncThreadInfo *TI = static_cast<SyncThreadInfo *>(Arg);
+ TI->UserFn(TI->UserData);
+ return nullptr;
+}
+
+static void *threadFuncAsync(void *Arg) {
+ std::unique_ptr<AsyncThreadInfo> Info(static_cast<AsyncThreadInfo *>(Arg));
+ (*Info)();
+ return nullptr;
+}
+
+static void
+llvm_execute_on_thread_impl(void *(*ThreadFunc)(void *), void *Arg,
+ llvm::Optional<unsigned> StackSizeInBytes,
+ JoiningPolicy JP) {
int errnum;
// Construct the attributes object.
@@ -77,33 +90,18 @@ llvm::llvm_execute_on_thread_impl(void *(*ThreadFunc)(void *), void *Arg,
if ((errnum = ::pthread_create(&Thread, &Attr, ThreadFunc, Arg)) != 0)
ReportErrnumFatal("pthread_create failed", errnum);
- return Thread;
-}
-
-void llvm::llvm_thread_detach_impl(pthread_t Thread) {
- int errnum;
-
- if ((errnum = ::pthread_detach(Thread)) != 0) {
- ReportErrnumFatal("pthread_detach failed", errnum);
- }
-}
-
-void llvm::llvm_thread_join_impl(pthread_t Thread) {
- int errnum;
-
- if ((errnum = ::pthread_join(Thread, nullptr)) != 0) {
- ReportErrnumFatal("pthread_join failed", errnum);
+ if (JP == JoiningPolicy::Join) {
+ // Wait for the thread
+ if ((errnum = ::pthread_join(Thread, nullptr)) != 0) {
+ ReportErrnumFatal("pthread_join failed", errnum);
+ }
+ } else if (JP == JoiningPolicy::Detach) {
+ if ((errnum = ::pthread_detach(Thread)) != 0) {
+ ReportErrnumFatal("pthread_detach failed", errnum);
+ }
}
}
-pthread_t llvm::llvm_thread_get_id_impl(pthread_t Thread) {
- return Thread;
-}
-
-pthread_t llvm::llvm_thread_get_current_id_impl() {
- return ::pthread_self();
-}
-
uint64_t llvm::get_threadid() {
#if defined(__APPLE__)
// Calling "mach_thread_self()" bumps the reference count on the thread
diff --git a/llvm/lib/Support/Windows/Threading.inc b/llvm/lib/Support/Windows/Threading.inc
index 12d8cbc21cbc..6448bb478d0c 100644
--- a/llvm/lib/Support/Windows/Threading.inc
+++ b/llvm/lib/Support/Windows/Threading.inc
@@ -23,10 +23,22 @@
#undef MemoryFence
#endif
-HANDLE
-llvm::llvm_execute_on_thread_impl(unsigned(__stdcall *ThreadFunc)(void *),
- void *Arg,
- llvm::Optional<unsigned> StackSizeInBytes) {
+static unsigned __stdcall threadFuncSync(void *Arg) {
+ SyncThreadInfo *TI = static_cast<SyncThreadInfo *>(Arg);
+ TI->UserFn(TI->UserData);
+ return 0;
+}
+
+static unsigned __stdcall threadFuncAsync(void *Arg) {
+ std::unique_ptr<AsyncThreadInfo> Info(static_cast<AsyncThreadInfo *>(Arg));
+ (*Info)();
+ return 0;
+}
+
+static void
+llvm_execute_on_thread_impl(unsigned (__stdcall *ThreadFunc)(void *), void *Arg,
+ llvm::Optional<unsigned> StackSizeInBytes,
+ JoiningPolicy JP) {
HANDLE hThread = (HANDLE)::_beginthreadex(
NULL, StackSizeInBytes.getValueOr(0), ThreadFunc, Arg, 0, NULL);
@@ -34,29 +46,16 @@ llvm::llvm_execute_on_thread_impl(unsigned(__stdcall *ThreadFunc)(void *),
ReportLastErrorFatal("_beginthreadex failed");
}
- return hThread;
-}
-
-void llvm::llvm_thread_join_impl(HANDLE hThread) {
- if (::WaitForSingleObject(hThread, INFINITE) == WAIT_FAILED) {
- ReportLastErrorFatal("WaitForSingleObject failed");
+ if (JP == JoiningPolicy::Join) {
+ if (::WaitForSingleObject(hThread, INFINITE) == WAIT_FAILED) {
+ ReportLastErrorFatal("WaitForSingleObject failed");
+ }
}
-}
-
-void llvm::llvm_thread_detach_impl(HANDLE hThread) {
if (::CloseHandle(hThread) == FALSE) {
ReportLastErrorFatal("CloseHandle failed");
}
}
-DWORD llvm::llvm_thread_get_id_impl(HANDLE hThread) {
- return ::GetThreadId(hThread);
-}
-
-DWORD llvm::llvm_thread_get_current_id_impl() {
- return ::GetCurrentThreadId();
-}
-
uint64_t llvm::get_threadid() {
return uint64_t(::GetCurrentThreadId());
}