16#ifndef LLVM_SUPPORT_THREAD_H
17#define LLVM_SUPPORT_THREAD_H
19#include "llvm/Config/llvm-config.h"
23typedef unsigned long DWORD;
28#if LLVM_ENABLE_THREADS
34#if LLVM_ON_UNIX || _WIN32
39 template <
typename CalleeTuple>
static void GenericThreadProxy(
void *
Ptr) {
40 std::unique_ptr<CalleeTuple>
Callee(
static_cast<CalleeTuple *
>(
Ptr));
42 [](
auto &&
F,
auto &&...Args) {
43 std::forward<decltype(F)>(
F)(std::forward<decltype(Args)>(Args)...);
50 using native_handle_type = pthread_t;
52 using start_routine_type =
void *(*)(
void *);
54 template <
typename CalleeTuple>
static void *ThreadProxy(
void *
Ptr) {
55 GenericThreadProxy<CalleeTuple>(
Ptr);
59 using native_handle_type = HANDLE;
61 using start_routine_type =
unsigned(__stdcall *)(
void *);
63 template <
typename CalleeTuple>
64 static unsigned __stdcall ThreadProxy(
void *
Ptr) {
65 GenericThreadProxy<CalleeTuple>(
Ptr);
70 static const std::optional<unsigned> DefaultStackSize;
74 :
Thread(std::exchange(
Other.Thread, native_handle_type())) {}
81 explicit thread(std::optional<unsigned> StackSizeInBytes, Function &&f,
93 Thread = std::exchange(
Other.Thread, native_handle_type());
97 bool joinable()
const noexcept {
return Thread != native_handle_type(); }
99 inline id get_id()
const noexcept;
101 native_handle_type native_handle()
const noexcept {
return Thread; }
104 return std::thread::hardware_concurrency();
113 native_handle_type
Thread;
116thread::native_handle_type
117llvm_execute_on_thread_impl(thread::start_routine_type ThreadFunc,
void *Arg,
118 std::optional<unsigned> StackSizeInBytes);
119void llvm_thread_join_impl(thread::native_handle_type
Thread);
120void llvm_thread_detach_impl(thread::native_handle_type
Thread);
121thread::id llvm_thread_get_id_impl(thread::native_handle_type
Thread);
122thread::id llvm_thread_get_current_id_impl();
125thread::thread(std::optional<unsigned> StackSizeInBytes, Function &&f,
127 typedef std::tuple<std::decay_t<Function>, std::decay_t<Args>...> CalleeTuple;
128 std::unique_ptr<CalleeTuple>
Callee(
129 new CalleeTuple(std::forward<Function>(f), std::forward<Args>(
args)...));
131 Thread = llvm_execute_on_thread_impl(ThreadProxy<CalleeTuple>,
Callee.get(),
133 if (
Thread != native_handle_type())
137thread::id thread::get_id()
const noexcept {
138 return llvm_thread_get_id_impl(
Thread);
142 llvm_thread_join_impl(
Thread);
143 Thread = native_handle_type();
147 llvm_thread_detach_impl(
Thread);
148 Thread = native_handle_type();
151namespace this_thread {
152inline thread::id get_id() {
return llvm_thread_get_current_id_impl(); }
161 using native_handle_type = std::thread::native_handle_type;
162 using id = std::thread::id;
166 :
Thread(std::exchange(
Other.Thread, std::thread())) {}
169 explicit thread(std::optional<unsigned> StackSizeInBytes, Function &&f,
181 Thread = std::exchange(
Other.Thread, std::thread());
185 bool joinable()
const noexcept {
return Thread.joinable(); }
187 id get_id()
const noexcept {
return Thread.get_id(); }
189 native_handle_type native_handle() noexcept {
return Thread.native_handle(); }
192 return std::thread::hardware_concurrency();
204namespace this_thread {
205 inline thread::id get_id() {
return std::this_thread::get_id(); }
221 template <
class Function,
class... Args>
224 f(std::forward<Args>(
args)...);
226 template <
class Function,
class... Args>
228 f(std::forward<Args>(
args)...);
234 "threading support");
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
This is an optimization pass for GlobalISel generic memory operations.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Implement std::hash so that hash_code can be used in STL containers.
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