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 LLVM_ON_UNIX || _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 start_routine_type =
void *(*)(
void *);
57 template <
typename CalleeTuple>
static void *ThreadProxy(
void *
Ptr) {
58 GenericThreadProxy<CalleeTuple>(
Ptr);
62 using native_handle_type = HANDLE;
64 using start_routine_type =
unsigned(__stdcall *)(
void *);
66 template <
typename CalleeTuple>
67 static unsigned __stdcall ThreadProxy(
void *
Ptr) {
68 GenericThreadProxy<CalleeTuple>(
Ptr);
73 LLVM_ABI static const std::optional<unsigned> DefaultStackSize;
77 :
Thread(std::exchange(
Other.Thread, native_handle_type())) {}
84 explicit thread(std::optional<unsigned> StackSizeInBytes, Function &&f,
96 Thread = std::exchange(
Other.Thread, native_handle_type());
100 bool joinable()
const noexcept {
return Thread != native_handle_type(); }
102 inline id get_id()
const noexcept;
104 native_handle_type native_handle()
const noexcept {
return Thread; }
107 return std::thread::hardware_concurrency();
116 native_handle_type
Thread;
120llvm_execute_on_thread_impl(thread::start_routine_type ThreadFunc,
void *Arg,
121 std::optional<unsigned> StackSizeInBytes);
122LLVM_ABI void llvm_thread_join_impl(thread::native_handle_type
Thread);
123LLVM_ABI void llvm_thread_detach_impl(thread::native_handle_type
Thread);
124LLVM_ABI thread::id llvm_thread_get_id_impl(thread::native_handle_type
Thread);
125LLVM_ABI thread::id llvm_thread_get_current_id_impl();
128thread::thread(std::optional<unsigned> StackSizeInBytes, Function &&f,
130 typedef std::tuple<std::decay_t<Function>, std::decay_t<Args>...> CalleeTuple;
131 std::unique_ptr<CalleeTuple>
Callee(
132 new CalleeTuple(std::forward<Function>(f), std::forward<Args>(
args)...));
134 Thread = llvm_execute_on_thread_impl(ThreadProxy<CalleeTuple>,
Callee.get(),
136 if (
Thread != native_handle_type())
140thread::id thread::get_id()
const noexcept {
141 return llvm_thread_get_id_impl(
Thread);
145 llvm_thread_join_impl(
Thread);
146 Thread = native_handle_type();
150 llvm_thread_detach_impl(
Thread);
151 Thread = native_handle_type();
154namespace this_thread {
155inline thread::id get_id() {
return llvm_thread_get_current_id_impl(); }
164 using native_handle_type = std::thread::native_handle_type;
165 using id = std::thread::id;
169 :
Thread(std::exchange(
Other.Thread, std::thread())) {}
172 explicit thread(std::optional<unsigned> StackSizeInBytes, Function &&f,
184 Thread = std::exchange(
Other.Thread, std::thread());
188 bool joinable()
const noexcept {
return Thread.joinable(); }
190 id get_id()
const noexcept {
return Thread.get_id(); }
192 native_handle_type native_handle() noexcept {
return Thread.native_handle(); }
195 return std::thread::hardware_concurrency();
207namespace this_thread {
208inline thread::id get_id() {
return std::this_thread::get_id(); }
225 template <
class Function,
class... Args>
228 f(std::forward<Args>(
args)...);
230 template <
class Function,
class... Args>
232 f(std::forward<Args>(
args)...);
238 "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)
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