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;
54#if defined(__LLVM_LIBC__)
55 using id = pthread_id_np_t;
57 using id =
unsigned long long;
61 using start_routine_type =
void *(*)(
void *);
63 template <
typename CalleeTuple>
static void *ThreadProxy(
void *Ptr) {
64 GenericThreadProxy<CalleeTuple>(Ptr);
68 using native_handle_type = HANDLE;
70 using start_routine_type = unsigned(__stdcall *)(
void *);
72 template <
typename CalleeTuple>
73 static unsigned __stdcall ThreadProxy(
void *Ptr) {
74 GenericThreadProxy<CalleeTuple>(Ptr);
79 LLVM_ABI static const std::optional<unsigned> DefaultStackSize;
83 :
Thread(std::exchange(
Other.Thread, native_handle_type())) {}
90 explicit thread(std::optional<unsigned> StackSizeInBytes, Function &&f,
102 Thread = std::exchange(
Other.Thread, native_handle_type());
106 bool joinable()
const noexcept {
return get_id() != 0; }
108 inline id get_id()
const noexcept;
110 native_handle_type native_handle()
const noexcept {
return Thread; }
113 return std::thread::hardware_concurrency();
122 native_handle_type
Thread;
126llvm_execute_on_thread_impl(thread::start_routine_type ThreadFunc,
void *Arg,
127 std::optional<unsigned> StackSizeInBytes);
128LLVM_ABI void llvm_thread_join_impl(thread::native_handle_type Thread);
129LLVM_ABI void llvm_thread_detach_impl(thread::native_handle_type Thread);
130LLVM_ABI thread::id llvm_thread_get_id_impl(thread::native_handle_type Thread);
131LLVM_ABI thread::id llvm_thread_get_current_id_impl();
136 using CalleeTuple = std::tuple<std::decay_t<Function>, std::decay_t<Args>...>;
137 std::unique_ptr<CalleeTuple>
Callee(
138 new CalleeTuple(std::forward<Function>(f), std::forward<Args>(
args)...));
140 Thread = llvm_execute_on_thread_impl(ThreadProxy<CalleeTuple>,
Callee.get(),
146thread::id thread::get_id()
const noexcept {
147 return llvm_thread_get_id_impl(Thread);
151 llvm_thread_join_impl(Thread);
152 Thread = native_handle_type();
156 llvm_thread_detach_impl(Thread);
157 Thread = native_handle_type();
160namespace this_thread {
161inline thread::id get_id() {
return llvm_thread_get_current_id_impl(); }
170 using native_handle_type = std::thread::native_handle_type;
171 using id = std::thread::id;
175 :
Thread(std::exchange(
Other.Thread, std::thread())) {}
178 explicit thread(std::optional<unsigned> StackSizeInBytes, Function &&f,
190 Thread = std::exchange(
Other.Thread, std::thread());
194 bool joinable()
const noexcept {
return Thread.joinable(); }
196 id get_id()
const noexcept {
return Thread.get_id(); }
198 native_handle_type native_handle() noexcept {
return Thread.native_handle(); }
201 return std::thread::hardware_concurrency();
213namespace this_thread {
214inline thread::id get_id() {
return std::this_thread::get_id(); }
231 template <
class Function,
class... Args>
234 f(std::forward<Args>(
args)...);
236 template <
class Function,
class... Args>
238 f(std::forward<Args>(
args)...);
244 "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