-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy paththread_pool.cpp
More file actions
112 lines (90 loc) · 2.94 KB
/
Copy paththread_pool.cpp
File metadata and controls
112 lines (90 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* @file thread_pool.cpp
*
* @copyright GNU General Public License Version 2.
* This file is part of YimMenu.
* YimMenu is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.
* YimMenu is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with YimMenu. If not, see <https://www.gnu.org/licenses/>.
*/
#include "thread_pool.hpp"
namespace big
{
thread_pool::thread_pool() :
m_accept_jobs(true)
{
this->m_managing_thread = std::thread(&thread_pool::create, this);
g_thread_pool = this;
}
thread_pool::~thread_pool()
{
g_thread_pool = nullptr;
}
void thread_pool::create()
{
const std::uint32_t thread_count = std::thread::hardware_concurrency();
LOG(VERBOSE) << "Allocating " << thread_count << " threads in thread pool.";
this->m_thread_pool.reserve(thread_count);
m_available_thread_count = thread_count;
for (std::uint32_t i = 0; i < thread_count; i++)
this->m_thread_pool.emplace_back(std::thread(&thread_pool::run, this));
}
void thread_pool::destroy()
{
this->m_managing_thread.join();
{
std::unique_lock lock(m_lock);
this->m_accept_jobs = false;
}
this->m_data_condition.notify_all();
for (auto& thread : m_thread_pool)
thread.join();
m_thread_pool.clear();
}
void thread_pool::push(std::function<void()> func, std::source_location location)
{
if (func)
{
{
std::unique_lock lock(this->m_lock);
this->m_job_stack.push({func, location});
if (m_available_thread_count < m_job_stack.size())
{
LOG(WARNING) << "thread_pool potentially starved";
}
}
this->m_data_condition.notify_all();
}
}
void thread_pool::run()
{
for (;;)
{
std::unique_lock lock(this->m_lock);
this->m_data_condition.wait(lock, [this]() {
return !this->m_job_stack.empty() || !this->m_accept_jobs;
});
if (!this->m_accept_jobs)
break;
if (this->m_job_stack.empty())
continue;
thread_pool_job job = this->m_job_stack.top();
this->m_job_stack.pop();
lock.unlock();
m_available_thread_count--;
try
{
const auto source_file = std::filesystem::path(job.m_source_location.file_name()).filename().string();
LOG(VERBOSE) << "Thread " << std::this_thread::get_id() << " executing " << source_file << ":"
<< job.m_source_location.line();
std::invoke(job.m_func);
}
catch (const std::exception& e)
{
LOG(WARNING) << "Exception thrown while executing job in thread:" << std::endl << e.what();
}
m_available_thread_count++;
}
LOG(VERBOSE) << "Thread " << std::this_thread::get_id() << " exiting...";
}
}