comparison Resources/Graveyard/Multithreading/BagOfTasksProcessor.cpp @ 2610:3ff4c50647ea jobs

moving the old scheduler to the graveyard
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 19 May 2018 16:40:26 +0200
parents Resources/Graveyard/BagOfTasksProcessor.cpp@3caca43371f5
children 4e43e67f8ecf
comparison
equal deleted inserted replaced
2609:f7a84b551ee4 2610:3ff4c50647ea
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium
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
37 #include "../Logging.h"
38 #include "../OrthancException.h"
39
40 #include <stdio.h>
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 }
64 catch (OrthancException& e)
65 {
66 LOG(ERROR) << "Exception while processing a bag of tasks: " << e.What();
67 return false;
68 }
69 catch (std::runtime_error& e)
70 {
71 LOG(ERROR) << "Runtime exception while processing a bag of tasks: " << e.what();
72 return false;
73 }
74 catch (...)
75 {
76 LOG(ERROR) << "Native exception while processing a bag of tasks";
77 return false;
78 }
79 }
80
81 uint64_t GetBag()
82 {
83 return bag_;
84 }
85 };
86
87
88 void BagOfTasksProcessor::SignalProgress(Task& task,
89 Bag& bag)
90 {
91 assert(bag.done_ < bag.size_);
92
93 bag.done_ += 1;
94
95 if (bag.done_ == bag.size_)
96 {
97 exitStatus_[task.GetBag()] = (bag.status_ == BagStatus_Running);
98 bagFinished_.notify_all();
99 }
100 }
101
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 {
120 // Do not execute this task, as its parent bag of tasks
121 // has failed or is tagged as canceled
122 that->SignalProgress(task, bag->second);
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
140 that->SignalProgress(task, bag->second);
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 {
219 if (countThreads == 0)
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 {
257 if (tasks.GetSize() == 0)
258 {
259 return new Handle(*this, 0, true);
260 }
261
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
275 return new Handle(*this, id, false);
276 }
277 }