-
Notifications
You must be signed in to change notification settings - Fork 256
/
thread_list.cc
152 lines (119 loc) · 4.42 KB
/
thread_list.cc
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2019-2019 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation, which is
listed in the file LICENSE.
This program 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
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "include/bareos.h"
#include "thread_list.h"
#include "include/jcr.h"
#include "include/make_unique.h"
#include "lib/berrno.h"
#include "lib/bsock.h"
#include <algorithm>
#include <condition_variable>
#include <thread>
#include <mutex>
#include <set>
struct ThreadListItem {
std::thread WorkerThread_;
void* data_ = nullptr;
ConfigurationParser* config_ = nullptr;
std::function<void*(ConfigurationParser* config_, void* data)>
ThreadInvokedHandler_ = nullptr;
};
struct ThreadListPrivate {
std::size_t maximum_thread_count_ = 0;
std::set<ThreadListItem*> thread_list_;
std::mutex thread_list_mutex_;
std::condition_variable wait_shutdown_condition;
std::function<void*(ConfigurationParser* config, void* data)>
ThreadInvokedHandler_ = nullptr;
std::function<void*(void* data)> ShutdownCallback_ = nullptr;
void CallRegisteredShutdownCallbackForAllThreads();
bool WaitForThreadsToShutdown();
};
ThreadList::ThreadList() : impl_(std::make_unique<ThreadListPrivate>()) {}
ThreadList::~ThreadList() = default;
void ThreadList::Init(int maximum_thread_count,
std::function<void*(ConfigurationParser* config,
void* data)> ThreadInvokedHandler,
std::function<void*(void* data)> ShutdownCallback)
{
if (impl_->thread_list_.size()) { return; }
impl_->maximum_thread_count_ = maximum_thread_count;
impl_->ThreadInvokedHandler_ = ThreadInvokedHandler;
impl_->ShutdownCallback_ = ShutdownCallback;
}
void ThreadListPrivate::CallRegisteredShutdownCallbackForAllThreads()
{
std::lock_guard<std::mutex> l(thread_list_mutex_);
for (auto item : thread_list_) {
if (ShutdownCallback_) ShutdownCallback_(item->data_);
}
}
bool ThreadListPrivate::WaitForThreadsToShutdown()
{
bool list_is_empty = false;
std::unique_lock<std::mutex> l(thread_list_mutex_);
int timeout = 0;
do {
list_is_empty = wait_shutdown_condition.wait_for(
l, std::chrono::seconds(10), [&]() { return thread_list_.empty(); });
} while (!list_is_empty && ++timeout < 3);
return list_is_empty;
}
bool ThreadList::WaitUntilThreadListIsEmpty()
{
impl_->CallRegisteredShutdownCallbackForAllThreads();
bool shutdown_successful = impl_->WaitForThreadsToShutdown();
return shutdown_successful;
}
struct CleanupOwnThreadAndNotify {
std::unique_ptr<ThreadListItem> item_;
ThreadList* t_;
CleanupOwnThreadAndNotify(std::unique_ptr<ThreadListItem> item, ThreadList* t)
: item_(std::move(item)), t_(t)
{
}
~CleanupOwnThreadAndNotify()
{
std::lock_guard<std::mutex> l(t_->impl_->thread_list_mutex_);
t_->impl_->thread_list_.erase(item_.get());
t_->impl_->wait_shutdown_condition.notify_one();
}
};
static void WorkerThread(std::unique_ptr<ThreadListItem> item, ThreadList* t)
{
SetJcrInTsd(INVALID_JCR);
item->ThreadInvokedHandler_(item->config_, item->data_);
CleanupOwnThreadAndNotify cleanup(std::move(item), t);
}
bool ThreadList::CreateAndAddNewThread(ConfigurationParser* config, void* data)
{
if (!impl_) { return false; }
std::lock_guard<std::mutex> l(impl_->thread_list_mutex_);
if (impl_->thread_list_.size() < impl_->maximum_thread_count_) {
ThreadListItem* item = new ThreadListItem;
item->data_ = data;
item->config_ = config;
item->ThreadInvokedHandler_ = impl_->ThreadInvokedHandler_;
item->WorkerThread_ =
std::thread(WorkerThread, std::unique_ptr<ThreadListItem>(item), this);
item->WorkerThread_.detach();
impl_->thread_list_.insert(item);
return true;
}
return false;
}
std::size_t ThreadList::GetSize() const { return impl_->thread_list_.size(); }