Mercurial > hg > orthanc-stone
annotate Platforms/Generic/Oracle.cpp @ 150:62670cc2bb50 wasm
print matrix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 01 Feb 2018 16:07:54 +0100 |
parents | e2fe9352f240 |
children | e9c7a78a3e77 |
rev | line source |
---|---|
80 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
135
e2fe9352f240
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
80 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
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 | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "Oracle.h" | |
23 | |
113
2eca030792aa
using the Orthanc Framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
24 #include <Core/Logging.h> |
2eca030792aa
using the Orthanc Framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
25 #include <Core/MultiThreading/SharedMessageQueue.h> |
2eca030792aa
using the Orthanc Framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
89
diff
changeset
|
26 #include <Core/OrthancException.h> |
80 | 27 |
28 #include <vector> | |
89
f244018a4e4b
BUGGY- trying to remove IVolumeSlicesObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
29 #include <stdio.h> |
80 | 30 |
31 namespace OrthancStone | |
32 { | |
33 class Oracle::PImpl | |
34 { | |
35 private: | |
36 enum State | |
37 { | |
38 State_Init, | |
39 State_Started, | |
40 State_Stopped | |
41 }; | |
42 | |
43 boost::mutex* globalMutex_; | |
44 boost::mutex oracleMutex_; | |
45 State state_; | |
46 std::vector<boost::thread*> threads_; | |
47 Orthanc::SharedMessageQueue queue_; | |
48 | |
49 static void Worker(PImpl* that) | |
50 { | |
51 for (;;) | |
52 { | |
53 State state; | |
54 | |
55 { | |
56 boost::mutex::scoped_lock lock(that->oracleMutex_); | |
57 state = that->state_; | |
58 } | |
59 | |
60 if (state == State_Stopped) | |
61 { | |
62 break; | |
63 } | |
64 | |
65 std::auto_ptr<Orthanc::IDynamicObject> item(that->queue_.Dequeue(100)); | |
66 if (item.get() != NULL) | |
67 { | |
68 IOracleCommand& command = dynamic_cast<IOracleCommand&>(*item); | |
69 command.Execute(); | |
70 | |
89
f244018a4e4b
BUGGY- trying to remove IVolumeSlicesObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
71 // Random sleeping to test |
f244018a4e4b
BUGGY- trying to remove IVolumeSlicesObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
72 //boost::this_thread::sleep(boost::posix_time::milliseconds(50 * (1 + rand() % 10))); |
f244018a4e4b
BUGGY- trying to remove IVolumeSlicesObserver
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
87
diff
changeset
|
73 |
80 | 74 if (that->globalMutex_ != NULL) |
75 { | |
76 boost::mutex::scoped_lock lock(*that->globalMutex_); | |
77 command.Commit(); | |
78 } | |
79 else | |
80 { | |
81 command.Commit(); | |
82 } | |
83 } | |
84 } | |
85 } | |
86 | |
87 public: | |
88 PImpl(boost::mutex* globalMutex, | |
89 unsigned int threadCount) : | |
90 globalMutex_(globalMutex), | |
91 state_(State_Init), | |
92 threads_(threadCount) | |
93 { | |
94 } | |
95 | |
96 ~PImpl() | |
97 { | |
98 if (state_ == State_Started) | |
99 { | |
100 LOG(ERROR) << "You should have manually called Oracle::Stop()"; | |
101 Stop(); | |
102 } | |
103 } | |
104 | |
87
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
105 Orthanc::SharedMessageQueue& GetQueue() |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
106 { |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
107 return queue_; |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
108 } |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
109 |
80 | 110 void Submit(IOracleCommand* command) |
111 { | |
112 std::auto_ptr<IOracleCommand> protection(command); | |
113 | |
114 if (command == NULL) | |
115 { | |
116 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
117 } | |
118 | |
119 boost::mutex::scoped_lock lock(oracleMutex_); | |
120 | |
121 switch (state_) | |
122 { | |
123 case State_Init: | |
124 case State_Started: | |
125 queue_.Enqueue(protection.release()); | |
126 break; | |
127 | |
128 case State_Stopped: | |
129 LOG(ERROR) << "Cannot schedule a request to the Oracle after having " | |
130 << "called Oracle::Stop()"; | |
131 break; | |
132 | |
133 default: | |
134 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
135 } | |
136 | |
137 } | |
138 | |
139 void Start() | |
140 { | |
141 boost::mutex::scoped_lock lock(oracleMutex_); | |
142 | |
143 if (state_ != State_Init) | |
144 { | |
145 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
146 } | |
147 | |
148 for (size_t i = 0; i < threads_.size(); i++) | |
149 { | |
150 threads_[i] = new boost::thread(Worker, this); | |
151 } | |
152 | |
153 state_ = State_Started; | |
154 } | |
155 | |
156 void Stop() | |
157 { | |
158 { | |
159 boost::mutex::scoped_lock lock(oracleMutex_); | |
160 | |
161 if (state_ != State_Started) | |
162 { | |
163 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
164 } | |
165 | |
166 state_ = State_Stopped; | |
167 } | |
168 | |
169 for (size_t i = 0; i < threads_.size(); i++) | |
170 { | |
171 if (threads_[i] != NULL) | |
172 { | |
173 if (threads_[i]->joinable()) | |
174 { | |
175 threads_[i]->join(); | |
176 } | |
177 | |
178 delete threads_[i]; | |
179 } | |
180 } | |
181 } | |
182 }; | |
183 | |
184 | |
185 Oracle::Oracle(boost::mutex& globalMutex, | |
186 unsigned int threadCount) : | |
187 pimpl_(new PImpl(&globalMutex, threadCount)) | |
188 { | |
189 } | |
190 | |
191 | |
192 Oracle::Oracle(unsigned int threadCount) : | |
193 pimpl_(new PImpl(NULL, threadCount)) | |
194 { | |
195 } | |
196 | |
197 | |
198 void Oracle::Start() | |
199 { | |
200 pimpl_->Start(); | |
201 } | |
202 | |
203 | |
204 void Oracle::Submit(IOracleCommand* command) | |
205 { | |
206 pimpl_->Submit(command); | |
207 } | |
208 | |
209 | |
210 void Oracle::Stop() | |
211 { | |
212 pimpl_->Stop(); | |
213 } | |
87
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
214 |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
215 |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
216 void Oracle::WaitEmpty() |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
217 { |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
218 pimpl_->GetQueue().WaitEmpty(50); |
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
86
diff
changeset
|
219 } |
80 | 220 } |