13#ifndef LLVM_SUPPORT_THREADPOOL_H
14#define LLVM_SUPPORT_THREADPOOL_H
17#include "llvm/Config/llvm-config.h"
24#include <condition_variable>
33class ThreadPoolTaskGroup;
52 virtual void asyncEnqueue(std::function<
void()> Task,
77 template <
typename Function,
typename... Args>
80 std::bind(std::forward<Function>(
F), std::forward<Args>(ArgList)...);
81 return async(std::move(Task));
85 template <
typename Function,
typename... Args>
88 std::bind(std::forward<Function>(
F), std::forward<Args>(ArgList)...);
89 return async(Group, std::move(Task));
94 template <
typename Func>
95 auto async(Func &&
F) -> std::shared_future<
decltype(
F())> {
96 return asyncImpl(std::function<
decltype(
F())()>(std::forward<Func>(
F)),
100 template <
typename Func>
102 -> std::shared_future<
decltype(
F())> {
103 return asyncImpl(std::function<
decltype(
F())()>(std::forward<Func>(
F)),
110 template <
typename ResTy>
111 std::shared_future<ResTy> asyncImpl(std::function<ResTy()> Task,
113 auto Future = std::async(std::launch::deferred, std::move(Task)).share();
114 asyncEnqueue([Future]() { Future.wait(); }, Group);
119#if LLVM_ENABLE_THREADS
124class StdThreadPool :
public ThreadPoolInterface {
133 ~StdThreadPool()
override;
138 void wait()
override;
145 void wait(ThreadPoolTaskGroup &Group)
override;
149 unsigned getMaxConcurrency()
const override {
return MaxThreadCount; }
153 unsigned getThreadCount()
const {
return MaxThreadCount; }
156 bool isWorkerThread()
const;
161 bool workCompletedUnlocked(ThreadPoolTaskGroup *Group)
const;
165 void asyncEnqueue(std::function<
void()> Task,
166 ThreadPoolTaskGroup *Group)
override {
167 int requestedThreads;
170 std::unique_lock<std::mutex> LockGuard(QueueLock);
173 assert(EnableFlag &&
"Queuing a thread during ThreadPool destruction");
174 Tasks.emplace_back(std::make_pair(std::move(Task), Group));
175 requestedThreads = ActiveThreads + Tasks.size();
177 QueueCondition.notify_one();
178 grow(requestedThreads);
183 void grow(
int requested);
185 void processTasks(ThreadPoolTaskGroup *WaitingForGroup);
188 std::vector<llvm::thread> Threads;
193 std::deque<std::pair<std::function<void()>, ThreadPoolTaskGroup *>> Tasks;
196 std::mutex QueueLock;
197 std::condition_variable QueueCondition;
200 std::condition_variable CompletionCondition;
203 unsigned ActiveThreads = 0;
205 DenseMap<ThreadPoolTaskGroup *, unsigned> ActiveGroups;
208 bool EnableFlag =
true;
210 const ThreadPoolStrategy Strategy;
213 const unsigned MaxThreadCount;
227 void wait()
override;
245 void asyncEnqueue(std::function<
void()> Task,
247 Tasks.emplace_back(std::make_pair(std::move(Task), Group));
251 std::deque<std::pair<std::function<void()>, ThreadPoolTaskGroup *>> Tasks;
254#if LLVM_ENABLE_THREADS
274 template <
typename Function,
typename... Args>
276 return Pool.
async(*
this, std::forward<Function>(
F),
277 std::forward<Args>(ArgList)...);
#define LLVM_DEPRECATED(MSG, FIX)
Looks at all the uses of the given value Returns the Liveness deduced from the uses of this value Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses If the result is MaybeLiveUses might be modified but its content should be ignored(since it might not be complete). DeadArgumentEliminationPass
This file defines the DenseMap class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A non-threaded implementation.
void wait() override
Blocking wait for all the tasks to execute first.
~SingleThreadExecutor() override
Blocking destructor: the pool will first execute the pending tasks.
unsigned getThreadCount() const
bool isWorkerThread() const
Returns true if the current thread is a worker thread of this thread pool.
unsigned getMaxConcurrency() const override
Returns always 1: there is no concurrency.
This defines the abstract base interface for a ThreadPool allowing asynchronous parallel execution on...
auto async(ThreadPoolTaskGroup &Group, Function &&F, Args &&...ArgList)
Overload, task will be in the given task group.
virtual void wait()=0
Blocking wait for all the threads to complete and the queue to be empty.
auto async(ThreadPoolTaskGroup &Group, Func &&F) -> std::shared_future< decltype(F())>
virtual unsigned getMaxConcurrency() const =0
Returns the maximum number of worker this pool can eventually grow to.
auto async(Func &&F) -> std::shared_future< decltype(F())>
Asynchronous submission of a task to the pool.
virtual ~ThreadPoolInterface()
Destroying the pool will drain the pending tasks and wait.
auto async(Function &&F, Args &&...ArgList)
Asynchronous submission of a task to the pool.
virtual void wait(ThreadPoolTaskGroup &Group)=0
Blocking wait for only all the threads in the given group to complete.
This tells how a thread pool will be used.
A group of tasks to be run on a thread pool.
auto async(Function &&F, Args &&...ArgList)
Calls ThreadPool::async() for this group.
void wait()
Calls ThreadPool::wait() for this group.
~ThreadPoolTaskGroup()
Blocking destructor: will wait for all the tasks in the group to complete by calling ThreadPool::wait...
ThreadPoolTaskGroup(ThreadPoolInterface &Pool)
The ThreadPool argument is the thread pool to forward calls to.
This is an optimization pass for GlobalISel generic memory operations.
ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount=0)
Returns a default thread strategy where all available hardware resources are to be used,...
SingleThreadExecutor DefaultThreadPool