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