Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Oracle/ThreadedOracle.cpp @ 1935:49c615745708
fix typo
author | Alain Mazy <am@osimis.io> |
---|---|
date | Mon, 25 Apr 2022 09:31:00 +0200 |
parents | 7053b8a0aaec |
children | 07964689cb0b |
rev | line source |
---|---|
748 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
1871
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
748 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
748 | 10 * as published by the Free Software Foundation, either version 3 of |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
16 * Lesser General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1571
diff
changeset
|
17 * |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
19 * License along with this program. If not, see |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
748 | 21 **/ |
22 | |
23 | |
24 #include "ThreadedOracle.h" | |
25 | |
26 #include "SleepOracleCommand.h" | |
27 | |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
28 #include <Logging.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1437
diff
changeset
|
29 #include <OrthancException.h> |
748 | 30 |
31 namespace OrthancStone | |
32 { | |
33 class ThreadedOracle::Item : public Orthanc::IDynamicObject | |
34 { | |
35 private: | |
1075 | 36 boost::weak_ptr<IObserver> receiver_; |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
37 std::unique_ptr<IOracleCommand> command_; |
748 | 38 |
39 public: | |
1075 | 40 Item(boost::weak_ptr<IObserver> receiver, |
748 | 41 IOracleCommand* command) : |
42 receiver_(receiver), | |
43 command_(command) | |
44 { | |
45 if (command == NULL) | |
46 { | |
47 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
48 } | |
49 } | |
50 | |
1098
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
51 boost::weak_ptr<IObserver> GetReceiver() |
748 | 52 { |
53 return receiver_; | |
54 } | |
55 | |
56 IOracleCommand& GetCommand() | |
57 { | |
58 assert(command_.get() != NULL); | |
59 return *command_; | |
60 } | |
61 }; | |
62 | |
63 | |
64 class ThreadedOracle::SleepingCommands : public boost::noncopyable | |
65 { | |
66 private: | |
67 class Item | |
68 { | |
69 private: | |
1075 | 70 boost::weak_ptr<IObserver> receiver_; |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
71 std::unique_ptr<SleepOracleCommand> command_; |
748 | 72 boost::posix_time::ptime expiration_; |
73 | |
74 public: | |
1098
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
75 Item(boost::weak_ptr<IObserver> receiver, |
748 | 76 SleepOracleCommand* command) : |
77 receiver_(receiver), | |
78 command_(command) | |
79 { | |
80 if (command == NULL) | |
81 { | |
82 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
83 } | |
84 | |
760
1181e1ad98ec
progressive loading working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
748
diff
changeset
|
85 expiration_ = (boost::posix_time::microsec_clock::local_time() + |
748 | 86 boost::posix_time::milliseconds(command_->GetDelay())); |
87 } | |
88 | |
89 const boost::posix_time::ptime& GetExpirationTime() const | |
90 { | |
91 return expiration_; | |
92 } | |
93 | |
94 void Awake(IMessageEmitter& emitter) | |
95 { | |
96 assert(command_.get() != NULL); | |
97 | |
98 SleepOracleCommand::TimeoutMessage message(*command_); | |
99 emitter.EmitMessage(receiver_, message); | |
100 } | |
101 }; | |
102 | |
103 typedef std::list<Item*> Content; | |
104 | |
105 boost::mutex mutex_; | |
106 Content content_; | |
107 | |
108 public: | |
109 ~SleepingCommands() | |
110 { | |
111 for (Content::iterator it = content_.begin(); it != content_.end(); ++it) | |
112 { | |
113 if (*it != NULL) | |
114 { | |
115 delete *it; | |
116 } | |
117 } | |
118 } | |
119 | |
1098
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
120 void Add(boost::weak_ptr<IObserver> receiver, |
748 | 121 SleepOracleCommand* command) // Takes ownership |
122 { | |
123 boost::mutex::scoped_lock lock(mutex_); | |
124 | |
125 content_.push_back(new Item(receiver, command)); | |
126 } | |
127 | |
128 void AwakeExpired(IMessageEmitter& emitter) | |
129 { | |
130 boost::mutex::scoped_lock lock(mutex_); | |
131 | |
760
1181e1ad98ec
progressive loading working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
748
diff
changeset
|
132 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); |
748 | 133 |
134 Content stillSleeping; | |
135 | |
136 for (Content::iterator it = content_.begin(); it != content_.end(); ++it) | |
137 { | |
138 if (*it != NULL && | |
139 (*it)->GetExpirationTime() <= now) | |
140 { | |
141 (*it)->Awake(emitter); | |
142 delete *it; | |
143 *it = NULL; | |
144 } | |
145 else | |
146 { | |
147 stillSleeping.push_back(*it); | |
148 } | |
149 } | |
150 | |
151 // Compact the still-sleeping commands | |
152 content_ = stillSleeping; | |
153 } | |
154 }; | |
155 | |
156 | |
157 void ThreadedOracle::Step() | |
158 { | |
1298
8a0a62189f46
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
159 std::unique_ptr<Orthanc::IDynamicObject> object(queue_.Dequeue(100)); |
748 | 160 |
161 if (object.get() != NULL) | |
162 { | |
163 Item& item = dynamic_cast<Item&>(*object); | |
164 | |
1077 | 165 if (item.GetCommand().GetType() == IOracleCommand::Type_Sleep) |
748 | 166 { |
1077 | 167 SleepOracleCommand& command = dynamic_cast<SleepOracleCommand&>(item.GetCommand()); |
168 | |
1299
c38c89684d83
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1279
diff
changeset
|
169 std::unique_ptr<SleepOracleCommand> copy(new SleepOracleCommand(command.GetDelay())); |
1077 | 170 |
171 if (command.HasPayload()) | |
748 | 172 { |
1128
8e3763d1736a
removing CustomOracleCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1126
diff
changeset
|
173 copy->AcquirePayload(command.ReleasePayload()); |
748 | 174 } |
1077 | 175 |
176 sleepingCommands_->Add(item.GetReceiver(), copy.release()); | |
748 | 177 } |
1077 | 178 else |
748 | 179 { |
1104 | 180 GenericOracleRunner runner; |
979 | 181 |
1109
79b1b541fe15
ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1104
diff
changeset
|
182 { |
79b1b541fe15
ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1104
diff
changeset
|
183 boost::mutex::scoped_lock lock(mutex_); |
79b1b541fe15
ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1104
diff
changeset
|
184 runner.SetOrthanc(orthanc_); |
79b1b541fe15
ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1104
diff
changeset
|
185 runner.SetRootDirectory(rootDirectory_); |
748 | 186 |
1126 | 187 #if ORTHANC_ENABLE_DCMTK == 1 |
1124
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
188 if (dicomCache_) |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
189 { |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
190 runner.SetDicomCache(dicomCache_); |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
191 } |
1126 | 192 #endif |
748 | 193 } |
1134
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1128
diff
changeset
|
194 |
87fbeb823375
allocating messages from oracle commands on the stack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1128
diff
changeset
|
195 runner.Run(item.GetReceiver(), emitter_, item.GetCommand()); |
748 | 196 } |
197 } | |
198 } | |
199 | |
200 | |
201 void ThreadedOracle::Worker(ThreadedOracle* that) | |
202 { | |
203 assert(that != NULL); | |
204 | |
205 for (;;) | |
206 { | |
207 { | |
208 boost::mutex::scoped_lock lock(that->mutex_); | |
209 if (that->state_ != State_Running) | |
210 { | |
211 return; | |
212 } | |
213 } | |
214 | |
215 that->Step(); | |
216 } | |
217 } | |
218 | |
219 | |
220 void ThreadedOracle::SleepingWorker(ThreadedOracle* that) | |
221 { | |
222 assert(that != NULL); | |
223 | |
224 for (;;) | |
225 { | |
226 { | |
227 boost::mutex::scoped_lock lock(that->mutex_); | |
228 if (that->state_ != State_Running) | |
229 { | |
230 return; | |
231 } | |
232 } | |
233 | |
234 that->sleepingCommands_->AwakeExpired(that->emitter_); | |
235 | |
236 boost::this_thread::sleep(boost::posix_time::milliseconds(that->sleepingTimeResolution_)); | |
237 } | |
238 } | |
239 | |
240 | |
241 void ThreadedOracle::StopInternal() | |
242 { | |
243 { | |
244 boost::mutex::scoped_lock lock(mutex_); | |
245 | |
246 if (state_ == State_Setup || | |
247 state_ == State_Stopped) | |
248 { | |
249 return; | |
250 } | |
251 else | |
252 { | |
253 state_ = State_Stopped; | |
254 } | |
255 } | |
256 | |
257 if (sleepingWorker_.joinable()) | |
258 { | |
259 sleepingWorker_.join(); | |
260 } | |
261 | |
262 for (size_t i = 0; i < workers_.size(); i++) | |
263 { | |
264 if (workers_[i] != NULL) | |
265 { | |
266 if (workers_[i]->joinable()) | |
267 { | |
268 workers_[i]->join(); | |
269 } | |
270 | |
271 delete workers_[i]; | |
272 } | |
273 } | |
274 } | |
275 | |
276 | |
277 ThreadedOracle::ThreadedOracle(IMessageEmitter& emitter) : | |
278 emitter_(emitter), | |
1109
79b1b541fe15
ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1104
diff
changeset
|
279 rootDirectory_("."), |
748 | 280 state_(State_Setup), |
281 workers_(4), | |
282 sleepingCommands_(new SleepingCommands), | |
283 sleepingTimeResolution_(50) // By default, time resolution of 50ms | |
284 { | |
285 } | |
286 | |
287 | |
765 | 288 ThreadedOracle::~ThreadedOracle() |
289 { | |
290 if (state_ == State_Running) | |
291 { | |
292 LOG(ERROR) << "The threaded oracle is still running, explicit call to " | |
293 << "Stop() is mandatory to avoid crashes"; | |
294 } | |
295 | |
296 try | |
297 { | |
298 StopInternal(); | |
299 } | |
300 catch (Orthanc::OrthancException& e) | |
301 { | |
302 LOG(ERROR) << "Exception while stopping the threaded oracle: " << e.What(); | |
303 } | |
304 catch (...) | |
305 { | |
306 LOG(ERROR) << "Native exception while stopping the threaded oracle"; | |
1097
4383382db01d
deprecating LockingEmitter
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1077
diff
changeset
|
307 } |
765 | 308 } |
309 | |
310 | |
748 | 311 void ThreadedOracle::SetOrthancParameters(const Orthanc::WebServiceParameters& orthanc) |
312 { | |
313 boost::mutex::scoped_lock lock(mutex_); | |
1109
79b1b541fe15
ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1104
diff
changeset
|
314 orthanc_ = orthanc; |
79b1b541fe15
ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1104
diff
changeset
|
315 } |
748 | 316 |
1109
79b1b541fe15
ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1104
diff
changeset
|
317 |
79b1b541fe15
ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1104
diff
changeset
|
318 void ThreadedOracle::SetRootDirectory(const std::string& rootDirectory) |
79b1b541fe15
ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1104
diff
changeset
|
319 { |
79b1b541fe15
ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1104
diff
changeset
|
320 boost::mutex::scoped_lock lock(mutex_); |
79b1b541fe15
ThreadedOracle::SetRootDirectory()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1104
diff
changeset
|
321 rootDirectory_ = rootDirectory; |
748 | 322 } |
323 | |
324 | |
760
1181e1ad98ec
progressive loading working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
748
diff
changeset
|
325 void ThreadedOracle::SetThreadsCount(unsigned int count) |
748 | 326 { |
327 boost::mutex::scoped_lock lock(mutex_); | |
328 | |
1571 | 329 if (count == 0) |
748 | 330 { |
331 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
332 } | |
333 else if (state_ != State_Setup) | |
334 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
819
diff
changeset
|
335 LOG(ERROR) << "ThreadedOracle::SetThreadsCount(): (state_ != State_Setup)"; |
748 | 336 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
337 } | |
338 else | |
339 { | |
340 workers_.resize(count); | |
341 } | |
342 } | |
343 | |
344 | |
345 void ThreadedOracle::SetSleepingTimeResolution(unsigned int milliseconds) | |
346 { | |
347 boost::mutex::scoped_lock lock(mutex_); | |
348 | |
1571 | 349 if (milliseconds == 0) |
748 | 350 { |
351 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
352 } | |
353 else if (state_ != State_Setup) | |
354 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
819
diff
changeset
|
355 LOG(ERROR) << "ThreadedOracle::SetSleepingTimeResolution(): (state_ != State_Setup)"; |
748 | 356 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
357 } | |
358 else | |
359 { | |
360 sleepingTimeResolution_ = milliseconds; | |
361 } | |
362 } | |
363 | |
364 | |
1124
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
365 void ThreadedOracle::SetDicomCacheSize(size_t size) |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
366 { |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
367 #if ORTHANC_ENABLE_DCMTK == 1 |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
368 boost::mutex::scoped_lock lock(mutex_); |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
369 |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
370 if (state_ != State_Setup) |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
371 { |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
372 LOG(ERROR) << "ThreadedOracle::SetDicomCacheSize(): (state_ != State_Setup)"; |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
373 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
374 } |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
375 else |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
376 { |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
377 if (size == 0) |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
378 { |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
379 dicomCache_.reset(); |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
380 } |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
381 else |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
382 { |
1150 | 383 dicomCache_.reset(new ParsedDicomCache(size)); |
1124
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
384 } |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
385 } |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
386 #endif |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
387 } |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
388 |
a8bf81756839
unsuccessful attempt to cache ParseDicomFileCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1109
diff
changeset
|
389 |
748 | 390 void ThreadedOracle::Start() |
391 { | |
392 boost::mutex::scoped_lock lock(mutex_); | |
393 | |
394 if (state_ != State_Setup) | |
395 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
819
diff
changeset
|
396 LOG(ERROR) << "ThreadedOracle::Start(): (state_ != State_Setup)"; |
748 | 397 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
398 } | |
399 else | |
400 { | |
1437
297e57d3508c
Moved two WARNING log entries to INFO. These weren't warnings.
Benjamin Golinvaux <bgo@osimis.io>
parents:
1300
diff
changeset
|
401 LOG(INFO) << "Starting oracle with " << workers_.size() << " worker threads"; |
748 | 402 state_ = State_Running; |
403 | |
404 for (unsigned int i = 0; i < workers_.size(); i++) | |
405 { | |
406 workers_[i] = new boost::thread(Worker, this); | |
407 } | |
408 | |
409 sleepingWorker_ = boost::thread(SleepingWorker, this); | |
410 } | |
411 } | |
412 | |
413 | |
1098
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
414 bool ThreadedOracle::Schedule(boost::shared_ptr<IObserver> receiver, |
748 | 415 IOracleCommand* command) |
416 { | |
1299
c38c89684d83
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1279
diff
changeset
|
417 std::unique_ptr<Item> item(new Item(receiver, command)); |
1098
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
418 |
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
419 { |
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
420 boost::mutex::scoped_lock lock(mutex_); |
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
421 |
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
422 if (state_ == State_Running) |
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
423 { |
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
424 //LOG(INFO) << "New oracle command queued"; |
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
425 queue_.Enqueue(item.release()); |
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
426 return true; |
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
427 } |
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
428 else |
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
429 { |
1140 | 430 LOG(TRACE) << "Command not enqueued, as the oracle has stopped"; |
1098
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
431 return false; |
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
432 } |
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1097
diff
changeset
|
433 } |
748 | 434 } |
435 } |