Repository navigation
Expand file tree
/
Copy paththreads_manager.cpp
More file actions
102 lines (85 loc) · 3.37 KB
/
Copy paththreads_manager.cpp
File metadata and controls
102 lines (85 loc) · 3.37 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
/*
Copyright Contributors to the libdnf project.
This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
Libdnf 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.
Libdnf 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 libdnf. If not, see <https://www.gnu.org/licenses/>.
*/
#include "threads_manager.hpp"
#include <libdnf5/common/exception.hpp>
#include <libdnf5/utils/bgettext/bgettext-mark-domain.h>
#include <locale.h>
#include <algorithm>
#include <chrono>
#include <iostream>
ThreadsManager::ThreadsManager() {
// collecting finished worker threads
running_threads_collector = std::thread([this]() {
while (!finish_collector) {
join_threads(true);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
}
ThreadsManager::~ThreadsManager() {
finish();
}
void ThreadsManager::register_thread(std::thread && thread) {
std::lock_guard<std::mutex> lock(running_threads_mutex);
running_threads.emplace_back(std::move(thread));
}
void ThreadsManager::mark_thread_finished(std::thread::id thread_id) {
std::lock_guard<std::mutex> lock(running_threads_mutex);
finished_threads.emplace_back(std::move(thread_id));
}
void ThreadsManager::join_threads(const bool only_finished) {
std::vector<std::thread> to_be_joined{};
{
std::lock_guard<std::mutex> lock(running_threads_mutex);
for (auto thread = running_threads.begin(); thread < running_threads.end();) {
auto in_finished = std::find(finished_threads.begin(), finished_threads.end(), thread->get_id());
if (thread->joinable() && (!only_finished || (in_finished != finished_threads.end()))) {
to_be_joined.push_back(std::move(*thread));
running_threads.erase(thread);
if (in_finished != finished_threads.end()) {
finished_threads.erase(in_finished);
}
} else {
++thread;
}
}
}
for (auto thread = to_be_joined.begin(); thread < to_be_joined.end(); ++thread) {
// join the thread and remove it from registry
thread->join();
}
}
void ThreadsManager::finish() {
if (!running_threads_collector.joinable()) {
return;
}
// join all threads
finish_collector = true;
join_threads(false);
running_threads_collector.join();
}
locale_t ThreadsManager::set_thread_locale(const std::string & thread_locale, locale_t & new_locale) {
auto no_locale = static_cast<locale_t>(0);
new_locale = newlocale(LC_ALL_MASK, thread_locale.c_str(), no_locale);
if (new_locale == no_locale) {
throw libdnf5::SystemError(errno, M_("Failed to create locale \"{}\"."), thread_locale);
}
locale_t orig_locale = uselocale(new_locale);
if (orig_locale == no_locale) {
freelocale(new_locale);
throw libdnf5::SystemError(errno, M_("Failed to use locale \"{}\"."), thread_locale);
}
return orig_locale;
}