16#ifndef LLVM_SUPPORT_THREAD_H
17#define LLVM_SUPPORT_THREAD_H
19#include "llvm/Config/llvm-config.h"
26typedef unsigned long DWORD;
31#if LLVM_ENABLE_THREADS
37#if defined(LLVM_ON_UNIX) || defined(_WIN32)
42 template <
typename CalleeTuple>
static void GenericThreadProxy(
void *Ptr) {
43 std::unique_ptr<CalleeTuple>
Callee(
static_cast<CalleeTuple *
>(Ptr));
45 [](
auto &&
F,
auto &&...Args) {
46 std::forward<decltype(F)>(
F)(std::forward<decltype(Args)>(Args)...);
53 using native_handle_type = pthread_t;
55 using id =
unsigned long long;
59 using start_routine_type =
void *(*)(
void *);
61 template <
typename CalleeTuple>
static void *ThreadProxy(
void *Ptr) {
62 GenericThreadProxy<CalleeTuple>(Ptr);
66 using native_handle_type = HANDLE;
68 using start_routine_type = unsigned(__stdcall *)(
void *);
70 template <
typename CalleeTuple>
71 static unsigned __stdcall ThreadProxy(
void *Ptr) {
72 GenericThreadProxy<CalleeTuple>(Ptr);
77 LLVM_ABI static const std::optional<unsigned> DefaultStackSize;
81 :
Thread(std::exchange(
Other.Thread, native_handle_type())) {}
88 explicit thread(std::optional<unsigned> StackSizeInBytes, Function &&f,
100 Thread = std::exchange(
Other.Thread, native_handle_type());
104 bool joinable()
const noexcept {
return get_id() != 0; }
106 inline id get_id()
const noexcept;
108 native_handle_type native_handle()
const noexcept {
return Thread; }
111 return std::thread::hardware_concurrency();
120 native_handle_type
Thread;
124llvm_execute_on_thread_impl(thread::start_routine_type ThreadFunc,
void *Arg,
125 std::optional<unsigned> StackSizeInBytes);
126LLVM_ABI void llvm_thread_join_impl(thread::native_handle_type Thread);
127LLVM_ABI void llvm_thread_detach_impl(thread::native_handle_type Thread);
128LLVM_ABI thread::id llvm_thread_get_id_impl(thread::native_handle_type Thread);
129LLVM_ABI thread::id llvm_thread_get_current_id_impl();
134 using CalleeTuple = std::tuple<std::decay_t<Function>, std::decay_t<Args>...>;
135 std::unique_ptr<CalleeTuple>
Callee(
136 new CalleeTuple(std::forward<Function>(f), std::forward<Args>(
args)...));
138 Thread = llvm_execute_on_thread_impl(ThreadProxy<CalleeTuple>,
Callee.get(),
144thread::id thread::get_id()
const noexcept {
145 return llvm_thread_get_id_impl(Thread);
149 llvm_thread_join_impl(Thread);
150 Thread = native_handle_type();
154 llvm_thread_detach_impl(Thread);
155 Thread = native_handle_type();
158namespace this_thread {
159inline thread::id get_id() {
return llvm_thread_get_current_id_impl(); }
168 using native_handle_type = std::thread::native_handle_type;
169 using id = std::thread::id;
173 :
Thread(std::exchange(
Other.Thread, std::thread())) {}
176 explicit thread(std::optional<unsigned> StackSizeInBytes, Function &&f,
188 Thread = std::exchange(
Other.Thread, std::thread());
192 bool joinable()
const noexcept {
return Thread.joinable(); }
194 id get_id()
const noexcept {
return Thread.get_id(); }
196 native_handle_type native_handle() noexcept {
return Thread.native_handle(); }
199 return std::thread::hardware_concurrency();
211namespace this_thread {
212inline thread::id get_id() {
return std::this_thread::get_id(); }
229 template <
class Function,
class... Args>
232 f(std::forward<Args>(
args)...);
234 template <
class Function,
class... Args>
236 f(std::forward<Args>(
args)...);
242 "threading support");
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
thread(std::optional< unsigned > StackSizeInBytes, Function &&f, Args &&...args)
static unsigned hardware_concurrency()
thread(Function &&f, Args &&...args)
thread(const thread &)=delete