Mercurial > hg > orthanc
annotate Core/MultiThreading/BagOfRunnablesBySteps.cpp @ 1529:4c1dbfddabd4
fix windows build
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 12 Aug 2015 14:25:17 +0200 |
parents | f967bdf8534e |
children | 1a3c20cd1b53 |
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 { | |
45 bool continue_; | |
46 bool stopFinishListener_; | |
47 | |
48 boost::mutex mutex_; | |
49 boost::condition_variable oneThreadIsStopped_; | |
50 boost::condition_variable oneThreadIsJoined_; | |
51 | |
52 // The list of threads that are waiting to be joined. | |
53 typedef std::stack<IRunnableBySteps*> StoppedThreads; | |
54 StoppedThreads stoppedThreads_; | |
55 | |
56 // The set of active runnables, i.e. the runnables that have not | |
57 // finished their job yet, plus the runnables that have not been | |
58 // joined yet. | |
59 typedef std::map<IRunnableBySteps*, boost::thread*> ActiveThreads; | |
60 ActiveThreads activeThreads_; | |
61 | |
62 // The thread that joins the runnables after they stop | |
63 std::auto_ptr<boost::thread> finishListener_; | |
64 }; | |
65 | |
66 | |
67 | |
68 void BagOfRunnablesBySteps::RunnableThread(BagOfRunnablesBySteps* bag, | |
69 IRunnableBySteps* runnable) | |
70 { | |
71 while (bag->pimpl_->continue_) | |
72 { | |
73 if (!runnable->Step()) | |
74 { | |
75 break; | |
76 } | |
77 } | |
78 | |
79 { | |
80 // Register this runnable as having stopped | |
81 boost::mutex::scoped_lock lock(bag->pimpl_->mutex_); | |
82 bag->pimpl_->stoppedThreads_.push(runnable); | |
83 bag->pimpl_->oneThreadIsStopped_.notify_one(); | |
84 } | |
85 } | |
86 | |
87 | |
88 void BagOfRunnablesBySteps::FinishListener(BagOfRunnablesBySteps* bag) | |
89 { | |
90 boost::mutex::scoped_lock lock(bag->pimpl_->mutex_); | |
91 | |
92 while (!bag->pimpl_->stopFinishListener_) | |
93 { | |
94 while (!bag->pimpl_->stoppedThreads_.empty()) | |
95 { | |
96 std::auto_ptr<IRunnableBySteps> r(bag->pimpl_->stoppedThreads_.top()); | |
97 bag->pimpl_->stoppedThreads_.pop(); | |
98 | |
99 assert(r.get() != NULL); | |
100 assert(bag->pimpl_->activeThreads_.find(r.get()) != bag->pimpl_->activeThreads_.end()); | |
101 | |
102 std::auto_ptr<boost::thread> t(bag->pimpl_->activeThreads_[r.get()]); | |
103 bag->pimpl_->activeThreads_.erase(r.get()); | |
104 | |
105 assert(t.get() != NULL); | |
106 assert(bag->pimpl_->activeThreads_.find(r.get()) == bag->pimpl_->activeThreads_.end()); | |
107 | |
431
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
108 if (t->joinable()) |
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
109 { |
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
110 t->join(); |
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
111 } |
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
112 |
0 | 113 bag->pimpl_->oneThreadIsJoined_.notify_one(); |
114 } | |
115 | |
116 bag->pimpl_->oneThreadIsStopped_.wait(lock); | |
117 } | |
118 } | |
119 | |
120 | |
121 BagOfRunnablesBySteps::BagOfRunnablesBySteps() : pimpl_(new PImpl) | |
122 { | |
123 pimpl_->continue_ = true; | |
124 pimpl_->stopFinishListener_ = false; | |
125 | |
126 // Everyting is set up, the finish listener can be started | |
127 pimpl_->finishListener_.reset(new boost::thread(FinishListener, this)); | |
128 } | |
129 | |
130 | |
131 BagOfRunnablesBySteps::~BagOfRunnablesBySteps() | |
132 { | |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
133 if (!pimpl_->stopFinishListener_) |
431
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
134 { |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
135 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
|
136 Finalize(); |
431
16b52fb8d034
fix by cyril paulus
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
398
diff
changeset
|
137 } |
0 | 138 } |
139 | |
140 | |
141 void BagOfRunnablesBySteps::Add(IRunnableBySteps* runnable) | |
142 { | |
143 // Make sure the runnable is deleted is something goes wrong | |
144 std::auto_ptr<IRunnableBySteps> runnableRabi(runnable); | |
145 | |
146 boost::mutex::scoped_lock lock(pimpl_->mutex_); | |
147 boost::thread* t(new boost::thread(RunnableThread, this, runnable)); | |
148 | |
149 pimpl_->activeThreads_.insert(std::make_pair(runnableRabi.release(), t)); | |
150 } | |
151 | |
152 | |
153 void BagOfRunnablesBySteps::StopAll() | |
154 { | |
155 boost::mutex::scoped_lock lock(pimpl_->mutex_); | |
156 pimpl_->continue_ = false; | |
157 | |
158 while (pimpl_->activeThreads_.size() > 0) | |
159 { | |
160 pimpl_->oneThreadIsJoined_.wait(lock); | |
161 } | |
162 | |
163 pimpl_->continue_ = true; | |
164 } | |
1453
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
165 |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
166 |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
167 |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
168 void BagOfRunnablesBySteps::Finalize() |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
169 { |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
170 if (!pimpl_->stopFinishListener_) |
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 StopAll(); |
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 // Stop the finish listener |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
175 pimpl_->stopFinishListener_ = true; |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
176 pimpl_->oneThreadIsStopped_.notify_one(); // Awakens the listener |
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 if (pimpl_->finishListener_->joinable()) |
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 pimpl_->finishListener_->join(); |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
181 } |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
182 } |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
183 } |
c0bdc47165ef
code to warn about possible threading problems
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
184 |
0 | 185 } |