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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
136
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
10 *
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
11 * In addition, as a special exception, the copyright holders of this
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
12 * program give permission to link the code of its release with the
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
15 * the linked executables. You must obey the GNU General Public License
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
19 * you do not wish to do so, delete this exception statement from your
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
20 * version. If you delete this exception statement from all source files
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
21 * in the program, then also delete it here.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
33 #include "../PrecompiledHeaders.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "BagOfRunnablesBySteps.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 #include <stack>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #include <boost/thread.hpp>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
41 namespace Orthanc
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 struct BagOfRunnablesBySteps::PImpl
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 };
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 void BagOfRunnablesBySteps::RunnableThread(BagOfRunnablesBySteps* bag,
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 IRunnableBySteps* runnable)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 if (!runnable->Step())
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 break;
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 // Register this runnable as having stopped
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 boost::mutex::scoped_lock lock(bag->pimpl_->mutex_);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 BagOfRunnablesBySteps::BagOfRunnablesBySteps() : pimpl_(new PImpl)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 BagOfRunnablesBySteps::~BagOfRunnablesBySteps()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 void BagOfRunnablesBySteps::Add(IRunnableBySteps* runnable)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 // Make sure the runnable is deleted is something goes wrong
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 std::auto_ptr<IRunnableBySteps> runnableRabi(runnable);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 boost::mutex::scoped_lock lock(pimpl_->mutex_);
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 boost::thread* t(new boost::thread(RunnableThread, this, runnable));
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 void BagOfRunnablesBySteps::StopAll()
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 {
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 }