Mercurial > hg > orthanc-stone
comparison Resources/Orthanc/Core/MultiThreading/RunnableWorkersPool.cpp @ 200:03afbee0cc7b
integration of Orthanc core into Stone
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 23 Mar 2018 11:04:03 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
199:dabe9982fca3 | 200:03afbee0cc7b |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
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 { | |
56 try | |
57 { | |
58 std::auto_ptr<IDynamicObject> obj(that->queue_.Dequeue(100)); | |
59 if (obj.get() != NULL) | |
60 { | |
61 IRunnableBySteps& runnable = *dynamic_cast<IRunnableBySteps*>(obj.get()); | |
62 | |
63 bool wishToContinue = runnable.Step(); | |
64 | |
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 } | |
71 } | |
72 catch (OrthancException& e) | |
73 { | |
74 LOG(ERROR) << "Exception while handling some runnable object: " << e.What(); | |
75 } | |
76 catch (std::bad_alloc&) | |
77 { | |
78 LOG(ERROR) << "Not enough memory to handle some runnable object"; | |
79 } | |
80 catch (std::exception& e) | |
81 { | |
82 LOG(ERROR) << "std::exception while handling some runnable object: " << e.what(); | |
83 } | |
84 catch (...) | |
85 { | |
86 LOG(ERROR) << "Native exception while handling some runnable object"; | |
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 | |
121 if (countWorkers == 0) | |
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 | |
135 void RunnableWorkersPool::Stop() | |
136 { | |
137 if (pimpl_->continue_) | |
138 { | |
139 pimpl_->continue_ = false; | |
140 | |
141 for (size_t i = 0; i < pimpl_->workers_.size(); i++) | |
142 { | |
143 PImpl::Worker* worker = pimpl_->workers_[i]; | |
144 | |
145 if (worker != NULL) | |
146 { | |
147 worker->Join(); | |
148 delete worker; | |
149 } | |
150 } | |
151 } | |
152 } | |
153 | |
154 | |
155 RunnableWorkersPool::~RunnableWorkersPool() | |
156 { | |
157 Stop(); | |
158 } | |
159 | |
160 | |
161 void RunnableWorkersPool::Add(IRunnableBySteps* runnable) | |
162 { | |
163 if (!pimpl_->continue_) | |
164 { | |
165 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
166 } | |
167 | |
168 pimpl_->queue_.Enqueue(runnable); | |
169 } | |
170 } |