2015-12-15 08:59:19 +08:00
|
|
|
//==-- llvm/Support/ThreadPool.cpp - A ThreadPool implementation -*- C++ -*-==//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2015-12-15 08:59:19 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements a crude C++11 based thread pool.
|
2016-06-23 02:01:11 +08:00
|
|
|
//
|
2015-12-15 08:59:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/ThreadPool.h"
|
|
|
|
|
|
|
|
#include "llvm/Config/llvm-config.h"
|
2017-10-05 04:27:01 +08:00
|
|
|
#include "llvm/Support/Threading.h"
|
2017-12-14 06:12:57 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-12-15 08:59:19 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#if LLVM_ENABLE_THREADS
|
|
|
|
|
2017-10-05 04:27:01 +08:00
|
|
|
// Default to hardware_concurrency
|
|
|
|
ThreadPool::ThreadPool() : ThreadPool(hardware_concurrency()) {}
|
2015-12-15 08:59:19 +08:00
|
|
|
|
|
|
|
ThreadPool::ThreadPool(unsigned ThreadCount)
|
|
|
|
: ActiveThreads(0), EnableFlag(true) {
|
|
|
|
// Create ThreadCount threads that will loop forever, wait on QueueCondition
|
|
|
|
// for tasks to be queued or the Pool to be destroyed.
|
|
|
|
Threads.reserve(ThreadCount);
|
|
|
|
for (unsigned ThreadID = 0; ThreadID < ThreadCount; ++ThreadID) {
|
|
|
|
Threads.emplace_back([&] {
|
|
|
|
while (true) {
|
2018-06-14 05:24:19 +08:00
|
|
|
PackagedTaskTy Task;
|
2015-12-15 08:59:19 +08:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> LockGuard(QueueLock);
|
|
|
|
// Wait for tasks to be pushed in the queue
|
|
|
|
QueueCondition.wait(LockGuard,
|
|
|
|
[&] { return !EnableFlag || !Tasks.empty(); });
|
|
|
|
// Exit condition
|
|
|
|
if (!EnableFlag && Tasks.empty())
|
|
|
|
return;
|
|
|
|
// Yeah, we have a task, grab it and release the lock on the queue
|
|
|
|
|
|
|
|
// We first need to signal that we are active before popping the queue
|
|
|
|
// in order for wait() to properly detect that even if the queue is
|
|
|
|
// empty, there is still a task in flight.
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> LockGuard(CompletionLock);
|
2017-11-27 21:42:03 +08:00
|
|
|
++ActiveThreads;
|
2015-12-15 08:59:19 +08:00
|
|
|
}
|
|
|
|
Task = std::move(Tasks.front());
|
|
|
|
Tasks.pop();
|
|
|
|
}
|
|
|
|
// Run the task we just grabbed
|
2018-06-14 05:24:19 +08:00
|
|
|
Task();
|
2015-12-15 08:59:19 +08:00
|
|
|
|
|
|
|
{
|
|
|
|
// Adjust `ActiveThreads`, in case someone waits on ThreadPool::wait()
|
|
|
|
std::unique_lock<std::mutex> LockGuard(CompletionLock);
|
|
|
|
--ActiveThreads;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify task completion, in case someone waits on ThreadPool::wait()
|
|
|
|
CompletionCondition.notify_all();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPool::wait() {
|
|
|
|
// Wait for all threads to complete and the queue to be empty
|
|
|
|
std::unique_lock<std::mutex> LockGuard(CompletionLock);
|
2016-04-07 07:46:40 +08:00
|
|
|
// The order of the checks for ActiveThreads and Tasks.empty() matters because
|
|
|
|
// any active threads might be modifying the Tasks queue, and this would be a
|
|
|
|
// race.
|
2015-12-15 08:59:19 +08:00
|
|
|
CompletionCondition.wait(LockGuard,
|
2016-04-07 07:46:40 +08:00
|
|
|
[&] { return !ActiveThreads && Tasks.empty(); });
|
2015-12-15 08:59:19 +08:00
|
|
|
}
|
|
|
|
|
2018-06-14 05:24:19 +08:00
|
|
|
std::shared_future<void> ThreadPool::asyncImpl(TaskTy Task) {
|
|
|
|
/// Wrap the Task in a packaged_task to return a future object.
|
|
|
|
PackagedTaskTy PackagedTask(std::move(Task));
|
|
|
|
auto Future = PackagedTask.get_future();
|
|
|
|
{
|
|
|
|
// Lock the queue and push the new task
|
|
|
|
std::unique_lock<std::mutex> LockGuard(QueueLock);
|
|
|
|
|
|
|
|
// Don't allow enqueueing after disabling the pool
|
|
|
|
assert(EnableFlag && "Queuing a thread during ThreadPool destruction");
|
|
|
|
|
|
|
|
Tasks.push(std::move(PackagedTask));
|
|
|
|
}
|
|
|
|
QueueCondition.notify_one();
|
|
|
|
return Future.share();
|
|
|
|
}
|
|
|
|
|
2015-12-15 08:59:19 +08:00
|
|
|
// The destructor joins all threads, waiting for completion.
|
|
|
|
ThreadPool::~ThreadPool() {
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> LockGuard(QueueLock);
|
|
|
|
EnableFlag = false;
|
|
|
|
}
|
|
|
|
QueueCondition.notify_all();
|
|
|
|
for (auto &Worker : Threads)
|
|
|
|
Worker.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else // LLVM_ENABLE_THREADS Disabled
|
|
|
|
|
|
|
|
ThreadPool::ThreadPool() : ThreadPool(0) {}
|
|
|
|
|
|
|
|
// No threads are launched, issue a warning if ThreadCount is not 0
|
|
|
|
ThreadPool::ThreadPool(unsigned ThreadCount)
|
|
|
|
: ActiveThreads(0) {
|
|
|
|
if (ThreadCount) {
|
|
|
|
errs() << "Warning: request a ThreadPool with " << ThreadCount
|
|
|
|
<< " threads, but LLVM_ENABLE_THREADS has been turned off\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadPool::wait() {
|
|
|
|
// Sequential implementation running the tasks
|
|
|
|
while (!Tasks.empty()) {
|
|
|
|
auto Task = std::move(Tasks.front());
|
|
|
|
Tasks.pop();
|
2017-06-14 08:36:21 +08:00
|
|
|
Task();
|
2015-12-15 08:59:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-14 08:36:21 +08:00
|
|
|
std::shared_future<void> ThreadPool::asyncImpl(TaskTy Task) {
|
2015-12-15 08:59:19 +08:00
|
|
|
// Get a Future with launch::deferred execution using std::async
|
|
|
|
auto Future = std::async(std::launch::deferred, std::move(Task)).share();
|
|
|
|
// Wrap the future so that both ThreadPool::wait() can operate and the
|
|
|
|
// returned future can be sync'ed on.
|
|
|
|
PackagedTaskTy PackagedTask([Future]() { Future.get(); });
|
|
|
|
Tasks.push(std::move(PackagedTask));
|
2016-11-28 17:17:12 +08:00
|
|
|
return Future;
|
2015-12-15 08:59:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ThreadPool::~ThreadPool() {
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|