blob: 40982d777914d346accb294d782725a3abd2cb16 [file] [log] [blame]
Mehdi Amini33a7ea42015-12-15 00:59:19 +00001//==-- llvm/Support/ThreadPool.cpp - A ThreadPool implementation -*- C++ -*-==//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Mehdi Amini33a7ea42015-12-15 00:59:19 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a crude C++11 based thread pool.
Jason Henline70378832016-06-22 18:01:11 +000010//
Mehdi Amini33a7ea42015-12-15 00:59:19 +000011//===----------------------------------------------------------------------===//
12
13#include "llvm/Support/ThreadPool.h"
14
15#include "llvm/Config/llvm-config.h"
Rafael Espindola8c0ff952017-10-04 20:27:01 +000016#include "llvm/Support/Threading.h"
Hans Wennborg86f0b702017-12-13 22:12:57 +000017#include "llvm/Support/raw_ostream.h"
Mehdi Amini33a7ea42015-12-15 00:59:19 +000018
19using namespace llvm;
20
21#if LLVM_ENABLE_THREADS
22
Rafael Espindola8c0ff952017-10-04 20:27:01 +000023// Default to hardware_concurrency
24ThreadPool::ThreadPool() : ThreadPool(hardware_concurrency()) {}
Mehdi Amini33a7ea42015-12-15 00:59:19 +000025
26ThreadPool::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([&] {
33 while (true) {
Zachary Turner9b8b0792018-06-13 21:24:19 +000034 PackagedTaskTy Task;
Mehdi Amini33a7ea42015-12-15 00:59:19 +000035 {
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(); });
40 // Exit condition
41 if (!EnableFlag && Tasks.empty())
42 return;
43 // Yeah, we have a task, grab it and release the lock on the queue
44
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.
48 {
Mehdi Amini33a7ea42015-12-15 00:59:19 +000049 std::unique_lock<std::mutex> LockGuard(CompletionLock);
Jan Korousc723f652017-11-27 13:42:03 +000050 ++ActiveThreads;
Mehdi Amini33a7ea42015-12-15 00:59:19 +000051 }
52 Task = std::move(Tasks.front());
53 Tasks.pop();
54 }
55 // Run the task we just grabbed
Zachary Turner9b8b0792018-06-13 21:24:19 +000056 Task();
Mehdi Amini33a7ea42015-12-15 00:59:19 +000057
58 {
59 // Adjust `ActiveThreads`, in case someone waits on ThreadPool::wait()
60 std::unique_lock<std::mutex> LockGuard(CompletionLock);
61 --ActiveThreads;
62 }
63
64 // Notify task completion, in case someone waits on ThreadPool::wait()
65 CompletionCondition.notify_all();
66 }
67 });
68 }
69}
70
71void ThreadPool::wait() {
72 // Wait for all threads to complete and the queue to be empty
73 std::unique_lock<std::mutex> LockGuard(CompletionLock);
Justin Lebar9e479e42016-04-06 23:46:40 +000074 // The order of the checks for ActiveThreads and Tasks.empty() matters because
75 // any active threads might be modifying the Tasks queue, and this would be a
76 // race.
Mehdi Amini33a7ea42015-12-15 00:59:19 +000077 CompletionCondition.wait(LockGuard,
Justin Lebar9e479e42016-04-06 23:46:40 +000078 [&] { return !ActiveThreads && Tasks.empty(); });
Mehdi Amini33a7ea42015-12-15 00:59:19 +000079}
80
Zachary Turner9b8b0792018-06-13 21:24:19 +000081std::shared_future<void> ThreadPool::asyncImpl(TaskTy Task) {
82 /// Wrap the Task in a packaged_task to return a future object.
83 PackagedTaskTy PackagedTask(std::move(Task));
84 auto Future = PackagedTask.get_future();
85 {
86 // Lock the queue and push the new task
87 std::unique_lock<std::mutex> LockGuard(QueueLock);
88
89 // Don't allow enqueueing after disabling the pool
90 assert(EnableFlag && "Queuing a thread during ThreadPool destruction");
91
92 Tasks.push(std::move(PackagedTask));
93 }
94 QueueCondition.notify_one();
95 return Future.share();
96}
97
Mehdi Amini33a7ea42015-12-15 00:59:19 +000098// The destructor joins all threads, waiting for completion.
99ThreadPool::~ThreadPool() {
100 {
101 std::unique_lock<std::mutex> LockGuard(QueueLock);
102 EnableFlag = false;
103 }
104 QueueCondition.notify_all();
105 for (auto &Worker : Threads)
106 Worker.join();
107}
108
109#else // LLVM_ENABLE_THREADS Disabled
110
111ThreadPool::ThreadPool() : ThreadPool(0) {}
112
113// No threads are launched, issue a warning if ThreadCount is not 0
114ThreadPool::ThreadPool(unsigned ThreadCount)
115 : ActiveThreads(0) {
116 if (ThreadCount) {
117 errs() << "Warning: request a ThreadPool with " << ThreadCount
118 << " threads, but LLVM_ENABLE_THREADS has been turned off\n";
119 }
120}
121
122void ThreadPool::wait() {
123 // Sequential implementation running the tasks
124 while (!Tasks.empty()) {
125 auto Task = std::move(Tasks.front());
126 Tasks.pop();
Peter Collingbourneb78a68d2017-06-14 00:36:21 +0000127 Task();
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000128 }
129}
130
Peter Collingbourneb78a68d2017-06-14 00:36:21 +0000131std::shared_future<void> ThreadPool::asyncImpl(TaskTy Task) {
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000132 // Get a Future with launch::deferred execution using std::async
133 auto Future = std::async(std::launch::deferred, std::move(Task)).share();
134 // Wrap the future so that both ThreadPool::wait() can operate and the
135 // returned future can be sync'ed on.
136 PackagedTaskTy PackagedTask([Future]() { Future.get(); });
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000137 Tasks.push(std::move(PackagedTask));
Davide Italiano0f0d5d82016-11-28 09:17:12 +0000138 return Future;
Mehdi Amini33a7ea42015-12-15 00:59:19 +0000139}
140
141ThreadPool::~ThreadPool() {
142 wait();
143}
144
145#endif