comparison OrthancStone/Sources/Loaders/OracleScheduler.cpp @ 1921:3b2445574705

fix OracleScheduler::SpawnFromQueue() if using cache
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 22 Mar 2022 10:23:44 +0100
parents 7053b8a0aaec
children 07964689cb0b
comparison
equal deleted inserted replaced
1920:05f0327d26c8 1921:3b2445574705
159 } 159 }
160 160
161 161
162 void OracleScheduler::CheckInvariants() const 162 void OracleScheduler::CheckInvariants() const
163 { 163 {
164 #if 0
165 char buf[1024];
166 sprintf(buf, "active: %d %d %d ; pending: %lu %lu %lu",
167 activeHighPriorityCommands_, activeStandardPriorityCommands_, activeLowPriorityCommands_,
168 highPriorityQueue_.size(), standardPriorityQueue_.size(), lowPriorityQueue_.size());
169 LOG(WARNING) << buf;
170 #endif
171
164 #ifndef NDEBUG 172 #ifndef NDEBUG
165 /*char buf[1024];
166 sprintf(buf, "active: %d %d %d ; pending: %lu %lu %lu",
167 activeHighPriorityCommands_, activeStandardPriorityCommands_, activeLowPriorityCommands_,
168 highPriorityQueue_.size(), standardPriorityQueue_.size(), lowPriorityQueue_.size());
169 LOG(INFO) << buf;*/
170
171 assert(activeHighPriorityCommands_ <= maxHighPriorityCommands_); 173 assert(activeHighPriorityCommands_ <= maxHighPriorityCommands_);
172 assert(activeStandardPriorityCommands_ <= maxStandardPriorityCommands_); 174 assert(activeStandardPriorityCommands_ <= maxStandardPriorityCommands_);
173 assert(activeLowPriorityCommands_ <= maxLowPriorityCommands_); 175 assert(activeLowPriorityCommands_ <= maxLowPriorityCommands_);
174 assert(totalProcessed_ <= totalScheduled_); 176 assert(totalProcessed_ <= totalScheduled_);
175 177
190 } 192 }
191 #endif 193 #endif
192 } 194 }
193 195
194 196
197 void OracleScheduler::ModifyNumberOfActiveCommands(Priority priority,
198 int delta)
199 {
200 switch (priority)
201 {
202 case Priority_High:
203 assert(static_cast<int>(activeHighPriorityCommands_) + delta >= 0);
204 activeHighPriorityCommands_ += delta;
205 break;
206
207 case Priority_Standard:
208 assert(static_cast<int>(activeStandardPriorityCommands_) + delta >= 0);
209 activeStandardPriorityCommands_ += delta;
210 break;
211
212 case Priority_Low:
213 assert(static_cast<int>(activeLowPriorityCommands_) + delta >= 0);
214 activeLowPriorityCommands_ += delta;
215 break;
216
217 default:
218 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
219 }
220 }
221
222
195 void OracleScheduler::SpawnFromQueue(Queue& queue, 223 void OracleScheduler::SpawnFromQueue(Queue& queue,
196 Priority priority) 224 Priority priority)
197 { 225 {
198 CheckInvariants(); 226 CheckInvariants();
199 227
210 * receiver has not been destroyed yet. 238 * receiver has not been destroyed yet.
211 **/ 239 **/
212 boost::shared_ptr<IObserver> observer(command->GetReceiver().lock()); 240 boost::shared_ptr<IObserver> observer(command->GetReceiver().lock());
213 if (observer) 241 if (observer)
214 { 242 {
243 ModifyNumberOfActiveCommands(priority, 1);
244
215 if (oracle_.Schedule(GetSharedObserver(), command->WrapCommand(priority))) 245 if (oracle_.Schedule(GetSharedObserver(), command->WrapCommand(priority)))
216 { 246 {
217 /** 247 /**
218 * Executing this code if "Schedule()" returned "false" 248 * Executing this code if "Schedule()" returned "false"
219 * above, will result in a memory leak within 249 * above, will result in a memory leak within
222 * the oracle), hereby stalling the scheduler during its 252 * the oracle), hereby stalling the scheduler during its
223 * destruction, and not freeing the 253 * destruction, and not freeing the
224 * "shared_ptr<OracleScheduler>" of the Stone context (check 254 * "shared_ptr<OracleScheduler>" of the Stone context (check
225 * out "sjo-playground/WebViewer/Backend/Leak") 255 * out "sjo-playground/WebViewer/Backend/Leak")
226 **/ 256 **/
227
228 switch (priority)
229 {
230 case Priority_High:
231 activeHighPriorityCommands_ ++;
232 break;
233
234 case Priority_Standard:
235 activeStandardPriorityCommands_ ++;
236 break;
237
238 case Priority_Low:
239 activeLowPriorityCommands_ ++;
240 break;
241
242 default:
243 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
244 }
245 } 257 }
246 else 258 else
247 { 259 {
260 // This is similar to "RemoveActiveCommand()"
261 ModifyNumberOfActiveCommands(priority, -1);
248 totalProcessed_ ++; 262 totalProcessed_ ++;
249 } 263 }
250 } 264 }
251 } 265 }
252 else 266 else
318 { 332 {
319 CheckInvariants(); 333 CheckInvariants();
320 334
321 totalProcessed_ ++; 335 totalProcessed_ ++;
322 336
323 switch (payload.GetActivePriority()) 337 ModifyNumberOfActiveCommands(payload.GetActivePriority(), -1);
324 { 338
325 case Priority_High:
326 assert(activeHighPriorityCommands_ > 0);
327 activeHighPriorityCommands_ --;
328 break;
329
330 case Priority_Standard:
331 assert(activeStandardPriorityCommands_ > 0);
332 activeStandardPriorityCommands_ --;
333 break;
334
335 case Priority_Low:
336 assert(activeLowPriorityCommands_ > 0);
337 activeLowPriorityCommands_ --;
338 break;
339
340 default:
341 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
342 }
343
344 SpawnCommands(); 339 SpawnCommands();
345 340
346 CheckInvariants(); 341 CheckInvariants();
347 } 342 }
348 343