Mercurial > hg > orthanc
annotate Core/MultiThreading/BagOfTasksProcessor.cpp @ 2322:dc7c8eebcc80
merge
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 14 Jul 2017 10:37:50 +0200 |
parents | a3a65de1840f |
children | 878b59270859 |
rev | line source |
---|---|
1920 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
2244
a3a65de1840f
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2110
diff
changeset
|
5 * Copyright (C) 2017 Osimis, Belgium |
1920 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "../PrecompiledHeaders.h" | |
35 #include "BagOfTasksProcessor.h" | |
36 | |
2110
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
37 #include "../Logging.h" |
1920 | 38 #include "../OrthancException.h" |
39 | |
2110
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
40 #include <stdio.h> |
1920 | 41 |
42 namespace Orthanc | |
43 { | |
44 class BagOfTasksProcessor::Task : public IDynamicObject | |
45 { | |
46 private: | |
47 uint64_t bag_; | |
48 std::auto_ptr<ICommand> command_; | |
49 | |
50 public: | |
51 Task(uint64_t bag, | |
52 ICommand* command) : | |
53 bag_(bag), | |
54 command_(command) | |
55 { | |
56 } | |
57 | |
58 bool Execute() | |
59 { | |
60 try | |
61 { | |
62 return command_->Execute(); | |
63 } | |
2110
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
64 catch (OrthancException& e) |
1920 | 65 { |
2110
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
66 LOG(ERROR) << "Exception while processing a bag of tasks: " << e.What(); |
1920 | 67 return false; |
68 } | |
2110
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
69 catch (std::runtime_error& e) |
1920 | 70 { |
2110
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
71 LOG(ERROR) << "Runtime exception while processing a bag of tasks: " << e.what(); |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
72 return false; |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
73 } |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
74 catch (...) |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
75 { |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
76 LOG(ERROR) << "Native exception while processing a bag of tasks"; |
1920 | 77 return false; |
78 } | |
79 } | |
80 | |
81 uint64_t GetBag() | |
82 { | |
83 return bag_; | |
84 } | |
85 }; | |
86 | |
87 | |
2110
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
88 void BagOfTasksProcessor::SignalProgress(Task& task, |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
89 Bag& bag) |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
90 { |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
91 assert(bag.done_ < bag.size_); |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
92 |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
93 bag.done_ += 1; |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
94 |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
95 if (bag.done_ == bag.size_) |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
96 { |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
97 exitStatus_[task.GetBag()] = (bag.status_ == BagStatus_Running); |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
98 bagFinished_.notify_all(); |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
99 } |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
100 } |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
101 |
1920 | 102 void BagOfTasksProcessor::Worker(BagOfTasksProcessor* that) |
103 { | |
104 while (that->continue_) | |
105 { | |
106 std::auto_ptr<IDynamicObject> obj(that->queue_.Dequeue(100)); | |
107 if (obj.get() != NULL) | |
108 { | |
109 Task& task = *dynamic_cast<Task*>(obj.get()); | |
110 | |
111 { | |
112 boost::mutex::scoped_lock lock(that->mutex_); | |
113 | |
114 Bags::iterator bag = that->bags_.find(task.GetBag()); | |
115 assert(bag != that->bags_.end()); | |
116 assert(bag->second.done_ < bag->second.size_); | |
117 | |
118 if (bag->second.status_ != BagStatus_Running) | |
119 { | |
2110
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
120 // Do not execute this task, as its parent bag of tasks |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
121 // has failed or is tagged as canceled |
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
122 that->SignalProgress(task, bag->second); |
1920 | 123 continue; |
124 } | |
125 } | |
126 | |
127 bool success = task.Execute(); | |
128 | |
129 { | |
130 boost::mutex::scoped_lock lock(that->mutex_); | |
131 | |
132 Bags::iterator bag = that->bags_.find(task.GetBag()); | |
133 assert(bag != that->bags_.end()); | |
134 | |
135 if (!success) | |
136 { | |
137 bag->second.status_ = BagStatus_Failed; | |
138 } | |
139 | |
2110
5b818f677dd6
fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2016
diff
changeset
|
140 that->SignalProgress(task, bag->second); |
1920 | 141 } |
142 } | |
143 } | |
144 } | |
145 | |
146 | |
147 void BagOfTasksProcessor::Cancel(int64_t bag) | |
148 { | |
149 boost::mutex::scoped_lock lock(mutex_); | |
150 | |
151 Bags::iterator it = bags_.find(bag); | |
152 if (it != bags_.end()) | |
153 { | |
154 it->second.status_ = BagStatus_Canceled; | |
155 } | |
156 } | |
157 | |
158 | |
159 bool BagOfTasksProcessor::Join(int64_t bag) | |
160 { | |
161 boost::mutex::scoped_lock lock(mutex_); | |
162 | |
163 while (continue_) | |
164 { | |
165 ExitStatus::iterator it = exitStatus_.find(bag); | |
166 if (it == exitStatus_.end()) // The bag is still running | |
167 { | |
168 bagFinished_.wait(lock); | |
169 } | |
170 else | |
171 { | |
172 bool status = it->second; | |
173 exitStatus_.erase(it); | |
174 return status; | |
175 } | |
176 } | |
177 | |
178 return false; // The processor is stopping | |
179 } | |
180 | |
181 | |
182 float BagOfTasksProcessor::GetProgress(int64_t bag) | |
183 { | |
184 boost::mutex::scoped_lock lock(mutex_); | |
185 | |
186 Bags::const_iterator it = bags_.find(bag); | |
187 if (it == bags_.end()) | |
188 { | |
189 // The bag of tasks has finished | |
190 return 1.0f; | |
191 } | |
192 else | |
193 { | |
194 return (static_cast<float>(it->second.done_) / | |
195 static_cast<float>(it->second.size_)); | |
196 } | |
197 } | |
198 | |
199 | |
200 bool BagOfTasksProcessor::Handle::Join() | |
201 { | |
202 if (hasJoined_) | |
203 { | |
204 return status_; | |
205 } | |
206 else | |
207 { | |
208 status_ = that_.Join(bag_); | |
209 hasJoined_ = true; | |
210 return status_; | |
211 } | |
212 } | |
213 | |
214 | |
215 BagOfTasksProcessor::BagOfTasksProcessor(size_t countThreads) : | |
216 countBags_(0), | |
217 continue_(true) | |
218 { | |
2016 | 219 if (countThreads == 0) |
1920 | 220 { |
221 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
222 } | |
223 | |
224 threads_.resize(countThreads); | |
225 | |
226 for (size_t i = 0; i < threads_.size(); i++) | |
227 { | |
228 threads_[i] = new boost::thread(Worker, this); | |
229 } | |
230 } | |
231 | |
232 | |
233 BagOfTasksProcessor::~BagOfTasksProcessor() | |
234 { | |
235 continue_ = false; | |
236 | |
237 bagFinished_.notify_all(); // Wakes up all the pending "Join()" | |
238 | |
239 for (size_t i = 0; i < threads_.size(); i++) | |
240 { | |
241 if (threads_[i]) | |
242 { | |
243 if (threads_[i]->joinable()) | |
244 { | |
245 threads_[i]->join(); | |
246 } | |
247 | |
248 delete threads_[i]; | |
249 threads_[i] = NULL; | |
250 } | |
251 } | |
252 } | |
253 | |
254 | |
255 BagOfTasksProcessor::Handle* BagOfTasksProcessor::Submit(BagOfTasks& tasks) | |
256 { | |
1923
6ac7f31fc543
fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1920
diff
changeset
|
257 if (tasks.GetSize() == 0) |
6ac7f31fc543
fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1920
diff
changeset
|
258 { |
6ac7f31fc543
fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1920
diff
changeset
|
259 return new Handle(*this, 0, true); |
6ac7f31fc543
fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1920
diff
changeset
|
260 } |
6ac7f31fc543
fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1920
diff
changeset
|
261 |
1920 | 262 boost::mutex::scoped_lock lock(mutex_); |
263 | |
264 uint64_t id = countBags_; | |
265 countBags_ += 1; | |
266 | |
267 Bag bag(tasks.GetSize()); | |
268 bags_[id] = bag; | |
269 | |
270 while (!tasks.IsEmpty()) | |
271 { | |
272 queue_.Enqueue(new Task(id, tasks.Pop())); | |
273 } | |
274 | |
1923
6ac7f31fc543
fix freeze if empty bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1920
diff
changeset
|
275 return new Handle(*this, id, false); |
1920 | 276 } |
277 } |