./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
72 / 72
100.0%
Branches:
64 / 73
87.7%
Functions:
9 / 9
100.0%

src/util/parallel.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "terraingen/util/parallel.h"
4
5 #include <algorithm>
6 #include <atomic>
7 #include <condition_variable>
8 #include <deque>
9 #include <exception>
10 #include <memory>
11 #include <mutex>
12 #include <thread>
13 #include <vector>
14
15 namespace terraingen {
16
17 namespace {
18
19 /// One `ParallelFor` call: its tasks, handed out in order to the caller and its helpers.
20 struct Job {
21 std::size_t count = 0;
22 const std::function<void(std::size_t)>* task = nullptr;
23 std::atomic<std::size_t> next{0};
24 std::atomic<int> slots{0};
25 std::atomic<bool> failed{false};
26 std::mutex mutex;
27 std::condition_variable idle;
28 int active = 0;
29 std::exception_ptr error;
30
31 /// Runs tasks until none are left or one has failed.
32 4320 void Work() {
33
2/2
✓ Branch 0 taken 97719 times.
✓ Branch 1 taken 4 times.
102043 while (!failed.load()) {
34
2/2
✓ Branch 0 taken 93402 times.
✓ Branch 1 taken 4317 times.
97719 const std::size_t i = next.fetch_add(1);
35
2/2
✓ Branch 0 taken 93402 times.
✓ Branch 1 taken 4317 times.
97719 if (i >= count) {
36 return;
37 }
38 93402 try {
39
1/1
✓ Branch 1 taken 93400 times.
93402 (*task)(i);
40 2 } catch (...) {
41
1/1
✓ Branch 1 taken 3 times.
3 const std::lock_guard lock(mutex);
42
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 if (!error) {
43
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 error = std::current_exception();
44 }
45 3 failed.store(true);
46 3 }
47 }
48 }
49
50 /// Whether helpers have nothing left to take.
51 4257 bool Exhausted() const {
52
4/4
✓ Branch 0 taken 4255 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1917 times.
✓ Branch 3 taken 2338 times.
4257 return failed.load() || next.load() >= count;
53 }
54 };
55
56 /// Threads kept for the life of the program, helping whichever jobs are waiting. The pool is
57 /// never destroyed: joining threads while statics are torn down can deadlock, as when a library is
58 /// unloaded on Windows, and the threads end with the process.
59 class Pool {
60 public:
61
1/1
✓ Branch 2 taken 240 times.
240 Pool() {
62
1/2
✓ Branch 1 taken 240 times.
✗ Branch 2 not taken.
240 const unsigned size = std::max(1u, std::thread::hardware_concurrency()) - 1;
63
2/2
✓ Branch 0 taken 720 times.
✓ Branch 1 taken 240 times.
960 for (unsigned t = 0; t < size; ++t) {
64
2/2
✓ Branch 1 taken 720 times.
✓ Branch 4 taken 720 times.
1440 std::thread([this] { Loop(); }).detach();
65 }
66 240 }
67
68 Pool(const Pool&) = delete;
69 Pool& operator=(const Pool&) = delete;
70
71 /// Offers `job` to the pool's threads.
72 2081 void Post(const std::shared_ptr<Job>& job) {
73 2081 {
74 2081 const std::lock_guard lock(m_mutex);
75
1/1
✓ Branch 1 taken 2081 times.
2081 m_jobs.push_back(job);
76 2081 }
77 2081 m_wake.notify_all();
78 2081 }
79
80 private:
81 626 void Loop() {
82 626 std::unique_lock lock(m_mutex);
83 4873 while (true) {
84
2/2
✓ Branch 0 taken 4775 times.
✓ Branch 1 taken 4257 times.
9032 m_wake.wait(lock, [this] { return !m_jobs.empty(); });
85
1/2
✓ Branch 0 taken 4257 times.
✗ Branch 1 not taken.
4257 std::shared_ptr<Job> job = m_jobs.front();
86
4/4
✓ Branch 1 taken 2338 times.
✓ Branch 2 taken 1919 times.
✓ Branch 3 taken 96 times.
✓ Branch 4 taken 2242 times.
4257 if (job->Exhausted() || job->slots.fetch_sub(1) <= 0) {
87 2015 m_jobs.pop_front();
88
1/2
✓ Branch 0 taken 2015 times.
✗ Branch 1 not taken.
2015 continue;
89 }
90
1/1
✓ Branch 1 taken 2241 times.
2242 lock.unlock();
91 2241 {
92
1/1
✓ Branch 1 taken 2242 times.
2241 const std::lock_guard jobLock(job->mutex);
93 2242 ++job->active;
94 2242 }
95
1/1
✓ Branch 1 taken 2242 times.
2242 job->Work();
96 2242 {
97
1/1
✓ Branch 1 taken 2242 times.
2242 const std::lock_guard jobLock(job->mutex);
98 2242 --job->active;
99 2242 }
100 2242 job->idle.notify_all();
101
1/1
✓ Branch 1 taken 2233 times.
2233 lock.lock();
102 4247 }
103 }
104
105 std::mutex m_mutex;
106 std::condition_variable m_wake;
107 std::deque<std::shared_ptr<Job>> m_jobs;
108 };
109
110 2081 Pool& SharedPool() {
111
5/6
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 1841 times.
✓ Branch 3 taken 240 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 240 times.
✓ Branch 9 taken 240 times.
2081 static Pool* const pool = new Pool;
112 2081 return *pool;
113 }
114
115 /// The number of threads a request resolves to: `threads` when positive, otherwise the hardware
116 /// thread count (at least 1).
117 245413 unsigned ResolveThreads(unsigned threads) {
118
2/2
✓ Branch 0 taken 2530 times.
✓ Branch 1 taken 242883 times.
245413 if (threads > 0) {
119 return threads;
120 }
121
1/2
✓ Branch 1 taken 2530 times.
✗ Branch 2 not taken.
5060 return std::max(1u, std::thread::hardware_concurrency());
122 }
123
124 } // namespace
125
126 245089 void ParallelFor(std::size_t count, unsigned threads,
127 const std::function<void(std::size_t index)>& task) {
128
2/2
✓ Branch 1 taken 1045 times.
✓ Branch 2 taken 244421 times.
245089 const std::size_t workers = std::min<std::size_t>(ResolveThreads(threads), count);
129
2/2
✓ Branch 0 taken 243385 times.
✓ Branch 1 taken 2081 times.
245466 if (workers <= 1) {
130
2/2
✓ Branch 0 taken 298096 times.
✓ Branch 1 taken 243451 times.
541547 for (std::size_t i = 0; i < count; ++i) {
131 298096 task(i);
132 }
133 243451 return;
134 }
135 2081 auto job = std::make_shared<Job>();
136
1/1
✓ Branch 1 taken 2081 times.
2081 job->count = count;
137 2081 job->task = &task;
138
1/1
✓ Branch 1 taken 2081 times.
2081 job->slots.store(static_cast<int>(workers - 1));
139
2/2
✓ Branch 1 taken 2081 times.
✓ Branch 4 taken 2081 times.
2081 SharedPool().Post(job);
140
1/1
✓ Branch 1 taken 2081 times.
2081 job->Work();
141 2081 {
142
1/1
✓ Branch 1 taken 2081 times.
2081 std::unique_lock lock(job->mutex);
143
3/4
✓ Branch 0 taken 599 times.
✓ Branch 1 taken 2081 times.
✓ Branch 2 taken 2081 times.
✗ Branch 3 not taken.
2680 job->idle.wait(lock, [&] { return job->active == 0; });
144 }
145
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2079 times.
2081 job->slots.store(0);
146
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2079 times.
2081 if (job->error) {
147
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
6 std::rethrow_exception(job->error);
148 }
149 2079 }
150
151 } // namespace terraingen
152