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