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