Mercurial > hg > orthanc
annotate Core/MultiThreading/BagOfRunnablesBySteps.cpp @ 1678:1a3c20cd1b53 db-changes
renames in BagOfRunnablesBySteps to make things clearer
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 06 Oct 2015 11:12:13 +0200 |
parents | f967bdf8534e |
children |
rev | line source |
---|---|
0 | 1 /** |
59 | 2 * Orthanc - A Lightweight, RESTful DICOM Store |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics |
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
824
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
0 | 5 * |
6 * This program is free software: you can redistribute it and/or | |
7 * modify it under the terms of the GNU General Public License as | |
8 * published by the Free Software Foundation, either version 3 of the | |
9 * License, or (at your option) any later version. | |
136 | 10 * |
11 * In addition, as a special exception, the copyright holders of this | |
12 * program give permission to link the code of its release with the | |
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
14 * that use the same license as the "OpenSSL" library), and distribute | |
15 * the linked executables. You must obey the GNU General Public License | |
16 * in all respects for all of the code used other than "OpenSSL". If you | |
17 * modify file(s) with this exception, you may extend this exception to | |
18 * your version of the file(s), but you are not obligated to do so. If | |
19 * you do not wish to do so, delete this exception statement from your | |
20 * version. If you delete this exception statement from all source files | |
21 * in the program, then also delete it here. | |
0 | 22 * |
23 * This program is distributed in the hope that it will be useful, but | |
24 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 * General Public License for more details. | |
27 * | |
28 * You should have received a copy of the GNU General Public License | |
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
30 **/ | |
31 | |
32 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
33 #include "../PrecompiledHeaders.h" |
0 | 34 #include "BagOfRunnablesBySteps.h" |
35 | |
1486
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1453
diff
changeset
|
36 #include "../Logging.h" |
f967bdf8534e
refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1453
diff
changeset
|
37 |
0 | 38 #include <stack> |
39 #include <boost/thread.hpp> | |
40 | |
59 | 41 namespace Orthanc |
0 | 42 { |
43 struct BagOfRunnablesBySteps::PImpl | |
44 { | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
45 boost::mutex mutex_; |
0 | 46 |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
47 // 1. The set of active runnables, i.e. the runnables that have |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
48 // not finished their job yet, plus the runnables that have not |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
49 // been joined yet. |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
50 typedef std::map<IRunnableBySteps*, boost::thread*> ActiveRunnables; |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
51 ActiveRunnables activeRunnables_; |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
52 bool continueActiveRunnables_; |
0 | 53 |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
54 // 2. Condition variable that is notified when one active runnable |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
55 // stops. |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
56 boost::condition_variable oneRunnableHasStopped_; |
0 | 57 |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
58 // 3. The list of runnables that have stopped but are waiting to be |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
59 // joined by the collector. |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
60 typedef std::stack<IRunnableBySteps*> StoppedRunnables; |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
61 StoppedRunnables stoppedRunnables_; |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
62 |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
63 // 4. Condition variable that is notified when one stopped |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
64 // runnable has been joined. |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
65 boost::condition_variable oneRunnableIsJoined_; |
0 | 66 |
67 // The thread that joins the runnables after they stop | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
68 bool continueCollector_; |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
69 std::auto_ptr<boost::thread> collector_; |
0 | 70 }; |
71 | |
72 | |
73 | |
74 void BagOfRunnablesBySteps::RunnableThread(BagOfRunnablesBySteps* bag, | |
75 IRunnableBySteps* runnable) | |
76 { | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
77 while (bag->pimpl_->continueActiveRunnables_) |
0 | 78 { |
79 if (!runnable->Step()) | |
80 { | |
81 break; | |
82 } | |
83 } | |
84 | |
85 { | |
86 // Register this runnable as having stopped | |
87 boost::mutex::scoped_lock lock(bag->pimpl_->mutex_); | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
88 bag->pimpl_->stoppedRunnables_.push(runnable); |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
89 bag->pimpl_->oneRunnableHasStopped_.notify_one(); |
0 | 90 } |
91 } | |
92 | |
93 | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
94 void BagOfRunnablesBySteps::CollectorThread(BagOfRunnablesBySteps* bag) |
0 | 95 { |
96 boost::mutex::scoped_lock lock(bag->pimpl_->mutex_); | |
97 | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
98 while (bag->pimpl_->continueCollector_) |
0 | 99 { |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
100 while (!bag->pimpl_->stoppedRunnables_.empty()) |
0 | 101 { |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
102 std::auto_ptr<IRunnableBySteps> r(bag->pimpl_->stoppedRunnables_.top()); |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
103 bag->pimpl_->stoppedRunnables_.pop(); |
0 | 104 |
105 assert(r.get() != NULL); | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
106 assert(bag->pimpl_->activeRunnables_.find(r.get()) != bag->pimpl_->activeRunnables_.end()); |
0 | 107 |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
108 std::auto_ptr<boost::thread> t(bag->pimpl_->activeRunnables_[r.get()]); |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
109 bag->pimpl_->activeRunnables_.erase(r.get()); |
0 | 110 |
111 assert(t.get() != NULL); | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
112 assert(bag->pimpl_->activeRunnables_.find(r.get()) == bag->pimpl_->activeRunnables_.end()); |
0 | 113 |
431
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
114 if (t->joinable()) |
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
115 { |
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
116 t->join(); |
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
117 } |
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
118 |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
119 bag->pimpl_->oneRunnableIsJoined_.notify_one(); |
0 | 120 } |
121 | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
122 bag->pimpl_->oneRunnableHasStopped_.wait(lock); |
0 | 123 } |
124 } | |
125 | |
126 | |
127 BagOfRunnablesBySteps::BagOfRunnablesBySteps() : pimpl_(new PImpl) | |
128 { | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
129 pimpl_->continueActiveRunnables_ = true; |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
130 pimpl_->continueCollector_ = true; |
0 | 131 |
132 // Everyting is set up, the finish listener can be started | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
133 pimpl_->collector_.reset(new boost::thread(CollectorThread, this)); |
0 | 134 } |
135 | |
136 | |
137 BagOfRunnablesBySteps::~BagOfRunnablesBySteps() | |
138 { | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
139 if (pimpl_->continueCollector_) |
431
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
140 { |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
141 LOG(ERROR) << "INTERNAL ERROR: BagOfRunnablesBySteps::Finalize() should be invoked manually to avoid mess in the destruction order!"; |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
142 Finalize(); |
431
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
143 } |
0 | 144 } |
145 | |
146 | |
147 void BagOfRunnablesBySteps::Add(IRunnableBySteps* runnable) | |
148 { | |
149 // Make sure the runnable is deleted is something goes wrong | |
150 std::auto_ptr<IRunnableBySteps> runnableRabi(runnable); | |
151 | |
152 boost::mutex::scoped_lock lock(pimpl_->mutex_); | |
153 boost::thread* t(new boost::thread(RunnableThread, this, runnable)); | |
154 | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
155 pimpl_->activeRunnables_.insert(std::make_pair(runnableRabi.release(), t)); |
0 | 156 } |
157 | |
158 | |
159 void BagOfRunnablesBySteps::StopAll() | |
160 { | |
161 boost::mutex::scoped_lock lock(pimpl_->mutex_); | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
162 pimpl_->continueActiveRunnables_ = false; |
0 | 163 |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
164 while (pimpl_->activeRunnables_.size() > 0) |
0 | 165 { |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
166 pimpl_->oneRunnableIsJoined_.wait(lock); |
0 | 167 } |
168 | |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
169 pimpl_->continueActiveRunnables_ = true; |
0 | 170 } |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
171 |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
172 |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
173 |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
174 void BagOfRunnablesBySteps::Finalize() |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
175 { |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
176 if (pimpl_->continueCollector_) |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
177 { |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
178 StopAll(); |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
179 |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
180 // Stop the finish listener |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
181 pimpl_->continueCollector_ = false; |
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
182 pimpl_->oneRunnableHasStopped_.notify_one(); // Awakens the listener |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
183 |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
184 if (pimpl_->collector_->joinable()) |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
185 { |
1678
1a3c20cd1b53
renames in BagOfRunnablesBySteps to make things clearer
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1486
diff
changeset
|
186 pimpl_->collector_->join(); |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
187 } |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
188 } |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
189 } |
0 | 190 } |