Mercurial > hg > orthanc
annotate Core/MultiThreading/RunnableWorkersPool.cpp @ 3535:41365091a41e
display a security warning in the logs at startup when ExecuteLuaEnabled is true
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Sun, 06 Oct 2019 09:52:57 +0200 |
parents | 4e43e67f8ecf |
children | 94f4a18a79cc |
rev | line source |
---|---|
1679 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1679 | 4 * Department, University Hospital of Liege, Belgium |
3060
4e43e67f8ecf
preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
1679 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "../PrecompiledHeaders.h" | |
35 #include "RunnableWorkersPool.h" | |
36 | |
37 #include "SharedMessageQueue.h" | |
38 #include "../OrthancException.h" | |
39 #include "../Logging.h" | |
40 | |
41 namespace Orthanc | |
42 { | |
43 struct RunnableWorkersPool::PImpl | |
44 { | |
45 class Worker | |
46 { | |
47 private: | |
48 const bool& continue_; | |
49 SharedMessageQueue& queue_; | |
50 boost::thread thread_; | |
51 | |
52 static void WorkerThread(Worker* that) | |
53 { | |
54 while (that->continue_) | |
55 { | |
2134
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
56 try |
1679 | 57 { |
2134
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
58 std::auto_ptr<IDynamicObject> obj(that->queue_.Dequeue(100)); |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
59 if (obj.get() != NULL) |
1679 | 60 { |
61 IRunnableBySteps& runnable = *dynamic_cast<IRunnableBySteps*>(obj.get()); | |
2134
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
62 |
1679 | 63 bool wishToContinue = runnable.Step(); |
2134
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
64 |
1679 | 65 if (wishToContinue) |
66 { | |
67 // The runnable wishes to continue, reinsert it at the beginning of the queue | |
68 that->queue_.Enqueue(obj.release()); | |
69 } | |
70 } | |
2134
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
71 } |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
72 catch (OrthancException& e) |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
73 { |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
74 LOG(ERROR) << "Exception while handling some runnable object: " << e.What(); |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
75 } |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
76 catch (std::bad_alloc&) |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
77 { |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
78 LOG(ERROR) << "Not enough memory to handle some runnable object"; |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
79 } |
2290
4d500a555aad
catching std::exception before (...) to try to get more debug info
Alain Mazy <alain@mazy.be>
parents:
2244
diff
changeset
|
80 catch (std::exception& e) |
4d500a555aad
catching std::exception before (...) to try to get more debug info
Alain Mazy <alain@mazy.be>
parents:
2244
diff
changeset
|
81 { |
2291 | 82 LOG(ERROR) << "std::exception while handling some runnable object: " << e.what(); |
2290
4d500a555aad
catching std::exception before (...) to try to get more debug info
Alain Mazy <alain@mazy.be>
parents:
2244
diff
changeset
|
83 } |
2134
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
84 catch (...) |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
85 { |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
86 LOG(ERROR) << "Native exception while handling some runnable object"; |
1679 | 87 } |
88 } | |
89 } | |
90 | |
91 public: | |
92 Worker(const bool& globalContinue, | |
93 SharedMessageQueue& queue) : | |
94 continue_(globalContinue), | |
95 queue_(queue) | |
96 { | |
97 thread_ = boost::thread(WorkerThread, this); | |
98 } | |
99 | |
100 void Join() | |
101 { | |
102 if (thread_.joinable()) | |
103 { | |
104 thread_.join(); | |
105 } | |
106 } | |
107 }; | |
108 | |
109 | |
110 bool continue_; | |
111 std::vector<Worker*> workers_; | |
112 SharedMessageQueue queue_; | |
113 }; | |
114 | |
115 | |
116 | |
117 RunnableWorkersPool::RunnableWorkersPool(size_t countWorkers) : pimpl_(new PImpl) | |
118 { | |
119 pimpl_->continue_ = true; | |
120 | |
1847 | 121 if (countWorkers == 0) |
1679 | 122 { |
123 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
124 } | |
125 | |
126 pimpl_->workers_.resize(countWorkers); | |
127 | |
128 for (size_t i = 0; i < countWorkers; i++) | |
129 { | |
130 pimpl_->workers_[i] = new PImpl::Worker(pimpl_->continue_, pimpl_->queue_); | |
131 } | |
132 } | |
133 | |
134 | |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
135 void RunnableWorkersPool::Stop() |
1679 | 136 { |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
137 if (pimpl_->continue_) |
1679 | 138 { |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
139 pimpl_->continue_ = false; |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
140 |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
141 for (size_t i = 0; i < pimpl_->workers_.size(); i++) |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
142 { |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
143 PImpl::Worker* worker = pimpl_->workers_[i]; |
1679 | 144 |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
145 if (worker != NULL) |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
146 { |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
147 worker->Join(); |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
148 delete worker; |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
149 } |
1679 | 150 } |
151 } | |
152 } | |
153 | |
154 | |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
155 RunnableWorkersPool::~RunnableWorkersPool() |
1679 | 156 { |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
157 Stop(); |
1679 | 158 } |
159 | |
160 | |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
161 void RunnableWorkersPool::Add(IRunnableBySteps* runnable) |
1679 | 162 { |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
163 if (!pimpl_->continue_) |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
164 { |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
165 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
166 } |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
167 |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
168 pimpl_->queue_.Enqueue(runnable); |
1679 | 169 } |
170 } |