-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathprogress_list.cpp
More file actions
134 lines (106 loc) · 3.24 KB
/
progress_list.cpp
File metadata and controls
134 lines (106 loc) · 3.24 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Copyright (C) 2017-2026 Topological Manifold
This program 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 3 of the License, or
(at your option) any later version.
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "progress_list.h"
#include "progress_interfaces.h"
#include <src/com/error.h>
#include <src/com/exception.h>
#include <list>
#include <mutex>
#include <thread>
#include <vector>
namespace ns::progress
{
// for worker threads
void RatioList::add_ratio(RatioControl* const ratio)
{
ASSERT(std::this_thread::get_id() != thread_id_);
const std::lock_guard lg(mutex_);
if (terminate_quietly_)
{
throw TerminateQuietlyException();
}
if (terminate_with_message_)
{
throw TerminateWithMessageException();
}
ratios_.emplace_back(ratio);
}
// for worker threads
void RatioList::delete_ratio(const RatioControl* const ratio) noexcept
{
try
{
ASSERT(std::this_thread::get_id() != thread_id_);
const std::lock_guard lg(mutex_);
auto iter = ratios_.cbegin();
while (iter != ratios_.cend())
{
if (*iter == ratio)
{
iter = ratios_.erase(iter);
continue;
}
++iter;
}
}
catch (...)
{
error_fatal("Exception in delete progress ratio");
}
}
// for UI thread
void RatioList::terminate_all_quietly()
{
ASSERT(std::this_thread::get_id() == thread_id_);
const std::lock_guard lg(mutex_);
terminate_quietly_ = true;
for (RatioControl* const ratio : ratios_)
{
ratio->terminate_quietly();
}
}
// for UI thread
void RatioList::terminate_all_with_message()
{
ASSERT(std::this_thread::get_id() == thread_id_);
const std::lock_guard lg(mutex_);
terminate_with_message_ = true;
for (RatioControl* const ratio : ratios_)
{
ratio->terminate_with_message();
}
}
// for UI thread
void RatioList::enable()
{
ASSERT(std::this_thread::get_id() == thread_id_);
const std::lock_guard lg(mutex_);
ASSERT(ratios_.empty());
terminate_quietly_ = false;
terminate_with_message_ = false;
}
// for UI thread
std::vector<RatioInfo> RatioList::ratios() const
{
ASSERT(std::this_thread::get_id() == thread_id_);
const std::lock_guard lg(mutex_);
std::vector<RatioInfo> res;
res.reserve(ratios_.size());
for (const RatioControl* const ratio : ratios_)
{
res.push_back(ratio->info());
}
return res;
}
}