chrono_batch_render/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
45 / 45
100.0%
Branches:
21 / 23
91.3%
Functions:
5 / 5
100.0%

util/thread_pool.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2 #include "chrono_batch_render/util/thread_pool.h"
3
4 #include <algorithm>
5 #include <utility>
6
7 namespace chrono::batch_render {
8
9 373 ThreadPool::~ThreadPool() {
10 373 std::deque<Job> dropped;
11 373 {
12 373 const std::lock_guard lock(m_mutex);
13 373 m_stopping = true;
14 373 dropped.swap(m_jobs);
15 373 }
16 373 m_ready.notify_all();
17
2/2
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 373 times.
488 for (std::thread& thread : m_threads) {
18 115 thread.join();
19 }
20 373 }
21
22 1183 void ThreadPool::Add(const void* key, std::function<void()> job) {
23 1183 {
24 1183 const std::lock_guard lock(m_mutex);
25
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1137 times.
1183 if (m_threads.empty()) {
26
2/2
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 46 times.
183 for (unsigned i = 0; i < m_size; ++i) {
27
1/1
✓ Branch 2 taken 137 times.
274 m_threads.emplace_back([this] { Run(); });
28 }
29 }
30 1183 m_jobs.push_back({key, std::move(job)});
31 1183 }
32 1183 m_ready.notify_one();
33 2366 }
34
35 1343 std::function<void()> ThreadPool::Take(const void* key) {
36 1343 const std::lock_guard lock(m_mutex);
37 1343 const auto found = std::ranges::find(m_jobs, key, &Job::key);
38
2/2
✓ Branch 0 taken 1209 times.
✓ Branch 1 taken 134 times.
1343 if (found == m_jobs.end()) {
39 1209 return {};
40 }
41
42 134 std::function<void()> taken = std::move(found->run);
43 134 m_jobs.erase(found);
44 134 return taken;
45 134 }
46
47 1124 bool ThreadPool::Cancel(const void* key) {
48 1124 return bool(Take(key));
49 }
50
51 137 void ThreadPool::Run() {
52 2235 for (;;) {
53
1/1
✓ Branch 1 taken 1186 times.
1186 Job job;
54 1186 {
55
1/1
✓ Branch 1 taken 1186 times.
1186 std::unique_lock lock(m_mutex);
56
5/5
✓ Branch 0 taken 1368 times.
✓ Branch 1 taken 115 times.
✓ Branch 2 taken 319 times.
✓ Branch 3 taken 1049 times.
✓ Branch 5 taken 1164 times.
2669 m_ready.wait(lock, [this] { return m_stopping || !m_jobs.empty(); });
57
2/2
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 1049 times.
1164 if (m_stopping) {
58
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 return;
59 }
60 1049 job = std::move(m_jobs.front());
61
1/2
✓ Branch 1 taken 1049 times.
✗ Branch 2 not taken.
1049 m_jobs.pop_front();
62 115 }
63
1/1
✓ Branch 1 taken 1049 times.
1049 job.run();
64 1164 }
65 }
66
67 }
68