]> git.mxchange.org Git - simgear.git/blobdiff - simgear/threads/SGQueue.hxx
Merge branch 'mathias/intersect'
[simgear.git] / simgear / threads / SGQueue.hxx
index 25fa2ca5b1db56d603be94c1362b2f3726354279..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.
@@ -47,6 +43,13 @@ public:
      */
     virtual void push( const T& item ) = 0;
 
+    /**
+     * View the item from the head of the queue.
+     *
+     * @return T next available object.
+     */
+    virtual T front() = 0;
+
     /**
      * Get an item from the head of the queue.
      *
@@ -54,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:
     /**
      * 
@@ -64,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:
@@ -85,8 +95,8 @@ public:
      * @return bool True if queue is empty, otherwisr false.
      */
     virtual bool empty() {
-       SGGuard<SGLOCK> g(mutex);
-       return fifo.empty();
+       OpenThreads::ScopedLock<SGLOCK> g(mutex);
+       return this->fifo.empty();
     }
 
     /**
@@ -95,8 +105,20 @@ public:
      * @param T object to add.
      */
     virtual void push( const T& item ) {
-       SGGuard<SGLOCK> g(mutex);
-       fifo.push( item );
+       OpenThreads::ScopedLock<SGLOCK> g(mutex);
+       this->fifo.push( item );
+    }
+
+    /**
+     * View the item from the head of the queue.
+     *
+     * @return T next available object.
+     */
+    virtual T front() {
+       OpenThreads::ScopedLock<SGLOCK> g(mutex);
+       assert( ! this->fifo.empty() );
+       T item = this->fifo.front();
+       return item;
     }
 
     /**
@@ -105,18 +127,29 @@ public:
      * @return T next available object.
      */
     virtual T pop() {
-       SGGuard<SGLOCK> g(mutex);
+       OpenThreads::ScopedLock<SGLOCK> g(mutex);
        //if (fifo.empty()) throw NoSuchElementException();
-       assert( ! fifo.empty() );
+       assert( ! this->fifo.empty() );
 //     if (fifo.empty())
 //     {
 //         mutex.unlock();
 //         pthread_exit( PTHREAD_CANCELED );
 //     }
-       T item = fifo.front();
-       fifo.pop();
+       T item = this->fifo.front();
+       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:
 
     /**
@@ -146,14 +179,14 @@ public:
     /**
      * Destroy this queue.
      */
-    ~SGBlockingQueue() { mutex.unlock(); }
+    ~SGBlockingQueue() {}
 
     /**
      * 
      */
     virtual bool empty() {
-       SGGuard<SGMutex> g(mutex);
-       return fifo.empty();
+       OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+       return this->fifo.empty();
     }
 
     /**
@@ -162,11 +195,27 @@ public:
      * @param T object to add.
      */
     virtual void push( const T& item ) {
-       SGGuard<SGMutex> g(mutex);
-       fifo.push( item );
+       OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+       this->fifo.push( item );
        not_empty.signal();
     }
 
+    /**
+     * View the item from the head of the queue.
+     * Calling thread is not suspended
+     *
+     * @return T next available object.
+     */
+    virtual T front() {
+       OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
+
+       assert(this->fifo.empty() != true);
+       //if (fifo.empty()) throw ??
+
+       T item = this->fifo.front();
+       return item;
+    }
+
     /**
      * Get an item from the head of the queue.
      * If no items are available then the calling thread is suspended
@@ -174,30 +223,40 @@ public:
      * @return T next available object.
      */
     virtual T pop() {
-       SGGuard<SGMutex> g(mutex);
+       OpenThreads::ScopedLock<OpenThreads::Mutex> g(mutex);
 
-       while (fifo.empty())
-           not_empty.wait(mutex);
+       while (this->fifo.empty())
+           not_empty.wait(&mutex);
 
-       assert(fifo.empty() != true);
+       assert(this->fifo.empty() != true);
        //if (fifo.empty()) throw ??
 
-       T item = fifo.front();
-       fifo.pop();
+       T item = this->fifo.front();
+       this->fifo.pop();
        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.