]> git.mxchange.org Git - simgear.git/blobdiff - simgear/threads/SGQueue.hxx
Geoff McLane:
[simgear.git] / simgear / threads / SGQueue.hxx
index 83a1cfebecdf2352117984b8af7bda77eb5280df..80877375e96fa391c57c449e54aa1d670d73ad4e 100644 (file)
@@ -3,15 +3,11 @@
 
 #include <simgear/compiler.h>
 
-#if defined ( SG_HAVE_STD_INCLUDES )
-#  include <cassert>
-#else
-#  include <assert.h>
-#endif
-
+#include <cassert>
 #include <queue>
-#include "SGThread.hxx"
-#include "SGGuard.hxx"
+#include <OpenThreads/Mutex>
+#include <OpenThreads/ScopedLock>
+#include <OpenThreads/Condition>
 
 /**
  * SGQueue defines an interface for a FIFO.
@@ -61,6 +57,13 @@ public:
      */
     virtual T pop() = 0;
 
+    /**
+     * Query the size of the queue
+     *
+     * @return size_t size of queue.
+     */
+    virtual size_t size() = 0;
+
 protected:
     /**
      * 
@@ -71,7 +74,7 @@ protected:
 /**
  * A simple thread safe queue.  All access functions are guarded with a mutex.
  */
-template<class T, class SGLOCK=SGMutex>
+template<class T, class SGLOCK=OpenThreads::Mutex>
 class SGLockedQueue : public SGQueue<T>
 {
 public:
@@ -92,7 +95,7 @@ public:
      * @return bool True if queue is empty, otherwisr false.
      */
     virtual bool empty() {
-       SGGuard<SGLOCK> g(mutex);
+       OpenThreads::ScopedLock<SGLOCK> g(mutex);
        return this->fifo.empty();
     }
 
@@ -102,7 +105,7 @@ public:
      * @param T object to add.
      */
     virtual void push( const T& item ) {
-       SGGuard<SGLOCK> g(mutex);
+       OpenThreads::ScopedLock<SGLOCK> g(mutex);
        this->fifo.push( item );
     }
 
@@ -112,7 +115,7 @@ public:
      * @return T next available object.
      */
     virtual T front() {
-       SGGuard<SGLOCK> g(mutex);
+       OpenThreads::ScopedLock<SGLOCK> g(mutex);
        assert( ! this->fifo.empty() );
        T item = this->fifo.front();
        return item;
@@ -124,7 +127,7 @@ public:
      * @return T next available object.
      */
     virtual T pop() {
-       SGGuard<SGLOCK> g(mutex);
+       OpenThreads::ScopedLock<SGLOCK> g(mutex);
        //if (fifo.empty()) throw NoSuchElementException();
        assert( ! this->fifo.empty() );
 //     if (fifo.empty())
@@ -136,6 +139,17 @@ public:
        this->fifo.pop();
        return item;
     }
+
+    /**
+     * Query the size of the queue
+     *
+     * @return size_t size of queue.
+     */
+    virtual size_t size() {
+       OpenThreads::ScopedLock<SGLOCK> g(mutex);
+        return this->fifo.size();
+    }
+
 private:
 
     /**
@@ -165,13 +179,13 @@ public:
     /**
      * Destroy this queue.
      */
-    ~SGBlockingQueue() { mutex.unlock(); }
+    ~SGBlockingQueue() {}
 
     /**
      * 
      */
     virtual bool empty() {
-       SGGuard<SGMutex> g(mutex);
+       OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
        return this->fifo.empty();
     }
 
@@ -181,7 +195,7 @@ public:
      * @param T object to add.
      */
     virtual void push( const T& item ) {
-       SGGuard<SGMutex> g(mutex);
+       OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
        this->fifo.push( item );
        not_empty.signal();
     }
@@ -193,7 +207,7 @@ public:
      * @return T next available object.
      */
     virtual T front() {
-       SGGuard<SGMutex> g(mutex);
+       OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
 
        assert(this->fifo.empty() != true);
        //if (fifo.empty()) throw ??
@@ -209,10 +223,10 @@ public:
      * @return T next available object.
      */
     virtual T pop() {
-       SGGuard<SGMutex> g(mutex);
+       OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
 
        while (this->fifo.empty())
-           not_empty.wait(mutex);
+           not_empty.wait(&mutex);
 
        assert(this->fifo.empty() != true);
        //if (fifo.empty()) throw ??
@@ -222,17 +236,27 @@ public:
        return item;
     }
 
+    /**
+     * Query the size of the queue
+     *
+     * @return size_t size of queue.
+     */
+    virtual size_t size() {
+       OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+        return this->fifo.size();
+    }
+
 private:
 
     /**
      * Mutex to serialise access.
      */
-    SGMutex mutex;
+    OpenThreads::Mutex mutex;
 
     /**
      * Condition to signal when queue not empty.
      */
-    SGPthreadCond not_empty;
+    OpenThreads::Condition not_empty;
 
 private:
     // Prevent copying.