diff options
Diffstat (limited to 'llvm/lib/Support/Unix/Threading.inc')
-rw-r--r-- | llvm/lib/Support/Unix/Threading.inc | 63 |
1 files changed, 26 insertions, 37 deletions
diff --git a/llvm/lib/Support/Unix/Threading.inc b/llvm/lib/Support/Unix/Threading.inc index afb887fc109..ed9a9656305 100644 --- a/llvm/lib/Support/Unix/Threading.inc +++ b/llvm/lib/Support/Unix/Threading.inc @@ -10,8 +10,6 @@ // //===----------------------------------------------------------------------===// -#include "Unix.h" -#include "llvm/ADT/ScopeExit.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/Twine.h" @@ -42,56 +40,47 @@ #include <unistd.h> // For syscall() #endif -static void *threadFuncSync(void *Arg) { - SyncThreadInfo *TI = static_cast<SyncThreadInfo *>(Arg); - TI->UserFn(TI->UserData); - return nullptr; +namespace { + struct ThreadInfo { + void(*UserFn)(void *); + void *UserData; + }; } -static void *threadFuncAsync(void *Arg) { - std::unique_ptr<AsyncThreadInfo> Info(static_cast<AsyncThreadInfo *>(Arg)); - (*Info)(); +static void *ExecuteOnThread_Dispatch(void *Arg) { + ThreadInfo *TI = reinterpret_cast<ThreadInfo*>(Arg); + TI->UserFn(TI->UserData); 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. +void llvm::llvm_execute_on_thread(void(*Fn)(void*), void *UserData, + unsigned RequestedStackSize) { + ThreadInfo Info = { Fn, UserData }; pthread_attr_t Attr; - if ((errnum = ::pthread_attr_init(&Attr)) != 0) { - ReportErrnumFatal("pthread_attr_init failed", errnum); - } + pthread_t Thread; - auto AttrGuard = llvm::make_scope_exit([&] { - if ((errnum = ::pthread_attr_destroy(&Attr)) != 0) { - ReportErrnumFatal("pthread_attr_destroy failed", errnum); - } - }); + // Construct the attributes object. + if (::pthread_attr_init(&Attr) != 0) + return; // Set the requested stack size, if given. - if (StackSizeInBytes) { - if ((errnum = ::pthread_attr_setstacksize(&Attr, *StackSizeInBytes)) != 0) { - ReportErrnumFatal("pthread_attr_setstacksize failed", errnum); - } + if (RequestedStackSize != 0) { + if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0) + goto error; } // Construct and execute the thread. - pthread_t Thread; - if ((errnum = ::pthread_create(&Thread, &Attr, ThreadFunc, Arg)) != 0) - ReportErrnumFatal("pthread_create failed", errnum); + if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0) + goto error; - if (JP == JoiningPolicy::Join) { - // Wait for the thread - if ((errnum = ::pthread_join(Thread, nullptr)) != 0) { - ReportErrnumFatal("pthread_join failed", errnum); - } - } + // Wait for the thread and clean up. + ::pthread_join(Thread, nullptr); + +error: + ::pthread_attr_destroy(&Attr); } + uint64_t llvm::get_threadid() { #if defined(__APPLE__) // Calling "mach_thread_self()" bumps the reference count on the thread |