Mercurial > hg > orthanc
annotate OrthancFramework/Sources/MultiThreading/RunnableWorkersPool.cpp @ 5111:7547c7dfd017
/tools/metrics-prometheus: added orthanc_last_change and orthanc_up_time_s
author | Alain Mazy <am@osimis.io> |
---|---|
date | Tue, 22 Nov 2022 18:01:23 +0100 |
parents | 43e613a7756b |
children | 0ea402b4d901 |
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 |
4870
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
1679 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 * the License, or (at your option) any later version. |
1679 | 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 | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
16 * Lesser General Public License for more details. |
1679 | 17 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
1679 | 21 **/ |
22 | |
23 | |
24 #include "../PrecompiledHeaders.h" | |
25 #include "RunnableWorkersPool.h" | |
26 | |
27 #include "SharedMessageQueue.h" | |
3712
2a170a8f1faf
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
28 #include "../Compatibility.h" |
1679 | 29 #include "../OrthancException.h" |
30 #include "../Logging.h" | |
31 | |
32 namespace Orthanc | |
33 { | |
34 struct RunnableWorkersPool::PImpl | |
35 { | |
36 class Worker | |
37 { | |
38 private: | |
39 const bool& continue_; | |
40 SharedMessageQueue& queue_; | |
41 boost::thread thread_; | |
42 | |
43 static void WorkerThread(Worker* that) | |
44 { | |
45 while (that->continue_) | |
46 { | |
2134
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
47 try |
1679 | 48 { |
3712
2a170a8f1faf
replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
49 std::unique_ptr<IDynamicObject> obj(that->queue_.Dequeue(100)); |
2134
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
50 if (obj.get() != NULL) |
1679 | 51 { |
52 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
|
53 |
1679 | 54 bool wishToContinue = runnable.Step(); |
2134
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
55 |
1679 | 56 if (wishToContinue) |
57 { | |
58 // The runnable wishes to continue, reinsert it at the beginning of the queue | |
59 that->queue_.Enqueue(obj.release()); | |
60 } | |
61 } | |
2134
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
62 } |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
63 catch (OrthancException& e) |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
64 { |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
65 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
|
66 } |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
67 catch (std::bad_alloc&) |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
68 { |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
69 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
|
70 } |
2290
4d500a555aad
catching std::exception before (...) to try to get more debug info
Alain Mazy <alain@mazy.be>
parents:
2244
diff
changeset
|
71 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
|
72 { |
2291 | 73 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
|
74 } |
2134
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
75 catch (...) |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
76 { |
ddc75c6c712d
Avoid hard crash if not enough memory
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
77 LOG(ERROR) << "Native exception while handling some runnable object"; |
1679 | 78 } |
79 } | |
80 } | |
81 | |
82 public: | |
83 Worker(const bool& globalContinue, | |
84 SharedMessageQueue& queue) : | |
85 continue_(globalContinue), | |
86 queue_(queue) | |
87 { | |
88 thread_ = boost::thread(WorkerThread, this); | |
89 } | |
90 | |
91 void Join() | |
92 { | |
93 if (thread_.joinable()) | |
94 { | |
95 thread_.join(); | |
96 } | |
97 } | |
98 }; | |
99 | |
100 | |
101 bool continue_; | |
102 std::vector<Worker*> workers_; | |
103 SharedMessageQueue queue_; | |
104 }; | |
105 | |
106 | |
107 | |
108 RunnableWorkersPool::RunnableWorkersPool(size_t countWorkers) : pimpl_(new PImpl) | |
109 { | |
110 pimpl_->continue_ = true; | |
111 | |
1847 | 112 if (countWorkers == 0) |
1679 | 113 { |
114 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
115 } | |
116 | |
117 pimpl_->workers_.resize(countWorkers); | |
118 | |
119 for (size_t i = 0; i < countWorkers; i++) | |
120 { | |
121 pimpl_->workers_[i] = new PImpl::Worker(pimpl_->continue_, pimpl_->queue_); | |
122 } | |
123 } | |
124 | |
125 | |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
126 void RunnableWorkersPool::Stop() |
1679 | 127 { |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
128 if (pimpl_->continue_) |
1679 | 129 { |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
130 pimpl_->continue_ = false; |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
131 |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
132 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
|
133 { |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
134 PImpl::Worker* worker = pimpl_->workers_[i]; |
1679 | 135 |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
136 if (worker != NULL) |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
137 { |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
138 worker->Join(); |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
139 delete worker; |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
140 } |
1679 | 141 } |
142 } | |
143 } | |
144 | |
145 | |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
146 RunnableWorkersPool::~RunnableWorkersPool() |
1679 | 147 { |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
148 Stop(); |
1679 | 149 } |
150 | |
151 | |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
152 void RunnableWorkersPool::Add(IRunnableBySteps* runnable) |
1679 | 153 { |
1681
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
154 if (!pimpl_->continue_) |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
155 { |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
156 throw OrthancException(ErrorCode_BadSequenceOfCalls); |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
157 } |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
158 |
ee4367497d0d
got rid of buggy BagOfRunnablesBySteps
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1679
diff
changeset
|
159 pimpl_->queue_.Enqueue(runnable); |
1679 | 160 } |
161 } |