#ifndef LLVM_SUPPORT_THREAD_H
#define LLVM_SUPPORT_THREAD_H
#include "llvm/Config/llvm-config.h"
#include <optional>
#ifdef _WIN32
typedef unsigned long DWORD;
typedef void *PVOID;
typedef PVOID HANDLE;
#endif
#if LLVM_ENABLE_THREADS
#include <thread>
namespace llvm {
#if LLVM_ON_UNIX || _WIN32
class thread { … };
thread::native_handle_type
llvm_execute_on_thread_impl(thread::start_routine_type ThreadFunc, void *Arg,
std::optional<unsigned> StackSizeInBytes);
void llvm_thread_join_impl(thread::native_handle_type Thread);
void llvm_thread_detach_impl(thread::native_handle_type Thread);
thread::id llvm_thread_get_id_impl(thread::native_handle_type Thread);
thread::id llvm_thread_get_current_id_impl();
template <class Function, class... Args>
thread::thread(std::optional<unsigned> StackSizeInBytes, Function &&f,
Args &&...args) { … }
thread::id thread::get_id() const noexcept { … }
void thread::join() { … }
void thread::detach() { … }
namespace this_thread {
inline thread::id get_id() { … }
}
#else
class thread {
public:
using native_handle_type = std::thread::native_handle_type;
using id = std::thread::id;
thread() : Thread(std::thread()) {}
thread(thread &&Other) noexcept
: Thread(std::exchange(Other.Thread, std::thread())) {}
template <class Function, class... Args>
explicit thread(std::optional<unsigned> StackSizeInBytes, Function &&f,
Args &&...args)
: Thread(std::forward<Function>(f), std::forward<Args>(args)...) {}
template <class Function, class... Args>
explicit thread(Function &&f, Args &&...args) : Thread(f, args...) {}
thread(const thread &) = delete;
~thread() {}
thread &operator=(thread &&Other) noexcept {
Thread = std::exchange(Other.Thread, std::thread());
return *this;
}
bool joinable() const noexcept { return Thread.joinable(); }
id get_id() const noexcept { return Thread.get_id(); }
native_handle_type native_handle() noexcept { return Thread.native_handle(); }
static unsigned hardware_concurrency() {
return std::thread::hardware_concurrency();
};
inline void join() { Thread.join(); }
inline void detach() { Thread.detach(); }
void swap(llvm::thread &Other) noexcept { std::swap(Thread, Other.Thread); }
private:
std::thread Thread;
};
namespace this_thread {
inline thread::id get_id() { return std::this_thread::get_id(); }
}
#endif
}
#else
#include <utility>
namespace llvm {
struct thread {
thread() {}
thread(thread &&other) {}
template <class Function, class... Args>
explicit thread(std::optional<unsigned> StackSizeInBytes, Function &&f,
Args &&...args) {
f(std::forward<Args>(args)...);
}
template <class Function, class... Args>
explicit thread(Function &&f, Args &&...args) {
f(std::forward<Args>(args)...);
}
thread(const thread &) = delete;
void detach() {
report_fatal_error("Detaching from a thread does not make sense with no "
"threading support");
}
void join() {}
static unsigned hardware_concurrency() { return 1; };
};
}
#endif
#endif