1 //==-- llvm/Support/ThreadPool.cpp - A ThreadPool implementation -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a crude C++11 based thread pool.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/ThreadPool.h"
16 #include "llvm/Config/llvm-config.h"
17 #include "llvm/Support/raw_ostream.h"
21 #if LLVM_ENABLE_THREADS
23 // Default to std::thread::hardware_concurrency
24 ThreadPool::ThreadPool() : ThreadPool(std::thread::hardware_concurrency()) {}
26 ThreadPool::ThreadPool(unsigned ThreadCount)
27 : ActiveThreads(0), EnableFlag(true) {
28 // Create ThreadCount threads that will loop forever, wait on QueueCondition
29 // for tasks to be queued or the Pool to be destroyed.
30 Threads.reserve(ThreadCount);
31 for (unsigned ThreadID = 0; ThreadID < ThreadCount; ++ThreadID) {
32 Threads.emplace_back([&] {
34 std::packaged_task<void()> Task;
36 std::unique_lock<std::mutex> LockGuard(QueueLock);
37 // Wait for tasks to be pushed in the queue
38 QueueCondition.wait(LockGuard,
39 [&] { return !EnableFlag || !Tasks.empty(); });
41 if (!EnableFlag && Tasks.empty())
43 // Yeah, we have a task, grab it and release the lock on the queue
45 // We first need to signal that we are active before popping the queue
46 // in order for wait() to properly detect that even if the queue is
47 // empty, there is still a task in flight.
50 std::unique_lock<std::mutex> LockGuard(CompletionLock);
52 Task = std::move(Tasks.front());
55 // Run the task we just grabbed
59 // Adjust `ActiveThreads`, in case someone waits on ThreadPool::wait()
60 std::unique_lock<std::mutex> LockGuard(CompletionLock);
64 // Notify task completion, in case someone waits on ThreadPool::wait()
65 CompletionCondition.notify_all();
71 void ThreadPool::wait() {
72 // Wait for all threads to complete and the queue to be empty
73 std::unique_lock<std::mutex> LockGuard(CompletionLock);
74 CompletionCondition.wait(LockGuard,
75 [&] { return Tasks.empty() && !ActiveThreads; });
78 std::shared_future<void> ThreadPool::asyncImpl(TaskTy Task) {
79 /// Wrap the Task in a packaged_task to return a future object.
80 std::packaged_task<void()> PackagedTask(std::move(Task));
81 std::future<void> Future = PackagedTask.get_future();
83 // Lock the queue and push the new task
84 std::unique_lock<std::mutex> LockGuard(QueueLock);
86 // Don't allow enqueueing after disabling the pool
87 assert(EnableFlag && "Queuing a thread during ThreadPool destruction");
89 Tasks.push(std::move(PackagedTask));
91 QueueCondition.notify_one();
92 return Future.share();
95 // The destructor joins all threads, waiting for completion.
96 ThreadPool::~ThreadPool() {
98 std::unique_lock<std::mutex> LockGuard(QueueLock);
101 QueueCondition.notify_all();
102 for (auto &Worker : Threads)
106 #else // LLVM_ENABLE_THREADS Disabled
108 ThreadPool::ThreadPool() : ThreadPool(0) {}
110 // No threads are launched, issue a warning if ThreadCount is not 0
111 ThreadPool::ThreadPool(unsigned ThreadCount)
112 : ActiveThreads(0), EnableFlag(true) {
114 errs() << "Warning: request a ThreadPool with " << ThreadCount
115 << " threads, but LLVM_ENABLE_THREADS has been turned off\n";
119 void ThreadPool::wait() {
120 // Sequential implementation running the tasks
121 while (!Tasks.empty()) {
122 auto Task = std::move(Tasks.front());
128 std::shared_future<void> ThreadPool::asyncImpl(TaskTy Task) {
129 // Get a Future with launch::deferred execution using std::async
130 auto Future = std::async(std::launch::deferred, std::move(Task)).share();
131 // Wrap the future so that both ThreadPool::wait() can operate and the
132 // returned future can be sync'ed on.
133 std::packaged_task<void()> PackagedTask([Future]() { Future.get(); });
134 Tasks.push(std::move(PackagedTask));
138 ThreadPool::~ThreadPool() {