|
14 | 14 | #define LLVM_SUPPORT_THREADPOOL_H |
15 | 15 |
|
16 | 16 | #include "llvm/ADT/DenseMap.h" |
| 17 | +#include "llvm/ADT/FunctionExtras.h" |
17 | 18 | #include "llvm/Config/llvm-config.h" |
18 | 19 | #include "llvm/Support/Compiler.h" |
19 | 20 | #include "llvm/Support/Jobserver.h" |
@@ -51,7 +52,7 @@ class ThreadPoolTaskGroup; |
51 | 52 | class LLVM_ABI ThreadPoolInterface { |
52 | 53 | /// The actual method to enqueue a task to be defined by the concrete |
53 | 54 | /// implementation. |
54 | | - virtual void asyncEnqueue(std::function<void()> Task, |
| 55 | + virtual void asyncEnqueue(llvm::unique_function<void()> Task, |
55 | 56 | ThreadPoolTaskGroup *Group) = 0; |
56 | 57 |
|
57 | 58 | public: |
@@ -95,22 +96,22 @@ class LLVM_ABI ThreadPoolInterface { |
95 | 96 | /// used to wait for the task to finish and is *non-blocking* on destruction. |
96 | 97 | template <typename Func> |
97 | 98 | auto async(Func &&F) -> std::shared_future<decltype(F())> { |
98 | | - return asyncImpl(std::function<decltype(F())()>(std::forward<Func>(F)), |
99 | | - nullptr); |
| 99 | + return asyncImpl( |
| 100 | + llvm::unique_function<decltype(F())()>(std::forward<Func>(F)), nullptr); |
100 | 101 | } |
101 | 102 |
|
102 | 103 | template <typename Func> |
103 | 104 | auto async(ThreadPoolTaskGroup &Group, Func &&F) |
104 | 105 | -> std::shared_future<decltype(F())> { |
105 | | - return asyncImpl(std::function<decltype(F())()>(std::forward<Func>(F)), |
106 | | - &Group); |
| 106 | + return asyncImpl( |
| 107 | + llvm::unique_function<decltype(F())()>(std::forward<Func>(F)), &Group); |
107 | 108 | } |
108 | 109 |
|
109 | 110 | private: |
110 | 111 | /// Asynchronous submission of a task to the pool. The returned future can be |
111 | 112 | /// used to wait for the task to finish and is *non-blocking* on destruction. |
112 | 113 | template <typename ResTy> |
113 | | - std::shared_future<ResTy> asyncImpl(std::function<ResTy()> Task, |
| 114 | + std::shared_future<ResTy> asyncImpl(llvm::unique_function<ResTy()> Task, |
114 | 115 | ThreadPoolTaskGroup *Group) { |
115 | 116 | auto Future = std::async(std::launch::deferred, std::move(Task)).share(); |
116 | 117 | asyncEnqueue([Future]() { Future.wait(); }, Group); |
@@ -160,7 +161,7 @@ class LLVM_ABI StdThreadPool : public ThreadPoolInterface { |
160 | 161 |
|
161 | 162 | /// Asynchronous submission of a task to the pool. The returned future can be |
162 | 163 | /// used to wait for the task to finish and is *non-blocking* on destruction. |
163 | | - void asyncEnqueue(std::function<void()> Task, |
| 164 | + void asyncEnqueue(llvm::unique_function<void()> Task, |
164 | 165 | ThreadPoolTaskGroup *Group) override { |
165 | 166 | int requestedThreads; |
166 | 167 | { |
@@ -189,7 +190,8 @@ class LLVM_ABI StdThreadPool : public ThreadPoolInterface { |
189 | 190 | mutable llvm::sys::RWMutex ThreadsLock; |
190 | 191 |
|
191 | 192 | /// Tasks waiting for execution in the pool. |
192 | | - std::deque<std::pair<std::function<void()>, ThreadPoolTaskGroup *>> Tasks; |
| 193 | + std::deque<std::pair<llvm::unique_function<void()>, ThreadPoolTaskGroup *>> |
| 194 | + Tasks; |
193 | 195 |
|
194 | 196 | /// Locking and signaling for accessing the Tasks queue. |
195 | 197 | std::mutex QueueLock; |
@@ -239,13 +241,14 @@ class LLVM_ABI SingleThreadExecutor : public ThreadPoolInterface { |
239 | 241 | private: |
240 | 242 | /// Asynchronous submission of a task to the pool. The returned future can be |
241 | 243 | /// used to wait for the task to finish and is *non-blocking* on destruction. |
242 | | - void asyncEnqueue(std::function<void()> Task, |
| 244 | + void asyncEnqueue(llvm::unique_function<void()> Task, |
243 | 245 | ThreadPoolTaskGroup *Group) override { |
244 | 246 | Tasks.emplace_back(std::make_pair(std::move(Task), Group)); |
245 | 247 | } |
246 | 248 |
|
247 | 249 | /// Tasks waiting for execution in the pool. |
248 | | - std::deque<std::pair<std::function<void()>, ThreadPoolTaskGroup *>> Tasks; |
| 250 | + std::deque<std::pair<llvm::unique_function<void()>, ThreadPoolTaskGroup *>> |
| 251 | + Tasks; |
249 | 252 | }; |
250 | 253 |
|
251 | 254 | #if LLVM_ENABLE_THREADS |
|
0 commit comments