LLVM  14.0.0git
ThreadPool.h
Go to the documentation of this file.
1 //===-- llvm/Support/ThreadPool.h - A ThreadPool implementation -*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines a crude C++11 based thread pool.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_SUPPORT_THREADPOOL_H
14 #define LLVM_SUPPORT_THREADPOOL_H
15 
16 #include "llvm/Config/llvm-config.h"
17 #include "llvm/Support/Threading.h"
18 #include "llvm/Support/thread.h"
19 
20 #include <future>
21 
22 #include <atomic>
23 #include <condition_variable>
24 #include <functional>
25 #include <memory>
26 #include <mutex>
27 #include <queue>
28 #include <utility>
29 
30 namespace llvm {
31 
32 /// A ThreadPool for asynchronous parallel execution on a defined number of
33 /// threads.
34 ///
35 /// The pool keeps a vector of threads alive, waiting on a condition variable
36 /// for some work to become available.
37 class ThreadPool {
38 public:
39  using TaskTy = std::function<void()>;
40  using PackagedTaskTy = std::packaged_task<void()>;
41 
42  /// Construct a pool using the hardware strategy \p S for mapping hardware
43  /// execution resources (threads, cores, CPUs)
44  /// Defaults to using the maximum execution resources in the system, but
45  /// accounting for the affinity mask.
47 
48  /// Blocking destructor: the pool will wait for all the threads to complete.
49  ~ThreadPool();
50 
51  /// Asynchronous submission of a task to the pool. The returned future can be
52  /// used to wait for the task to finish and is *non-blocking* on destruction.
53  template <typename Function, typename... Args>
54  inline std::shared_future<void> async(Function &&F, Args &&... ArgList) {
55  auto Task =
56  std::bind(std::forward<Function>(F), std::forward<Args>(ArgList)...);
57  return asyncImpl(std::move(Task));
58  }
59 
60  /// Asynchronous submission of a task to the pool. The returned future can be
61  /// used to wait for the task to finish and is *non-blocking* on destruction.
62  template <typename Function>
63  inline std::shared_future<void> async(Function &&F) {
64  return asyncImpl(std::forward<Function>(F));
65  }
66 
67  /// Blocking wait for all the threads to complete and the queue to be empty.
68  /// It is an error to try to add new tasks while blocking on this call.
69  void wait();
70 
71  unsigned getThreadCount() const { return ThreadCount; }
72 
73  /// Returns true if the current thread is a worker thread of this thread pool.
74  bool isWorkerThread() const;
75 
76 private:
77  bool workCompletedUnlocked() { return !ActiveThreads && Tasks.empty(); }
78 
79  /// Asynchronous submission of a task to the pool. The returned future can be
80  /// used to wait for the task to finish and is *non-blocking* on destruction.
81  std::shared_future<void> asyncImpl(TaskTy F);
82 
83  /// Threads in flight
84  std::vector<llvm::thread> Threads;
85 
86  /// Tasks waiting for execution in the pool.
87  std::queue<PackagedTaskTy> Tasks;
88 
89  /// Locking and signaling for accessing the Tasks queue.
90  std::mutex QueueLock;
91  std::condition_variable QueueCondition;
92 
93  /// Signaling for job completion
94  std::condition_variable CompletionCondition;
95 
96  /// Keep track of the number of thread actually busy
97  unsigned ActiveThreads = 0;
98 
99 #if LLVM_ENABLE_THREADS // avoids warning for unused variable
100  /// Signal for the destruction of the pool, asking thread to exit.
101  bool EnableFlag = true;
102 #endif
103 
104  unsigned ThreadCount;
105 };
106 }
107 
108 #endif // LLVM_SUPPORT_THREADPOOL_H
llvm::hardware_concurrency
ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount=0)
Returns a default thread strategy where all available hardware resources are to be used,...
Definition: Threading.h:188
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::ThreadPool::PackagedTaskTy
std::packaged_task< void()> PackagedTaskTy
Definition: ThreadPool.h:40
llvm::ThreadPool
A ThreadPool for asynchronous parallel execution on a defined number of threads.
Definition: ThreadPool.h:37
llvm::Function
Definition: Function.h:61
llvm::ThreadPoolStrategy
This tells how a thread pool will be used.
Definition: Threading.h:119
llvm::ThreadPool::ThreadPool
ThreadPool(ThreadPoolStrategy S=hardware_concurrency())
Construct a pool using the hardware strategy S for mapping hardware execution resources (threads,...
Definition: ThreadPool.cpp:23
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::ThreadPool::getThreadCount
unsigned getThreadCount() const
Definition: ThreadPool.h:71
Threading.h
llvm::ThreadPool::async
std::shared_future< void > async(Function &&F)
Asynchronous submission of a task to the pool.
Definition: ThreadPool.h:63
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::ThreadPool::isWorkerThread
bool isWorkerThread() const
Returns true if the current thread is a worker thread of this thread pool.
Definition: ThreadPool.cpp:75
llvm::ThreadPool::~ThreadPool
~ThreadPool()
Blocking destructor: the pool will wait for all the threads to complete.
Definition: ThreadPool.cpp:101
llvm::ThreadPool::TaskTy
std::function< void()> TaskTy
Definition: ThreadPool.h:39
function
print Print MemDeps of function
Definition: MemDepPrinter.cpp:83
llvm::ThreadPool::async
std::shared_future< void > async(Function &&F, Args &&... ArgList)
Asynchronous submission of a task to the pool.
Definition: ThreadPool.h:54
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::ThreadPool::wait
void wait()
Blocking wait for all the threads to complete and the queue to be empty.
Definition: ThreadPool.cpp:69
thread.h
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389