Mercurial > hg > orthanc
annotate Core/MultiThreading/ThreadedCommandProcessor.cpp @ 769:3f946e5c3802
ReusableDicomUserConnection
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 30 Apr 2014 13:49:41 +0200 |
parents | 0da078f3affc |
children | a811bdf8b8eb |
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 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 #include "ThreadedCommandProcessor.h" |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 #include "../OrthancException.h" |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 namespace Orthanc |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 static const int32_t TIMEOUT = 10; |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 void ThreadedCommandProcessor::Processor(ThreadedCommandProcessor* that) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 while (!that->done_) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 std::auto_ptr<IDynamicObject> command(that->queue_.Dequeue(TIMEOUT)); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 if (command.get() != NULL) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 { |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
50 bool success = false; |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
51 |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 try |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 { |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
54 if (that->success_) |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
55 { |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
56 // No command has failed so far |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
57 |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
58 if (that->cancel_) |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
59 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
60 // The commands have been canceled. Skip the execution |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
61 // of this command, yet mark it as succeeded. |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
62 success = true; |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
63 } |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
64 else |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
65 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
66 success = dynamic_cast<ICommand&>(*command).Execute(); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
67 } |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
68 } |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
69 else |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
70 { |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
71 // 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
|
72 } |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
73 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 catch (OrthancException) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
75 { |
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 boost::mutex::scoped_lock lock(that->mutex_); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
80 assert(that->remainingCommands_ > 0); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
81 that->remainingCommands_--; |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
82 |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
83 if (!success) |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
84 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
85 if (!that->cancel_ && that->listener_ && that->success_) |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
86 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
87 // This is the first command that fails |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
88 that->listener_->SignalFailure(); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
89 } |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
90 |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
91 that->success_ = false; |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
92 } |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
93 else |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
94 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
95 if (!that->cancel_ && that->listener_) |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
96 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
97 if (that->remainingCommands_ == 0) |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
98 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
99 that->listener_->SignalSuccess(that->totalCommands_); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
100 } |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
101 else |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
102 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
103 that->listener_->SignalProgress(that->totalCommands_ - that->remainingCommands_, |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
104 that->totalCommands_); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
105 } |
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 } |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
108 |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
109 that->processedCommand_.notify_all(); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
110 } |
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 ThreadedCommandProcessor::ThreadedCommandProcessor(unsigned int numThreads) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
117 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
118 if (numThreads < 1) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
119 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
120 throw OrthancException(ErrorCode_ParameterOutOfRange); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
121 } |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
122 |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
123 listener_ = NULL; |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
124 success_ = true; |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
125 done_ = false; |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
126 cancel_ = false; |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
127 threads_.resize(numThreads); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
128 remainingCommands_ = 0; |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
129 totalCommands_ = 0; |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
130 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
131 for (unsigned int i = 0; i < numThreads; i++) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
132 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
133 threads_[i] = new boost::thread(Processor, this); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
134 } |
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 ThreadedCommandProcessor::~ThreadedCommandProcessor() |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
139 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
140 done_ = true; |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
141 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
142 for (unsigned int i = 0; i < threads_.size(); i++) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
143 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
144 boost::thread* t = threads_[i]; |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
145 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
146 if (t != NULL) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
147 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
148 if (t->joinable()) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
149 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
150 t->join(); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
151 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
152 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
153 delete t; |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
154 } |
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 void ThreadedCommandProcessor::Post(ICommand* command) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
160 { |
723
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
161 if (command == NULL) |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
162 { |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
163 throw OrthancException(ErrorCode_ParameterOutOfRange); |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
164 } |
0da078f3affc
multithreading tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
689
diff
changeset
|
165 |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
166 boost::mutex::scoped_lock lock(mutex_); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
167 queue_.Enqueue(command); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
168 remainingCommands_++; |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
169 totalCommands_++; |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
170 } |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
171 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
172 |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
173 bool ThreadedCommandProcessor::Join() |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
174 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
175 boost::mutex::scoped_lock lock(mutex_); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
176 |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
177 while (!remainingCommands_ == 0) |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
178 { |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
179 processedCommand_.wait(lock); |
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
180 } |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
181 |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
182 if (cancel_ && listener_) |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
183 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
184 listener_->SignalCancel(); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
185 } |
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 // Reset the sequence counters for subsequent commands |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
188 bool hasSucceeded = success_; |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
189 success_ = true; |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
190 totalCommands_ = 0; |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
191 cancel_ = false; |
466
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
192 |
9da3596069b8
handling failed commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
461
diff
changeset
|
193 return hasSucceeded; |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
194 } |
467
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
195 |
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 void ThreadedCommandProcessor::Cancel() |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
198 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
199 boost::mutex::scoped_lock lock(mutex_); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
200 |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
201 cancel_ = true; |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
202 } |
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 void ThreadedCommandProcessor::SetListener(IListener& listener) |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
206 { |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
207 boost::mutex::scoped_lock lock(mutex_); |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
208 listener_ = &listener; |
322c1b497036
cancel and listener for commands
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
466
diff
changeset
|
209 } |
458
84966299c8f8
ThreadedCommandProcessor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
210 } |