annotate OrthancFramework/Resources/Graveyard/Multithreading/BagOfTasksProcessor.cpp @ 5336:dd9795dc380d

renamed MetricsType as MetricsUpdate to clarify
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 27 Jun 2023 15:56:04 +0200
parents 0ea402b4d901
children 48b8dae6dc77
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
5185
0ea402b4d901 upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
0ea402b4d901 upgrade to year 2023
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
6 * Copyright (C) 2021-2023 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 *
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 **/
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "../PrecompiledHeaders.h"
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include "BagOfTasksProcessor.h"
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
2110
5b818f677dd6 fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2016
diff changeset
27 #include "../Logging.h"
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include "../OrthancException.h"
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
2110
5b818f677dd6 fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2016
diff changeset
30 #include <stdio.h>
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 namespace Orthanc
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 class BagOfTasksProcessor::Task : public IDynamicObject
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 private:
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 uint64_t bag_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 std::auto_ptr<ICommand> command_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 public:
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 Task(uint64_t bag,
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 ICommand* command) :
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 bag_(bag),
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 command_(command)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 bool Execute()
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 try
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 return command_->Execute();
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 }
2110
5b818f677dd6 fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2016
diff changeset
54 catch (OrthancException& e)
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 return false;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 }
2110
5b818f677dd6 fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2016
diff changeset
59 catch (std::runtime_error& e)
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 return false;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 uint64_t GetBag()
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 return bag_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 };
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 void BagOfTasksProcessor::Worker(BagOfTasksProcessor* that)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 while (that->continue_)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 std::auto_ptr<IDynamicObject> obj(that->queue_.Dequeue(100));
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 if (obj.get() != NULL)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 Task& task = *dynamic_cast<Task*>(obj.get());
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 boost::mutex::scoped_lock lock(that->mutex_);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 Bags::iterator bag = that->bags_.find(task.GetBag());
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 assert(bag != that->bags_.end());
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 assert(bag->second.done_ < bag->second.size_);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 if (bag->second.status_ != BagStatus_Running)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 continue;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 bool success = task.Execute();
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 boost::mutex::scoped_lock lock(that->mutex_);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 Bags::iterator bag = that->bags_.find(task.GetBag());
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 assert(bag != that->bags_.end());
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 if (!success)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 bag->second.status_ = BagStatus_Failed;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129
2110
5b818f677dd6 fix in BagOfTasksProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2016
diff changeset
130 that->SignalProgress(task, bag->second);
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 void BagOfTasksProcessor::Cancel(int64_t bag)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 boost::mutex::scoped_lock lock(mutex_);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 Bags::iterator it = bags_.find(bag);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 if (it != bags_.end())
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 it->second.status_ = BagStatus_Canceled;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 bool BagOfTasksProcessor::Join(int64_t bag)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 boost::mutex::scoped_lock lock(mutex_);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 while (continue_)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 ExitStatus::iterator it = exitStatus_.find(bag);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 if (it == exitStatus_.end()) // The bag is still running
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 bagFinished_.wait(lock);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 else
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 bool status = it->second;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 exitStatus_.erase(it);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164 return status;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 return false; // The processor is stopping
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 float BagOfTasksProcessor::GetProgress(int64_t bag)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 boost::mutex::scoped_lock lock(mutex_);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 Bags::const_iterator it = bags_.find(bag);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 if (it == bags_.end())
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 // The bag of tasks has finished
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 return 1.0f;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 else
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 return (static_cast<float>(it->second.done_) /
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 static_cast<float>(it->second.size_));
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 bool BagOfTasksProcessor::Handle::Join()
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 if (hasJoined_)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 return status_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 else
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 status_ = that_.Join(bag_);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 hasJoined_ = true;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 return status_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 BagOfTasksProcessor::BagOfTasksProcessor(size_t countThreads) :
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 countBags_(0),
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 continue_(true)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 {
2016
0ae26237569a cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1923
diff changeset
209 if (countThreads == 0)
1920
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 throw OrthancException(ErrorCode_ParameterOutOfRange);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 threads_.resize(countThreads);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 for (size_t i = 0; i < threads_.size(); i++)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 threads_[i] = new boost::thread(Worker, this);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 BagOfTasksProcessor::~BagOfTasksProcessor()
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225 continue_ = false;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 bagFinished_.notify_all(); // Wakes up all the pending "Join()"
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 for (size_t i = 0; i < threads_.size(); i++)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 if (threads_[i])
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 if (threads_[i]->joinable())
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 threads_[i]->join();
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 delete threads_[i];
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 threads_[i] = NULL;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 BagOfTasksProcessor::Handle* BagOfTasksProcessor::Submit(BagOfTasks& tasks)
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 boost::mutex::scoped_lock lock(mutex_);
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 uint64_t id = countBags_;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 countBags_ += 1;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 Bag bag(tasks.GetSize());
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 bags_[id] = bag;
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 while (!tasks.IsEmpty())
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 {
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262 queue_.Enqueue(new Task(id, tasks.Pop()));
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 }
b97aa579e85b bag of tasks
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 }