Mercurial > hg > orthanc
comparison Core/MultiThreading/BagOfRunnablesBySteps.cpp @ 0:3959d33612cc
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 19 Jul 2012 14:32:22 +0200 |
parents | |
children | a15e90e5d6fc |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3959d33612cc |
---|---|
1 /** | |
2 * Palantir - A Lightweight, RESTful DICOM Store | |
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. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * General Public License for more details. | |
15 * | |
16 * You should have received a copy of the GNU General Public License | |
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 **/ | |
19 | |
20 | |
21 #include "BagOfRunnablesBySteps.h" | |
22 | |
23 #include <stack> | |
24 #include <boost/thread.hpp> | |
25 | |
26 namespace Palantir | |
27 { | |
28 struct BagOfRunnablesBySteps::PImpl | |
29 { | |
30 bool continue_; | |
31 bool stopFinishListener_; | |
32 | |
33 boost::mutex mutex_; | |
34 boost::condition_variable oneThreadIsStopped_; | |
35 boost::condition_variable oneThreadIsJoined_; | |
36 | |
37 // The list of threads that are waiting to be joined. | |
38 typedef std::stack<IRunnableBySteps*> StoppedThreads; | |
39 StoppedThreads stoppedThreads_; | |
40 | |
41 // The set of active runnables, i.e. the runnables that have not | |
42 // finished their job yet, plus the runnables that have not been | |
43 // joined yet. | |
44 typedef std::map<IRunnableBySteps*, boost::thread*> ActiveThreads; | |
45 ActiveThreads activeThreads_; | |
46 | |
47 // The thread that joins the runnables after they stop | |
48 std::auto_ptr<boost::thread> finishListener_; | |
49 }; | |
50 | |
51 | |
52 | |
53 void BagOfRunnablesBySteps::RunnableThread(BagOfRunnablesBySteps* bag, | |
54 IRunnableBySteps* runnable) | |
55 { | |
56 while (bag->pimpl_->continue_) | |
57 { | |
58 if (!runnable->Step()) | |
59 { | |
60 break; | |
61 } | |
62 } | |
63 | |
64 { | |
65 // Register this runnable as having stopped | |
66 boost::mutex::scoped_lock lock(bag->pimpl_->mutex_); | |
67 bag->pimpl_->stoppedThreads_.push(runnable); | |
68 bag->pimpl_->oneThreadIsStopped_.notify_one(); | |
69 } | |
70 } | |
71 | |
72 | |
73 void BagOfRunnablesBySteps::FinishListener(BagOfRunnablesBySteps* bag) | |
74 { | |
75 boost::mutex::scoped_lock lock(bag->pimpl_->mutex_); | |
76 | |
77 while (!bag->pimpl_->stopFinishListener_) | |
78 { | |
79 while (!bag->pimpl_->stoppedThreads_.empty()) | |
80 { | |
81 std::auto_ptr<IRunnableBySteps> r(bag->pimpl_->stoppedThreads_.top()); | |
82 bag->pimpl_->stoppedThreads_.pop(); | |
83 | |
84 assert(r.get() != NULL); | |
85 assert(bag->pimpl_->activeThreads_.find(r.get()) != bag->pimpl_->activeThreads_.end()); | |
86 | |
87 std::auto_ptr<boost::thread> t(bag->pimpl_->activeThreads_[r.get()]); | |
88 bag->pimpl_->activeThreads_.erase(r.get()); | |
89 | |
90 assert(t.get() != NULL); | |
91 assert(bag->pimpl_->activeThreads_.find(r.get()) == bag->pimpl_->activeThreads_.end()); | |
92 | |
93 t->join(); | |
94 bag->pimpl_->oneThreadIsJoined_.notify_one(); | |
95 } | |
96 | |
97 bag->pimpl_->oneThreadIsStopped_.wait(lock); | |
98 } | |
99 } | |
100 | |
101 | |
102 BagOfRunnablesBySteps::BagOfRunnablesBySteps() : pimpl_(new PImpl) | |
103 { | |
104 pimpl_->continue_ = true; | |
105 pimpl_->stopFinishListener_ = false; | |
106 | |
107 // Everyting is set up, the finish listener can be started | |
108 pimpl_->finishListener_.reset(new boost::thread(FinishListener, this)); | |
109 } | |
110 | |
111 | |
112 BagOfRunnablesBySteps::~BagOfRunnablesBySteps() | |
113 { | |
114 StopAll(); | |
115 | |
116 // Stop the finish listener | |
117 pimpl_->stopFinishListener_ = true; | |
118 pimpl_->oneThreadIsStopped_.notify_one(); // Awakens the listener | |
119 pimpl_->finishListener_->join(); | |
120 } | |
121 | |
122 | |
123 void BagOfRunnablesBySteps::Add(IRunnableBySteps* runnable) | |
124 { | |
125 // Make sure the runnable is deleted is something goes wrong | |
126 std::auto_ptr<IRunnableBySteps> runnableRabi(runnable); | |
127 | |
128 boost::mutex::scoped_lock lock(pimpl_->mutex_); | |
129 boost::thread* t(new boost::thread(RunnableThread, this, runnable)); | |
130 | |
131 pimpl_->activeThreads_.insert(std::make_pair(runnableRabi.release(), t)); | |
132 } | |
133 | |
134 | |
135 void BagOfRunnablesBySteps::StopAll() | |
136 { | |
137 boost::mutex::scoped_lock lock(pimpl_->mutex_); | |
138 pimpl_->continue_ = false; | |
139 | |
140 while (pimpl_->activeThreads_.size() > 0) | |
141 { | |
142 pimpl_->oneThreadIsJoined_.wait(lock); | |
143 } | |
144 | |
145 pimpl_->continue_ = true; | |
146 } | |
147 } |