]> git.mxchange.org Git - simgear.git/blobdiff - simgear/threads/SGQueue.hxx
Fix #1783: repeated error message on console
[simgear.git] / simgear / threads / SGQueue.hxx
index e7311e56d9f33c7130ecc32e1ae5609f9be33c3e..41c2cf306147eb512c0614907c2d86fb3c9c1ab3 100644 (file)
@@ -5,9 +5,8 @@
 
 #include <cassert>
 #include <queue>
-#include <OpenThreads/Mutex>
-#include <OpenThreads/ScopedLock>
-#include <OpenThreads/Condition>
+#include "SGGuard.hxx"
+#include "SGThread.hxx"
 
 /**
  * SGQueue defines an interface for a FIFO.
@@ -39,21 +38,21 @@ public:
     /**
      * Add an item to the end of the queue.
      *
-     * @param T object to add.
+     * @param item  object to add.
      */
     virtual void push( const T& item ) = 0;
 
     /**
      * View the item from the head of the queue.
      *
-     * @return T next available object.
+     * @return The next available object.
      */
     virtual T front() = 0;
 
     /**
      * Get an item from the head of the queue.
      *
-     * @return T next available object.
+     * @return The next available object.
      */
     virtual T pop() = 0;
 
@@ -66,7 +65,7 @@ public:
 
 protected:
     /**
-     * 
+     *
      */
     std::queue<T> fifo;
 };
@@ -74,7 +73,7 @@ protected:
 /**
  * A simple thread safe queue.  All access functions are guarded with a mutex.
  */
-template<class T, class SGLOCK=OpenThreads::Mutex>
+template<class T>
 class SGLockedQueue : public SGQueue<T>
 {
 public:
@@ -87,35 +86,35 @@ public:
     /**
      * Destroy this object.
      */
-    ~SGLockedQueue() {}
+    virtual ~SGLockedQueue() {}
 
     /**
      * Returns whether this queue is empty (contains no elements).
      *
-     * @return bool True if queue is empty, otherwisr false.
+     * @return True if queue is empty, otherwise false.
      */
     virtual bool empty() {
-       OpenThreads::ScopedLock<SGLOCK> g(mutex);
+       SGGuard<SGMutex> g(mutex);
        return this->fifo.empty();
     }
 
     /**
      * Add an item to the end of the queue.
      *
-     * @param T object to add.
+     * @param item object to add.
      */
     virtual void push( const T& item ) {
-       OpenThreads::ScopedLock<SGLOCK> g(mutex);
+       SGGuard<SGMutex> g(mutex);
        this->fifo.push( item );
     }
 
     /**
      * View the item from the head of the queue.
      *
-     * @return T next available object.
+     * @return The next available object.
      */
     virtual T front() {
-       OpenThreads::ScopedLock<SGLOCK> g(mutex);
+       SGGuard<SGMutex> g(mutex);
        assert( ! this->fifo.empty() );
        T item = this->fifo.front();
        return item;
@@ -124,12 +123,12 @@ public:
     /**
      * Get an item from the head of the queue.
      *
-     * @return T next available object.
+     * @return The next available object.
      */
     virtual T pop() {
-       OpenThreads::ScopedLock<SGLOCK> g(mutex);
-       //if (fifo.empty()) throw NoSuchElementException();
-       assert( ! this->fifo.empty() );
+       SGGuard<SGMutex> g(mutex);
+           if (this->fifo.empty()) return T(); // assumes T is default constructable
+        
 //     if (fifo.empty())
 //     {
 //         mutex.unlock();
@@ -143,10 +142,10 @@ public:
     /**
      * Query the size of the queue
      *
-     * @return size_t size of queue.
+     * @return Size of queue.
      */
     virtual size_t size() {
-       OpenThreads::ScopedLock<SGLOCK> g(mutex);
+       SGGuard<SGMutex> g(mutex);
         return this->fifo.size();
     }
 
@@ -155,7 +154,7 @@ private:
     /**
      * Mutex to serialise access.
      */
-    SGLOCK mutex;
+    SGMutex mutex;
 
 private:
     // Prevent copying.
@@ -179,23 +178,23 @@ public:
     /**
      * Destroy this queue.
      */
-    ~SGBlockingQueue() {}
+    virtual ~SGBlockingQueue() {}
 
     /**
-     * 
+     *
      */
     virtual bool empty() {
-       OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+       SGGuard<SGMutex> g(mutex);
        return this->fifo.empty();
     }
 
     /**
      * Add an item to the end of the queue.
      *
-     * @param T object to add.
+     * @param item The object to add.
      */
     virtual void push( const T& item ) {
-       OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+       SGGuard<SGMutex> g(mutex);
        this->fifo.push( item );
        not_empty.signal();
     }
@@ -204,10 +203,10 @@ public:
      * View the item from the head of the queue.
      * Calling thread is not suspended
      *
-     * @return T next available object.
+     * @return The next available object.
      */
     virtual T front() {
-       OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+       SGGuard<SGMutex> g(mutex);
 
        assert(this->fifo.empty() != true);
        //if (fifo.empty()) throw ??
@@ -220,13 +219,13 @@ public:
      * Get an item from the head of the queue.
      * If no items are available then the calling thread is suspended
      *
-     * @return T next available object.
+     * @return The next available object.
      */
     virtual T pop() {
-       OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+       SGGuard<SGMutex> g(mutex);
 
        while (this->fifo.empty())
-           not_empty.wait(&mutex);
+           not_empty.wait(mutex);
 
        assert(this->fifo.empty() != true);
        //if (fifo.empty()) throw ??
@@ -239,10 +238,10 @@ public:
     /**
      * Query the size of the queue
      *
-     * @return size_t size of queue.
+     * @return Size of queue.
      */
     virtual size_t size() {
-       OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+       SGGuard<SGMutex> g(mutex);
         return this->fifo.size();
     }
 
@@ -251,12 +250,12 @@ private:
     /**
      * Mutex to serialise access.
      */
-    OpenThreads::Mutex mutex;
+    SGMutex mutex;
 
     /**
      * Condition to signal when queue not empty.
      */
-    OpenThreads::Condition not_empty;
+    SGWaitCondition not_empty;
 
 private:
     // Prevent copying.
@@ -281,23 +280,31 @@ public:
     /**
      * Destroy this dequeue.
      */
-    ~SGBlockingDeque() {}
+    virtual ~SGBlockingDeque() {}
 
     /**
-     * 
+     *
+     */
+    virtual void clear() {
+    SGGuard<SGMutex> g(mutex);
+    this->queue.clear();
+    }
+
+    /**
+     *
      */
     virtual bool empty() {
-    OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+    SGGuard<SGMutex> g(mutex);
     return this->queue.empty();
     }
 
     /**
      * Add an item to the front of the queue.
      *
-     * @param T object to add.
+     * @param item The object to add.
      */
     virtual void push_front( const T& item ) {
-    OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+    SGGuard<SGMutex> g(mutex);
     this->queue.push_front( item );
     not_empty.signal();
     }
@@ -305,10 +312,10 @@ public:
     /**
      * Add an item to the back of the queue.
      *
-     * @param T object to add.
+     * @param item The object to add.
      */
     virtual void push_back( const T& item ) {
-    OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+    SGGuard<SGMutex> g(mutex);
     this->queue.push_back( item );
     not_empty.signal();
     }
@@ -317,10 +324,10 @@ public:
      * View the item from the head of the queue.
      * Calling thread is not suspended
      *
-     * @return T next available object.
+     * @return The next available object.
      */
     virtual T front() {
-    OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+    SGGuard<SGMutex> g(mutex);
 
     assert(this->queue.empty() != true);
     //if (queue.empty()) throw ??
@@ -333,13 +340,13 @@ public:
      * Get an item from the head of the queue.
      * If no items are available then the calling thread is suspended
      *
-     * @return T next available object.
+     * @return The next available object.
      */
     virtual T pop_front() {
-    OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+    SGGuard<SGMutex> g(mutex);
 
     while (this->queue.empty())
-        not_empty.wait(&mutex);
+        not_empty.wait(mutex);
 
     assert(this->queue.empty() != true);
     //if (queue.empty()) throw ??
@@ -353,13 +360,13 @@ public:
      * Get an item from the tail of the queue.
      * If no items are available then the calling thread is suspended
      *
-     * @return T next available object.
+     * @return The next available object.
      */
     virtual T pop_back() {
-    OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+    SGGuard<SGMutex> g(mutex);
 
     while (this->queue.empty())
-        not_empty.wait(&mutex);
+        not_empty.wait(mutex);
 
     assert(this->queue.empty() != true);
     //if (queue.empty()) throw ??
@@ -372,24 +379,29 @@ public:
     /**
      * Query the size of the queue
      *
-     * @return size_t size of queue.
+     * @return Size of queue.
      */
     virtual size_t size() {
-    OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+    SGGuard<SGMutex> g(mutex);
         return this->queue.size();
     }
 
+    void waitOnNotEmpty() {
+       SGGuard<SGMutex> g(mutex);
+       while (this->queue.empty())
+           not_empty.wait(mutex);
+    }
 private:
 
     /**
      * Mutex to serialise access.
      */
-    OpenThreads::Mutex mutex;
+    SGMutex mutex;
 
     /**
      * Condition to signal when queue not empty.
      */
-    OpenThreads::Condition not_empty;
+    SGWaitCondition not_empty;
 
 private:
     // Prevent copying.