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