changeset 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 05f0327d26c8
children b64a58e35f90
files OrthancStone/Sources/Loaders/OracleScheduler.cpp OrthancStone/Sources/Loaders/OracleScheduler.h
diffstat 2 files changed, 43 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancStone/Sources/Loaders/OracleScheduler.cpp	Mon Mar 21 16:57:41 2022 +0100
+++ b/OrthancStone/Sources/Loaders/OracleScheduler.cpp	Tue Mar 22 10:23:44 2022 +0100
@@ -161,13 +161,15 @@
   
   void OracleScheduler::CheckInvariants() const
   {
+#if 0
+    char buf[1024];
+    sprintf(buf, "active: %d %d %d ; pending: %lu %lu %lu", 
+            activeHighPriorityCommands_, activeStandardPriorityCommands_, activeLowPriorityCommands_,
+            highPriorityQueue_.size(), standardPriorityQueue_.size(), lowPriorityQueue_.size());
+    LOG(WARNING) << buf;
+#endif
+  
 #ifndef NDEBUG
-    /*char buf[1024];
-      sprintf(buf, "active: %d %d %d ; pending: %lu %lu %lu", 
-      activeHighPriorityCommands_, activeStandardPriorityCommands_, activeLowPriorityCommands_,
-      highPriorityQueue_.size(), standardPriorityQueue_.size(), lowPriorityQueue_.size());
-      LOG(INFO) << buf;*/
-  
     assert(activeHighPriorityCommands_ <= maxHighPriorityCommands_);
     assert(activeStandardPriorityCommands_ <= maxStandardPriorityCommands_);
     assert(activeLowPriorityCommands_ <= maxLowPriorityCommands_);
@@ -192,6 +194,32 @@
   }
 
   
+  void OracleScheduler::ModifyNumberOfActiveCommands(Priority priority,
+                                                     int delta)
+  {
+    switch (priority)
+    {
+      case Priority_High:
+        assert(static_cast<int>(activeHighPriorityCommands_) + delta >= 0);
+        activeHighPriorityCommands_ += delta;
+        break;
+
+      case Priority_Standard:
+        assert(static_cast<int>(activeStandardPriorityCommands_) + delta >= 0);
+        activeStandardPriorityCommands_ += delta;
+        break;
+
+      case Priority_Low:
+        assert(static_cast<int>(activeLowPriorityCommands_) + delta >= 0);
+        activeLowPriorityCommands_ += delta;
+        break;
+
+      default:
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
+    }
+  }
+    
+
   void OracleScheduler::SpawnFromQueue(Queue& queue,
                                        Priority priority)
   {
@@ -212,6 +240,8 @@
       boost::shared_ptr<IObserver> observer(command->GetReceiver().lock());
       if (observer)
       {
+        ModifyNumberOfActiveCommands(priority, 1);
+        
         if (oracle_.Schedule(GetSharedObserver(), command->WrapCommand(priority)))
         {
           /**
@@ -224,27 +254,11 @@
            * "shared_ptr<OracleScheduler>" of the Stone context (check
            * out "sjo-playground/WebViewer/Backend/Leak")
            **/
-
-          switch (priority)
-          {
-            case Priority_High:
-              activeHighPriorityCommands_ ++;
-              break;
-
-            case Priority_Standard:
-              activeStandardPriorityCommands_ ++;
-              break;
-
-            case Priority_Low:
-              activeLowPriorityCommands_ ++;
-              break;
-
-            default:
-              throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
-          }
         }
         else
         {
+          // This is similar to "RemoveActiveCommand()"
+          ModifyNumberOfActiveCommands(priority, -1);
           totalProcessed_ ++;
         }
       }
@@ -320,27 +334,8 @@
 
     totalProcessed_ ++;
 
-    switch (payload.GetActivePriority())
-    {
-      case Priority_High:
-        assert(activeHighPriorityCommands_ > 0);
-        activeHighPriorityCommands_ --;
-        break;
-
-      case Priority_Standard:
-        assert(activeStandardPriorityCommands_ > 0);
-        activeStandardPriorityCommands_ --;
-        break;
-
-      case Priority_Low:
-        assert(activeLowPriorityCommands_ > 0);
-        activeLowPriorityCommands_ --;
-        break;
-
-      default:
-        throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
-    }
-
+    ModifyNumberOfActiveCommands(payload.GetActivePriority(), -1);
+    
     SpawnCommands();
 
     CheckInvariants();
--- a/OrthancStone/Sources/Loaders/OracleScheduler.h	Mon Mar 21 16:57:41 2022 +0100
+++ b/OrthancStone/Sources/Loaders/OracleScheduler.h	Tue Mar 22 10:23:44 2022 +0100
@@ -85,6 +85,9 @@
 
     void CheckInvariants() const;
 
+    void ModifyNumberOfActiveCommands(Priority priority,
+                                      int delta);
+
     void SpawnFromQueue(Queue& queue,
                         Priority priority);