Zachary Turner | 3a57fbd | 2017-05-11 00:03:52 +0000 | [diff] [blame] | 1 | //===- llvm/Support/Parallel.cpp - Parallel algorithms --------------------===// |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 2 | // |
Zachary Turner | 3a57fbd | 2017-05-11 00:03:52 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Zachary Turner | 3a57fbd | 2017-05-11 00:03:52 +0000 | [diff] [blame] | 10 | #include "llvm/Support/Parallel.h" |
Zachary Turner | 6083b2b | 2017-05-05 21:14:55 +0000 | [diff] [blame] | 11 | #include "llvm/Config/llvm-config.h" |
Rafael Espindola | 8c0ff95 | 2017-10-04 20:27:01 +0000 | [diff] [blame] | 12 | #include "llvm/Support/Threading.h" |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 13 | |
| 14 | #include <atomic> |
| 15 | #include <stack> |
Rui Ueyama | 552c290 | 2017-05-05 21:27:30 +0000 | [diff] [blame] | 16 | #include <thread> |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 17 | |
Zachary Turner | 3a57fbd | 2017-05-11 00:03:52 +0000 | [diff] [blame] | 18 | using namespace llvm; |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 19 | |
| 20 | namespace { |
| 21 | |
| 22 | /// \brief An abstract class that takes closures and runs them asynchronously. |
| 23 | class Executor { |
| 24 | public: |
| 25 | virtual ~Executor() = default; |
| 26 | virtual void add(std::function<void()> func) = 0; |
| 27 | |
| 28 | static Executor *getDefaultExecutor(); |
| 29 | }; |
| 30 | |
| 31 | #if !LLVM_ENABLE_THREADS |
| 32 | class SyncExecutor : public Executor { |
| 33 | public: |
| 34 | virtual void add(std::function<void()> F) { F(); } |
| 35 | }; |
| 36 | |
| 37 | Executor *Executor::getDefaultExecutor() { |
| 38 | static SyncExecutor Exec; |
| 39 | return &Exec; |
| 40 | } |
| 41 | |
| 42 | #elif defined(_MSC_VER) |
| 43 | /// \brief An Executor that runs tasks via ConcRT. |
| 44 | class ConcRTExecutor : public Executor { |
| 45 | struct Taskish { |
| 46 | Taskish(std::function<void()> Task) : Task(Task) {} |
| 47 | |
| 48 | std::function<void()> Task; |
| 49 | |
| 50 | static void run(void *P) { |
| 51 | Taskish *Self = static_cast<Taskish *>(P); |
| 52 | Self->Task(); |
| 53 | concurrency::Free(Self); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | public: |
| 58 | virtual void add(std::function<void()> F) { |
| 59 | Concurrency::CurrentScheduler::ScheduleTask( |
| 60 | Taskish::run, new (concurrency::Alloc(sizeof(Taskish))) Taskish(F)); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | Executor *Executor::getDefaultExecutor() { |
| 65 | static ConcRTExecutor exec; |
| 66 | return &exec; |
| 67 | } |
| 68 | |
| 69 | #else |
| 70 | /// \brief An implementation of an Executor that runs closures on a thread pool |
| 71 | /// in filo order. |
| 72 | class ThreadPoolExecutor : public Executor { |
| 73 | public: |
Rafael Espindola | 8c0ff95 | 2017-10-04 20:27:01 +0000 | [diff] [blame] | 74 | explicit ThreadPoolExecutor(unsigned ThreadCount = hardware_concurrency()) |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 75 | : Done(ThreadCount) { |
| 76 | // Spawn all but one of the threads in another thread as spawning threads |
| 77 | // can take a while. |
| 78 | std::thread([&, ThreadCount] { |
| 79 | for (size_t i = 1; i < ThreadCount; ++i) { |
| 80 | std::thread([=] { work(); }).detach(); |
| 81 | } |
| 82 | work(); |
| 83 | }).detach(); |
| 84 | } |
| 85 | |
| 86 | ~ThreadPoolExecutor() override { |
| 87 | std::unique_lock<std::mutex> Lock(Mutex); |
| 88 | Stop = true; |
| 89 | Lock.unlock(); |
| 90 | Cond.notify_all(); |
| 91 | // Wait for ~Latch. |
| 92 | } |
| 93 | |
| 94 | void add(std::function<void()> F) override { |
| 95 | std::unique_lock<std::mutex> Lock(Mutex); |
| 96 | WorkStack.push(F); |
| 97 | Lock.unlock(); |
| 98 | Cond.notify_one(); |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | void work() { |
| 103 | while (true) { |
| 104 | std::unique_lock<std::mutex> Lock(Mutex); |
| 105 | Cond.wait(Lock, [&] { return Stop || !WorkStack.empty(); }); |
| 106 | if (Stop) |
| 107 | break; |
| 108 | auto Task = WorkStack.top(); |
| 109 | WorkStack.pop(); |
| 110 | Lock.unlock(); |
| 111 | Task(); |
| 112 | } |
| 113 | Done.dec(); |
| 114 | } |
| 115 | |
| 116 | std::atomic<bool> Stop{false}; |
| 117 | std::stack<std::function<void()>> WorkStack; |
| 118 | std::mutex Mutex; |
| 119 | std::condition_variable Cond; |
Zachary Turner | 7a2d568 | 2017-05-11 00:22:18 +0000 | [diff] [blame] | 120 | parallel::detail::Latch Done; |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | Executor *Executor::getDefaultExecutor() { |
| 124 | static ThreadPoolExecutor exec; |
| 125 | return &exec; |
| 126 | } |
| 127 | #endif |
| 128 | } |
| 129 | |
Hans Wennborg | 905da74 | 2017-05-11 15:32:47 +0000 | [diff] [blame] | 130 | #if LLVM_ENABLE_THREADS |
Zachary Turner | 20c8e91 | 2017-05-11 00:18:52 +0000 | [diff] [blame] | 131 | void parallel::detail::TaskGroup::spawn(std::function<void()> F) { |
Zachary Turner | 0a340ce | 2017-05-10 17:39:18 +0000 | [diff] [blame] | 132 | L.inc(); |
| 133 | Executor::getDefaultExecutor()->add([&, F] { |
| 134 | F(); |
| 135 | L.dec(); |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 136 | }); |
| 137 | } |
Hans Wennborg | 905da74 | 2017-05-11 15:32:47 +0000 | [diff] [blame] | 138 | #endif |