#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 "SGThread.hxx"
/**
* SGQueue defines an interface for a FIFO.
protected:
/**
- *
+ *
*/
std::queue<T> fifo;
};
/**
* A simple thread safe queue. All access functions are guarded with a mutex.
*/
-template<class T, class SGLOCK=SGMutex>
+template<class T>
class SGLockedQueue : public SGQueue<T>
{
public:
* @return bool True if queue is empty, otherwisr false.
*/
virtual bool empty() {
- SGGuard<SGLOCK> g(mutex);
+ SGGuard<SGMutex> g(mutex);
return this->fifo.empty();
}
* @param T object to add.
*/
virtual void push( const T& item ) {
- SGGuard<SGLOCK> g(mutex);
+ SGGuard<SGMutex> g(mutex);
this->fifo.push( item );
}
* @return T next available object.
*/
virtual T front() {
- SGGuard<SGLOCK> g(mutex);
+ SGGuard<SGMutex> g(mutex);
assert( ! this->fifo.empty() );
T item = this->fifo.front();
return item;
* @return T next available object.
*/
virtual T pop() {
- SGGuard<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();
* @return size_t size of queue.
*/
virtual size_t size() {
- SGGuard<SGLOCK> g(mutex);
+ SGGuard<SGMutex> g(mutex);
return this->fifo.size();
}
/**
* Mutex to serialise access.
*/
- SGLOCK mutex;
+ SGMutex mutex;
private:
// Prevent copying.
/**
* Destroy this queue.
*/
- ~SGBlockingQueue() { mutex.unlock(); }
+ ~SGBlockingQueue() {}
/**
- *
+ *
*/
virtual bool empty() {
SGGuard<SGMutex> g(mutex);
/**
* Condition to signal when queue not empty.
*/
- SGPthreadCond not_empty;
+ SGWaitCondition not_empty;
private:
// Prevent copying.
SGBlockingQueue& operator=( const SGBlockingQueue& );
};
+
+/**
+ * A guarded deque blocks threads trying to retrieve items
+ * when none are available.
+ */
+template<class T>
+class SGBlockingDeque
+{
+public:
+ /**
+ * Create a new SGBlockingDequeue.
+ */
+ SGBlockingDeque() {}
+
+ /**
+ * Destroy this dequeue.
+ */
+ ~SGBlockingDeque() {}
+
+ /**
+ *
+ */
+ virtual void clear() {
+ SGGuard<SGMutex> g(mutex);
+ this->queue.clear();
+ }
+
+ /**
+ *
+ */
+ virtual bool empty() {
+ SGGuard<SGMutex> g(mutex);
+ return this->queue.empty();
+ }
+
+ /**
+ * Add an item to the front of the queue.
+ *
+ * @param T object to add.
+ */
+ virtual void push_front( const T& item ) {
+ SGGuard<SGMutex> g(mutex);
+ this->queue.push_front( item );
+ not_empty.signal();
+ }
+
+ /**
+ * Add an item to the back of the queue.
+ *
+ * @param T object to add.
+ */
+ virtual void push_back( const T& item ) {
+ SGGuard<SGMutex> g(mutex);
+ this->queue.push_back( 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() {
+ SGGuard<SGMutex> g(mutex);
+
+ assert(this->queue.empty() != true);
+ //if (queue.empty()) throw ??
+
+ T item = this->queue.front();
+ return item;
+ }
+
+ /**
+ * 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.
+ */
+ virtual T pop_front() {
+ SGGuard<SGMutex> g(mutex);
+
+ while (this->queue.empty())
+ not_empty.wait(mutex);
+
+ assert(this->queue.empty() != true);
+ //if (queue.empty()) throw ??
+
+ T item = this->queue.front();
+ this->queue.pop_front();
+ return item;
+ }
+
+ /**
+ * 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.
+ */
+ virtual T pop_back() {
+ SGGuard<SGMutex> g(mutex);
+
+ while (this->queue.empty())
+ not_empty.wait(mutex);
+
+ assert(this->queue.empty() != true);
+ //if (queue.empty()) throw ??
+
+ T item = this->queue.back();
+ this->queue.pop_back();
+ return item;
+ }
+
+ /**
+ * Query the size of the queue
+ *
+ * @return size_t size of queue.
+ */
+ virtual size_t size() {
+ 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.
+ */
+ SGMutex mutex;
+
+ /**
+ * Condition to signal when queue not empty.
+ */
+ SGWaitCondition not_empty;
+
+private:
+ // Prevent copying.
+ SGBlockingDeque( const SGBlockingDeque& );
+ SGBlockingDeque& operator=( const SGBlockingDeque& );
+
+protected:
+ std::deque<T> queue;
+};
+
#endif // SGQUEUE_HXX_INCLUDED