13#ifndef LLVM_SUPPORT_TASKQUEUE_H
14#define LLVM_SUPPORT_TASKQUEUE_H
16#include "llvm/Config/llvm-config.h"
22#include <condition_variable>
40 template <
typename Callable>
struct Task {
41 using ResultTy = std::invoke_result_t<Callable>;
42 explicit Task(Callable C,
TaskQueue &Parent)
43 : C(std::move(C)),
P(std::make_shared<std::promise<ResultTy>>()),
47 void invokeCallbackAndSetPromise(
T*) {
51 void invokeCallbackAndSetPromise(
void*) {
56 void operator()()
noexcept {
57 ResultTy *Dummy =
nullptr;
58 invokeCallbackAndSetPromise(Dummy);
59 Parent->completeTask();
63 std::shared_ptr<std::promise<ResultTy>>
P;
80 template <
typename Callable>
81 std::future<std::invoke_result_t<Callable>>
async(Callable &&
C) {
82#if !LLVM_ENABLE_THREADS
84 "TaskQueue requires building with LLVM_ENABLE_THREADS!");
86 Task<Callable>
T{std::move(
C), *
this};
87 using ResultTy = std::invoke_result_t<Callable>;
88 std::future<ResultTy>
F =
T.P->get_future();
90 std::lock_guard<std::mutex> Lock(QueueLock);
95 Tasks.push_back(std::move(
T));
97 Scheduler.
async(std::move(
T));
98 IsTaskInFlight =
true;
105 void completeTask() {
109 std::function<void()> Continuation;
111 std::lock_guard<std::mutex> Lock(QueueLock);
113 IsTaskInFlight =
false;
117 Continuation = std::move(Tasks.front());
120 Scheduler.
async(std::move(Continuation));
124 ThreadPool &Scheduler;
128 bool IsTaskInFlight =
false;
131 std::mutex QueueLock;
134 std::deque<std::function<void()>> Tasks;
Machine Instruction Scheduler
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
TaskQueue executes serialized work on a user-defined Thread Pool.
TaskQueue(ThreadPool &Scheduler)
Construct a task queue with no work.
~TaskQueue()
Blocking destructor: the queue will wait for all work to complete.
std::future< std::invoke_result_t< Callable > > async(Callable &&C)
Asynchronous submission of a task to the queue.
A ThreadPool for asynchronous parallel execution on a defined number of threads.
void wait()
Blocking wait for all the threads to complete and the queue to be empty.
auto async(Function &&F, Args &&...ArgList)
Asynchronous submission of a task to the pool.
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.