0
|
1 /**
|
59
|
2 * Orthanc - A Lightweight, RESTful DICOM Store
|
0
|
3 * Copyright (C) 2012 Medical Physics Department, CHU of Liege,
|
|
4 * Belgium
|
|
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
|
|
33 #include "BagOfRunnablesBySteps.h"
|
|
34
|
|
35 #include <stack>
|
|
36 #include <boost/thread.hpp>
|
|
37
|
59
|
38 namespace Orthanc
|
0
|
39 {
|
|
40 struct BagOfRunnablesBySteps::PImpl
|
|
41 {
|
|
42 bool continue_;
|
|
43 bool stopFinishListener_;
|
|
44
|
|
45 boost::mutex mutex_;
|
|
46 boost::condition_variable oneThreadIsStopped_;
|
|
47 boost::condition_variable oneThreadIsJoined_;
|
|
48
|
|
49 // The list of threads that are waiting to be joined.
|
|
50 typedef std::stack<IRunnableBySteps*> StoppedThreads;
|
|
51 StoppedThreads stoppedThreads_;
|
|
52
|
|
53 // The set of active runnables, i.e. the runnables that have not
|
|
54 // finished their job yet, plus the runnables that have not been
|
|
55 // joined yet.
|
|
56 typedef std::map<IRunnableBySteps*, boost::thread*> ActiveThreads;
|
|
57 ActiveThreads activeThreads_;
|
|
58
|
|
59 // The thread that joins the runnables after they stop
|
|
60 std::auto_ptr<boost::thread> finishListener_;
|
|
61 };
|
|
62
|
|
63
|
|
64
|
|
65 void BagOfRunnablesBySteps::RunnableThread(BagOfRunnablesBySteps* bag,
|
|
66 IRunnableBySteps* runnable)
|
|
67 {
|
|
68 while (bag->pimpl_->continue_)
|
|
69 {
|
|
70 if (!runnable->Step())
|
|
71 {
|
|
72 break;
|
|
73 }
|
|
74 }
|
|
75
|
|
76 {
|
|
77 // Register this runnable as having stopped
|
|
78 boost::mutex::scoped_lock lock(bag->pimpl_->mutex_);
|
|
79 bag->pimpl_->stoppedThreads_.push(runnable);
|
|
80 bag->pimpl_->oneThreadIsStopped_.notify_one();
|
|
81 }
|
|
82 }
|
|
83
|
|
84
|
|
85 void BagOfRunnablesBySteps::FinishListener(BagOfRunnablesBySteps* bag)
|
|
86 {
|
|
87 boost::mutex::scoped_lock lock(bag->pimpl_->mutex_);
|
|
88
|
|
89 while (!bag->pimpl_->stopFinishListener_)
|
|
90 {
|
|
91 while (!bag->pimpl_->stoppedThreads_.empty())
|
|
92 {
|
|
93 std::auto_ptr<IRunnableBySteps> r(bag->pimpl_->stoppedThreads_.top());
|
|
94 bag->pimpl_->stoppedThreads_.pop();
|
|
95
|
|
96 assert(r.get() != NULL);
|
|
97 assert(bag->pimpl_->activeThreads_.find(r.get()) != bag->pimpl_->activeThreads_.end());
|
|
98
|
|
99 std::auto_ptr<boost::thread> t(bag->pimpl_->activeThreads_[r.get()]);
|
|
100 bag->pimpl_->activeThreads_.erase(r.get());
|
|
101
|
|
102 assert(t.get() != NULL);
|
|
103 assert(bag->pimpl_->activeThreads_.find(r.get()) == bag->pimpl_->activeThreads_.end());
|
|
104
|
|
105 t->join();
|
|
106 bag->pimpl_->oneThreadIsJoined_.notify_one();
|
|
107 }
|
|
108
|
|
109 bag->pimpl_->oneThreadIsStopped_.wait(lock);
|
|
110 }
|
|
111 }
|
|
112
|
|
113
|
|
114 BagOfRunnablesBySteps::BagOfRunnablesBySteps() : pimpl_(new PImpl)
|
|
115 {
|
|
116 pimpl_->continue_ = true;
|
|
117 pimpl_->stopFinishListener_ = false;
|
|
118
|
|
119 // Everyting is set up, the finish listener can be started
|
|
120 pimpl_->finishListener_.reset(new boost::thread(FinishListener, this));
|
|
121 }
|
|
122
|
|
123
|
|
124 BagOfRunnablesBySteps::~BagOfRunnablesBySteps()
|
|
125 {
|
|
126 StopAll();
|
|
127
|
|
128 // Stop the finish listener
|
|
129 pimpl_->stopFinishListener_ = true;
|
|
130 pimpl_->oneThreadIsStopped_.notify_one(); // Awakens the listener
|
|
131 pimpl_->finishListener_->join();
|
|
132 }
|
|
133
|
|
134
|
|
135 void BagOfRunnablesBySteps::Add(IRunnableBySteps* runnable)
|
|
136 {
|
|
137 // Make sure the runnable is deleted is something goes wrong
|
|
138 std::auto_ptr<IRunnableBySteps> runnableRabi(runnable);
|
|
139
|
|
140 boost::mutex::scoped_lock lock(pimpl_->mutex_);
|
|
141 boost::thread* t(new boost::thread(RunnableThread, this, runnable));
|
|
142
|
|
143 pimpl_->activeThreads_.insert(std::make_pair(runnableRabi.release(), t));
|
|
144 }
|
|
145
|
|
146
|
|
147 void BagOfRunnablesBySteps::StopAll()
|
|
148 {
|
|
149 boost::mutex::scoped_lock lock(pimpl_->mutex_);
|
|
150 pimpl_->continue_ = false;
|
|
151
|
|
152 while (pimpl_->activeThreads_.size() > 0)
|
|
153 {
|
|
154 pimpl_->oneThreadIsJoined_.wait(lock);
|
|
155 }
|
|
156
|
|
157 pimpl_->continue_ = true;
|
|
158 }
|
|
159 }
|