2017-05-11 08:03:52 +08:00
|
|
|
//===- llvm/Support/Parallel.cpp - Parallel algorithms --------------------===//
|
2017-05-06 05:09:26 +08:00
|
|
|
//
|
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
|
2017-05-06 05:09:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-05-11 08:03:52 +08:00
|
|
|
#include "llvm/Support/Parallel.h"
|
2017-05-06 05:14:55 +08:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2018-05-11 23:25:38 +08:00
|
|
|
|
|
|
|
#if LLVM_ENABLE_THREADS
|
|
|
|
|
2017-10-05 04:27:01 +08:00
|
|
|
#include "llvm/Support/Threading.h"
|
2017-05-06 05:09:26 +08:00
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <stack>
|
2017-05-06 05:27:30 +08:00
|
|
|
#include <thread>
|
2017-05-06 05:09:26 +08:00
|
|
|
|
2019-04-25 19:33:30 +08:00
|
|
|
namespace llvm {
|
|
|
|
namespace parallel {
|
|
|
|
namespace detail {
|
2017-05-06 05:09:26 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-05-01 23:54:18 +08:00
|
|
|
/// An abstract class that takes closures and runs them asynchronously.
|
2017-05-06 05:09:26 +08:00
|
|
|
class Executor {
|
|
|
|
public:
|
|
|
|
virtual ~Executor() = default;
|
|
|
|
virtual void add(std::function<void()> func) = 0;
|
|
|
|
|
|
|
|
static Executor *getDefaultExecutor();
|
|
|
|
};
|
|
|
|
|
2018-05-11 23:25:38 +08:00
|
|
|
#if defined(_MSC_VER)
|
2018-05-01 23:54:18 +08:00
|
|
|
/// An Executor that runs tasks via ConcRT.
|
2017-05-06 05:09:26 +08:00
|
|
|
class ConcRTExecutor : public Executor {
|
|
|
|
struct Taskish {
|
|
|
|
Taskish(std::function<void()> Task) : Task(Task) {}
|
|
|
|
|
|
|
|
std::function<void()> Task;
|
|
|
|
|
|
|
|
static void run(void *P) {
|
|
|
|
Taskish *Self = static_cast<Taskish *>(P);
|
|
|
|
Self->Task();
|
|
|
|
concurrency::Free(Self);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual void add(std::function<void()> F) {
|
|
|
|
Concurrency::CurrentScheduler::ScheduleTask(
|
|
|
|
Taskish::run, new (concurrency::Alloc(sizeof(Taskish))) Taskish(F));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Executor *Executor::getDefaultExecutor() {
|
|
|
|
static ConcRTExecutor exec;
|
|
|
|
return &exec;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
2018-05-01 23:54:18 +08:00
|
|
|
/// An implementation of an Executor that runs closures on a thread pool
|
2017-05-06 05:09:26 +08:00
|
|
|
/// in filo order.
|
|
|
|
class ThreadPoolExecutor : public Executor {
|
|
|
|
public:
|
2017-10-05 04:27:01 +08:00
|
|
|
explicit ThreadPoolExecutor(unsigned ThreadCount = hardware_concurrency())
|
2017-05-06 05:09:26 +08:00
|
|
|
: Done(ThreadCount) {
|
|
|
|
// Spawn all but one of the threads in another thread as spawning threads
|
|
|
|
// can take a while.
|
|
|
|
std::thread([&, ThreadCount] {
|
|
|
|
for (size_t i = 1; i < ThreadCount; ++i) {
|
|
|
|
std::thread([=] { work(); }).detach();
|
|
|
|
}
|
|
|
|
work();
|
|
|
|
}).detach();
|
|
|
|
}
|
|
|
|
|
|
|
|
~ThreadPoolExecutor() override {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
Stop = true;
|
|
|
|
Lock.unlock();
|
|
|
|
Cond.notify_all();
|
|
|
|
// Wait for ~Latch.
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(std::function<void()> F) override {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
WorkStack.push(F);
|
|
|
|
Lock.unlock();
|
|
|
|
Cond.notify_one();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void work() {
|
|
|
|
while (true) {
|
|
|
|
std::unique_lock<std::mutex> Lock(Mutex);
|
|
|
|
Cond.wait(Lock, [&] { return Stop || !WorkStack.empty(); });
|
|
|
|
if (Stop)
|
|
|
|
break;
|
|
|
|
auto Task = WorkStack.top();
|
|
|
|
WorkStack.pop();
|
|
|
|
Lock.unlock();
|
|
|
|
Task();
|
|
|
|
}
|
|
|
|
Done.dec();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::atomic<bool> Stop{false};
|
|
|
|
std::stack<std::function<void()>> WorkStack;
|
|
|
|
std::mutex Mutex;
|
|
|
|
std::condition_variable Cond;
|
2017-05-11 08:22:18 +08:00
|
|
|
parallel::detail::Latch Done;
|
2017-05-06 05:09:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Executor *Executor::getDefaultExecutor() {
|
|
|
|
static ThreadPoolExecutor exec;
|
|
|
|
return &exec;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-25 19:33:30 +08:00
|
|
|
static std::atomic<int> TaskGroupInstances;
|
|
|
|
|
|
|
|
// Latch::sync() called by the dtor may cause one thread to block. If is a dead
|
|
|
|
// lock if all threads in the default executor are blocked. To prevent the dead
|
|
|
|
// lock, only allow the first TaskGroup to run tasks parallelly. In the scenario
|
|
|
|
// of nested parallel_for_each(), only the outermost one runs parallelly.
|
|
|
|
TaskGroup::TaskGroup() : Parallel(TaskGroupInstances++ == 0) {}
|
|
|
|
TaskGroup::~TaskGroup() { --TaskGroupInstances; }
|
|
|
|
|
|
|
|
void TaskGroup::spawn(std::function<void()> F) {
|
|
|
|
if (Parallel) {
|
|
|
|
L.inc();
|
|
|
|
Executor::getDefaultExecutor()->add([&, F] {
|
|
|
|
F();
|
|
|
|
L.dec();
|
|
|
|
});
|
|
|
|
} else {
|
2017-05-11 01:39:18 +08:00
|
|
|
F();
|
2019-04-25 19:33:30 +08:00
|
|
|
}
|
2017-05-06 05:09:26 +08:00
|
|
|
}
|
2019-04-25 19:33:30 +08:00
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
} // namespace parallel
|
|
|
|
} // namespace llvm
|
2018-05-11 23:25:38 +08:00
|
|
|
#endif // LLVM_ENABLE_THREADS
|