]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm-project/llvm/include/llvm/Support/ThreadPool.h
Merge llvm-project main llvmorg-14-init-11187-g222442ec2d71
[FreeBSD/FreeBSD.git] / contrib / llvm-project / llvm / include / llvm / Support / ThreadPool.h
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   /// Construct a pool using the hardware strategy \p S for mapping hardware
40   /// execution resources (threads, cores, CPUs)
41   /// Defaults to using the maximum execution resources in the system, but
42   /// accounting for the affinity mask.
43   ThreadPool(ThreadPoolStrategy S = hardware_concurrency());
44
45   /// Blocking destructor: the pool will wait for all the threads to complete.
46   ~ThreadPool();
47
48   /// Asynchronous submission of a task to the pool. The returned future can be
49   /// used to wait for the task to finish and is *non-blocking* on destruction.
50   template <typename Function, typename... Args>
51   inline auto async(Function &&F, Args &&...ArgList) {
52     auto Task =
53         std::bind(std::forward<Function>(F), std::forward<Args>(ArgList)...);
54     return async(std::move(Task));
55   }
56
57   /// Asynchronous submission of a task to the pool. The returned future can be
58   /// used to wait for the task to finish and is *non-blocking* on destruction.
59   template <typename Func>
60   auto async(Func &&F) -> std::shared_future<decltype(F())> {
61     return asyncImpl(std::function<decltype(F())()>(std::forward<Func>(F)));
62   }
63
64   /// Blocking wait for all the threads to complete and the queue to be empty.
65   /// It is an error to try to add new tasks while blocking on this call.
66   void wait();
67
68   unsigned getThreadCount() const { return ThreadCount; }
69
70   /// Returns true if the current thread is a worker thread of this thread pool.
71   bool isWorkerThread() const;
72
73 private:
74   /// Helpers to create a promise and a callable wrapper of \p Task that sets
75   /// the result of the promise. Returns the callable and a future to access the
76   /// result.
77   template <typename ResTy>
78   static std::pair<std::function<void()>, std::future<ResTy>>
79   createTaskAndFuture(std::function<ResTy()> Task) {
80     std::shared_ptr<std::promise<ResTy>> Promise =
81         std::make_shared<std::promise<ResTy>>();
82     auto F = Promise->get_future();
83     return {
84         [Promise = std::move(Promise), Task]() { Promise->set_value(Task()); },
85         std::move(F)};
86   }
87   static std::pair<std::function<void()>, std::future<void>>
88   createTaskAndFuture(std::function<void()> Task) {
89     std::shared_ptr<std::promise<void>> Promise =
90         std::make_shared<std::promise<void>>();
91     auto F = Promise->get_future();
92     return {[Promise = std::move(Promise), Task]() {
93               Task();
94               Promise->set_value();
95             },
96             std::move(F)};
97   }
98
99   bool workCompletedUnlocked() { return !ActiveThreads && Tasks.empty(); }
100
101   /// Asynchronous submission of a task to the pool. The returned future can be
102   /// used to wait for the task to finish and is *non-blocking* on destruction.
103   template <typename ResTy>
104   std::shared_future<ResTy> asyncImpl(std::function<ResTy()> Task) {
105
106 #if LLVM_ENABLE_THREADS
107     /// Wrap the Task in a std::function<void()> that sets the result of the
108     /// corresponding future.
109     auto R = createTaskAndFuture(Task);
110
111     {
112       // Lock the queue and push the new task
113       std::unique_lock<std::mutex> LockGuard(QueueLock);
114
115       // Don't allow enqueueing after disabling the pool
116       assert(EnableFlag && "Queuing a thread during ThreadPool destruction");
117       Tasks.push(std::move(R.first));
118     }
119     QueueCondition.notify_one();
120     return R.second.share();
121
122 #else // LLVM_ENABLE_THREADS Disabled
123
124     // Get a Future with launch::deferred execution using std::async
125     auto Future = std::async(std::launch::deferred, std::move(Task)).share();
126     // Wrap the future so that both ThreadPool::wait() can operate and the
127     // returned future can be sync'ed on.
128     Tasks.push([Future]() { Future.get(); });
129     return Future;
130 #endif
131   }
132
133   /// Threads in flight
134   std::vector<llvm::thread> Threads;
135
136   /// Tasks waiting for execution in the pool.
137   std::queue<std::function<void()>> Tasks;
138
139   /// Locking and signaling for accessing the Tasks queue.
140   std::mutex QueueLock;
141   std::condition_variable QueueCondition;
142
143   /// Signaling for job completion
144   std::condition_variable CompletionCondition;
145
146   /// Keep track of the number of thread actually busy
147   unsigned ActiveThreads = 0;
148
149 #if LLVM_ENABLE_THREADS // avoids warning for unused variable
150   /// Signal for the destruction of the pool, asking thread to exit.
151   bool EnableFlag = true;
152 #endif
153
154   unsigned ThreadCount;
155 };
156 }
157
158 #endif // LLVM_SUPPORT_THREADPOOL_H