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 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Zachary Turner | 3a57fbd | 2017-05-11 00:03:52 +0000 | [diff] [blame] | 9 | #include "llvm/Support/Parallel.h" |
Zachary Turner | 6083b2b | 2017-05-05 21:14:55 +0000 | [diff] [blame] | 10 | #include "llvm/Config/llvm-config.h" |
Nico Weber | 0f2a48c | 2018-05-11 15:25:38 +0000 | [diff] [blame] | 11 | |
| 12 | #if LLVM_ENABLE_THREADS |
| 13 | |
Rafael Espindola | 8c0ff95 | 2017-10-04 20:27:01 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Threading.h" |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 15 | |
| 16 | #include <atomic> |
| 17 | #include <stack> |
Rui Ueyama | 552c290 | 2017-05-05 21:27:30 +0000 | [diff] [blame] | 18 | #include <thread> |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 19 | |
Fangrui Song | f6a6290 | 2019-04-25 11:33:30 +0000 | [diff] [blame] | 20 | namespace llvm { |
| 21 | namespace parallel { |
| 22 | namespace detail { |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 26 | /// An abstract class that takes closures and runs them asynchronously. |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 27 | class Executor { |
| 28 | public: |
| 29 | virtual ~Executor() = default; |
| 30 | virtual void add(std::function<void()> func) = 0; |
| 31 | |
| 32 | static Executor *getDefaultExecutor(); |
| 33 | }; |
| 34 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 35 | /// An implementation of an Executor that runs closures on a thread pool |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 36 | /// in filo order. |
| 37 | class ThreadPoolExecutor : public Executor { |
| 38 | public: |
Rafael Espindola | 8c0ff95 | 2017-10-04 20:27:01 +0000 | [diff] [blame] | 39 | explicit ThreadPoolExecutor(unsigned ThreadCount = hardware_concurrency()) |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 40 | : Done(ThreadCount) { |
| 41 | // Spawn all but one of the threads in another thread as spawning threads |
| 42 | // can take a while. |
| 43 | std::thread([&, ThreadCount] { |
| 44 | for (size_t i = 1; i < ThreadCount; ++i) { |
| 45 | std::thread([=] { work(); }).detach(); |
| 46 | } |
| 47 | work(); |
| 48 | }).detach(); |
| 49 | } |
| 50 | |
| 51 | ~ThreadPoolExecutor() override { |
| 52 | std::unique_lock<std::mutex> Lock(Mutex); |
| 53 | Stop = true; |
| 54 | Lock.unlock(); |
| 55 | Cond.notify_all(); |
| 56 | // Wait for ~Latch. |
| 57 | } |
| 58 | |
| 59 | void add(std::function<void()> F) override { |
| 60 | std::unique_lock<std::mutex> Lock(Mutex); |
| 61 | WorkStack.push(F); |
| 62 | Lock.unlock(); |
| 63 | Cond.notify_one(); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | void work() { |
| 68 | while (true) { |
| 69 | std::unique_lock<std::mutex> Lock(Mutex); |
| 70 | Cond.wait(Lock, [&] { return Stop || !WorkStack.empty(); }); |
| 71 | if (Stop) |
| 72 | break; |
| 73 | auto Task = WorkStack.top(); |
| 74 | WorkStack.pop(); |
| 75 | Lock.unlock(); |
| 76 | Task(); |
| 77 | } |
| 78 | Done.dec(); |
| 79 | } |
| 80 | |
| 81 | std::atomic<bool> Stop{false}; |
| 82 | std::stack<std::function<void()>> WorkStack; |
| 83 | std::mutex Mutex; |
| 84 | std::condition_variable Cond; |
Zachary Turner | 7a2d568 | 2017-05-11 00:22:18 +0000 | [diff] [blame] | 85 | parallel::detail::Latch Done; |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | Executor *Executor::getDefaultExecutor() { |
| 89 | static ThreadPoolExecutor exec; |
| 90 | return &exec; |
| 91 | } |
Nico Weber | d496003 | 2019-10-10 18:57:23 +0000 | [diff] [blame^] | 92 | } // namespace |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 93 | |
Fangrui Song | f6a6290 | 2019-04-25 11:33:30 +0000 | [diff] [blame] | 94 | static std::atomic<int> TaskGroupInstances; |
| 95 | |
| 96 | // Latch::sync() called by the dtor may cause one thread to block. If is a dead |
| 97 | // lock if all threads in the default executor are blocked. To prevent the dead |
| 98 | // lock, only allow the first TaskGroup to run tasks parallelly. In the scenario |
| 99 | // of nested parallel_for_each(), only the outermost one runs parallelly. |
| 100 | TaskGroup::TaskGroup() : Parallel(TaskGroupInstances++ == 0) {} |
| 101 | TaskGroup::~TaskGroup() { --TaskGroupInstances; } |
| 102 | |
| 103 | void TaskGroup::spawn(std::function<void()> F) { |
| 104 | if (Parallel) { |
| 105 | L.inc(); |
| 106 | Executor::getDefaultExecutor()->add([&, F] { |
| 107 | F(); |
| 108 | L.dec(); |
| 109 | }); |
| 110 | } else { |
Zachary Turner | 0a340ce | 2017-05-10 17:39:18 +0000 | [diff] [blame] | 111 | F(); |
Fangrui Song | f6a6290 | 2019-04-25 11:33:30 +0000 | [diff] [blame] | 112 | } |
Zachary Turner | f7ca8fc | 2017-05-05 21:09:26 +0000 | [diff] [blame] | 113 | } |
Fangrui Song | f6a6290 | 2019-04-25 11:33:30 +0000 | [diff] [blame] | 114 | |
| 115 | } // namespace detail |
| 116 | } // namespace parallel |
| 117 | } // namespace llvm |
Nico Weber | 0f2a48c | 2018-05-11 15:25:38 +0000 | [diff] [blame] | 118 | #endif // LLVM_ENABLE_THREADS |