changeset 1227:7266c37d0354

lifo policy in SharedMessageQueue
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 20 Nov 2014 11:58:47 +0100
parents 3f62e1269cca
children e0f7014d39a4
files Core/MultiThreading/SharedMessageQueue.cpp Core/MultiThreading/SharedMessageQueue.h
diffstat 2 files changed, 82 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/Core/MultiThreading/SharedMessageQueue.cpp	Wed Nov 19 11:15:34 2014 +0100
+++ b/Core/MultiThreading/SharedMessageQueue.cpp	Thu Nov 20 11:58:47 2014 +0100
@@ -33,11 +33,40 @@
 #include "../PrecompiledHeaders.h"
 #include "SharedMessageQueue.h"
 
+
+
+/**
+ * FIFO (queue):
+ * 
+ *            back                         front
+ *            +--+--+--+--+--+--+--+--+--+--+--+
+ * Enqueue -> |  |  |  |  |  |  |  |  |  |  |  |
+ *            |  |  |  |  |  |  |  |  |  |  |  | -> Dequeue
+ *            +--+--+--+--+--+--+--+--+--+--+--+
+ *                                            ^
+ *                                            |
+ *                                      Make room here
+ *
+ *
+ * LIFO (stack):
+ * 
+ *            back                         front
+ *            +--+--+--+--+--+--+--+--+--+--+--+
+ *            |  |  |  |  |  |  |  |  |  |  |  | <- Enqueue
+ *            |  |  |  |  |  |  |  |  |  |  |  | -> Dequeue
+ *            +--+--+--+--+--+--+--+--+--+--+--+
+ *              ^
+ *              |
+ *        Make room here
+ **/
+
+
 namespace Orthanc
 {
-  SharedMessageQueue::SharedMessageQueue(unsigned int maxSize)
+  SharedMessageQueue::SharedMessageQueue(unsigned int maxSize) :
+    isFifo_(true),
+    maxSize_(maxSize)
   {
-    maxSize_ = maxSize;
   }
 
 
@@ -56,12 +85,31 @@
 
     if (maxSize_ != 0 && queue_.size() > maxSize_)
     {
-      // Too many elements in the queue: First remove the oldest
-      delete queue_.front();
-      queue_.pop_front();
+      if (isFifo_)
+      {
+        // Too many elements in the queue: Make room
+        delete queue_.front();
+        queue_.pop_front();
+      }
+      else
+      {
+        // Too many elements in the stack: Make room
+        delete queue_.back();
+        queue_.pop_back();
+      }
     }
 
-    queue_.push_back(message);
+    if (isFifo_)
+    {
+      // Queue policy (FIFO)
+      queue_.push_back(message);
+    }
+    else
+    {
+      // Stack policy (LIFO)
+      queue_.push_front(message);
+    }
+
     elementAvailable_.notify_one();
   }
 
@@ -124,4 +172,17 @@
 
     return true;
   }
+
+
+  void SharedMessageQueue::SetFifoPolicy()
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+    isFifo_ = true;
+  }
+
+  void SharedMessageQueue::SetLifoPolicy()
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+    isFifo_ = false;
+  }
 }
--- a/Core/MultiThreading/SharedMessageQueue.h	Wed Nov 19 11:15:34 2014 +0100
+++ b/Core/MultiThreading/SharedMessageQueue.h	Thu Nov 20 11:58:47 2014 +0100
@@ -45,6 +45,7 @@
   private:
     typedef std::list<IDynamicObject*>  Queue;
 
+    bool isFifo_;
     unsigned int maxSize_;
     Queue queue_;
     boost::mutex mutex_;
@@ -63,5 +64,19 @@
     IDynamicObject* Dequeue(int32_t millisecondsTimeout);
 
     bool WaitEmpty(int32_t millisecondsTimeout);
+
+    bool IsFifoPolicy() const
+    {
+      return isFifo_;
+    }
+
+    bool IsLifoPolicy() const
+    {
+      return !isFifo_;
+    }
+
+    void SetFifoPolicy();
+
+    void SetLifoPolicy();
   };
 }