Mercurial > hg > orthanc
annotate Core/MultiThreading/ThreadedCommandProcessor.cpp @ 833:30efdc8fac70
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 02 Jun 2014 12:17:47 +0200 |
parents | a811bdf8b8eb |
children | 6e7e5ed91c2d |
rev | line source |
---|---|
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 /** |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 * Orthanc - A Lightweight, RESTful DICOM Store |
689 | 3 * Copyright (C) 2012-2014 Medical Physics Department, CHU of Liege, |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 * Belgium |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 * |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 * This program is free software: you can redistribute it and/or |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 * modify it under the terms of the GNU General Public License as |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 * published by the Free Software Foundation, either version 3 of the |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 * License, or (at your option) any later version. |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 * |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 * In addition, as a special exception, the copyright holders of this |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 * program give permission to link the code of its release with the |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 * that use the same license as the "OpenSSL" library), and distribute |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 * the linked executables. You must obey the GNU General Public License |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 * in all respects for all of the code used other than "OpenSSL". If you |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 * modify file(s) with this exception, you may extend this exception to |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 * your version of the file(s), but you are not obligated to do so. If |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 * you do not wish to do so, delete this exception statement from your |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 * version. If you delete this exception statement from all source files |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 * in the program, then also delete it here. |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 * |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 * This program is distributed in the hope that it will be useful, but |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 * WITHOUT ANY WARRANTY; without even the implied warranty of |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 * General Public License for more details. |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 * |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 * You should have received a copy of the GNU General Public License |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 **/ |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
723
diff
changeset
|
33 #include "../PrecompiledHeaders.h" |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 #include "ThreadedCommandProcessor.h" |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 #include "../OrthancException.h" |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 namespace Orthanc |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 static const int32_t TIMEOUT = 10; |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 void ThreadedCommandProcessor::Processor(ThreadedCommandProcessor* that) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 while (!that->done_) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 std::auto_ptr<IDynamicObject> command(that->queue_.Dequeue(TIMEOUT)); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 if (command.get() != NULL) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 { |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
51 bool success = false; |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
52 |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 try |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 { |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
55 if (that->success_) |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
56 { |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
57 // No command has failed so far |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
58 |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
59 if (that->cancel_) |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
60 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
61 // The commands have been canceled. Skip the execution |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
62 // of this command, yet mark it as succeeded. |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
63 success = true; |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
64 } |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
65 else |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
66 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
67 success = dynamic_cast<ICommand&>(*command).Execute(); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
68 } |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
69 } |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
70 else |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
71 { |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
72 // A command has already failed. Skip the execution of this command. |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
73 } |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
75 catch (OrthancException) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
76 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
80 boost::mutex::scoped_lock lock(that->mutex_); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
81 assert(that->remainingCommands_ > 0); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
82 that->remainingCommands_--; |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
83 |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
84 if (!success) |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
85 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
86 if (!that->cancel_ && that->listener_ && that->success_) |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
87 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
88 // This is the first command that fails |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
89 that->listener_->SignalFailure(); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
90 } |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
91 |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
92 that->success_ = false; |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
93 } |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
94 else |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
95 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
96 if (!that->cancel_ && that->listener_) |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
97 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
98 if (that->remainingCommands_ == 0) |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
99 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
100 that->listener_->SignalSuccess(that->totalCommands_); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
101 } |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
102 else |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
103 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
104 that->listener_->SignalProgress(that->totalCommands_ - that->remainingCommands_, |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
105 that->totalCommands_); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
106 } |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
107 } |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
108 } |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
109 |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
110 that->processedCommand_.notify_all(); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
111 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
112 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
113 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
114 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
115 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
116 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
117 ThreadedCommandProcessor::ThreadedCommandProcessor(unsigned int numThreads) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
118 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
119 if (numThreads < 1) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
120 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
121 throw OrthancException(ErrorCode_ParameterOutOfRange); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
122 } |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
123 |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
124 listener_ = NULL; |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
125 success_ = true; |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
126 done_ = false; |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
127 cancel_ = false; |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
128 threads_.resize(numThreads); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
129 remainingCommands_ = 0; |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
130 totalCommands_ = 0; |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
131 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
132 for (unsigned int i = 0; i < numThreads; i++) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
133 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
134 threads_[i] = new boost::thread(Processor, this); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
135 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
136 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
137 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
138 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
139 ThreadedCommandProcessor::~ThreadedCommandProcessor() |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
140 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
141 done_ = true; |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
142 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
143 for (unsigned int i = 0; i < threads_.size(); i++) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
144 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
145 boost::thread* t = threads_[i]; |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
146 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
147 if (t != NULL) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
148 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
149 if (t->joinable()) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
150 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
151 t->join(); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
152 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
153 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
154 delete t; |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
155 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
156 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
157 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
158 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
159 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
160 void ThreadedCommandProcessor::Post(ICommand* command) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
161 { |
723
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
162 if (command == NULL) |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
163 { |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
164 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
165 } |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
166 |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
167 boost::mutex::scoped_lock lock(mutex_); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
168 queue_.Enqueue(command); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
169 remainingCommands_++; |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
170 totalCommands_++; |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
171 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
172 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
173 |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
174 bool ThreadedCommandProcessor::Join() |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
175 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
176 boost::mutex::scoped_lock lock(mutex_); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
177 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
178 while (!remainingCommands_ == 0) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
179 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
180 processedCommand_.wait(lock); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
181 } |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
182 |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
183 if (cancel_ && listener_) |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
184 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
185 listener_->SignalCancel(); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
186 } |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
187 |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
188 // Reset the sequence counters for subsequent commands |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
189 bool hasSucceeded = success_; |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
190 success_ = true; |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
191 totalCommands_ = 0; |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
192 cancel_ = false; |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
193 |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
194 return hasSucceeded; |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
195 } |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
196 |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
197 |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
198 void ThreadedCommandProcessor::Cancel() |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
199 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
200 boost::mutex::scoped_lock lock(mutex_); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
201 |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
202 cancel_ = true; |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
203 } |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
204 |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
205 |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
206 void ThreadedCommandProcessor::SetListener(IListener& listener) |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
207 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
208 boost::mutex::scoped_lock lock(mutex_); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
209 listener_ = &listener; |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
210 } |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
211 } |