X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fthreads%2FSGQueue.hxx;h=37122c5ae150be619b48a62e85cba2ad1a6393ea;hb=5af8bb7c8e259f5bdda67beb87adcc453e0b32e7;hp=9f534ec909a34e53b01e99c0e8f42b85fcaa0989;hpb=4e7fe460a5c5c1b64dd1d540bc197dbb89614b7f;p=simgear.git diff --git a/simgear/threads/SGQueue.hxx b/simgear/threads/SGQueue.hxx index 9f534ec9..37122c5a 100644 --- a/simgear/threads/SGQueue.hxx +++ b/simgear/threads/SGQueue.hxx @@ -3,15 +3,10 @@ #include -#if defined ( SG_HAVE_STD_INCLUDES ) -# include -#else -# include -#endif - +#include #include -#include "SGThread.hxx" #include "SGGuard.hxx" +#include "SGThread.hxx" /** * SGQueue defines an interface for a FIFO. @@ -70,7 +65,7 @@ public: protected: /** - * + * */ std::queue fifo; }; @@ -78,7 +73,7 @@ protected: /** * A simple thread safe queue. All access functions are guarded with a mutex. */ -template +template class SGLockedQueue : public SGQueue { public: @@ -99,7 +94,7 @@ public: * @return bool True if queue is empty, otherwisr false. */ virtual bool empty() { - SGGuard g(mutex); + SGGuard g(mutex); return this->fifo.empty(); } @@ -109,7 +104,7 @@ public: * @param T object to add. */ virtual void push( const T& item ) { - SGGuard g(mutex); + SGGuard g(mutex); this->fifo.push( item ); } @@ -119,7 +114,7 @@ public: * @return T next available object. */ virtual T front() { - SGGuard g(mutex); + SGGuard g(mutex); assert( ! this->fifo.empty() ); T item = this->fifo.front(); return item; @@ -131,7 +126,7 @@ public: * @return T next available object. */ virtual T pop() { - SGGuard g(mutex); + SGGuard g(mutex); //if (fifo.empty()) throw NoSuchElementException(); assert( ! this->fifo.empty() ); // if (fifo.empty()) @@ -150,7 +145,7 @@ public: * @return size_t size of queue. */ virtual size_t size() { - SGGuard g(mutex); + SGGuard g(mutex); return this->fifo.size(); } @@ -159,7 +154,7 @@ private: /** * Mutex to serialise access. */ - SGLOCK mutex; + SGMutex mutex; private: // Prevent copying. @@ -186,7 +181,7 @@ public: ~SGBlockingQueue() {} /** - * + * */ virtual bool empty() { SGGuard g(mutex); @@ -260,7 +255,7 @@ private: /** * Condition to signal when queue not empty. */ - SGPthreadCond not_empty; + SGWaitCondition not_empty; private: // Prevent copying. @@ -268,4 +263,148 @@ private: SGBlockingQueue& operator=( const SGBlockingQueue& ); }; + +/** + * A guarded deque blocks threads trying to retrieve items + * when none are available. + */ +template +class SGBlockingDeque +{ +public: + /** + * Create a new SGBlockingDequeue. + */ + SGBlockingDeque() {} + + /** + * Destroy this dequeue. + */ + ~SGBlockingDeque() {} + + /** + * + */ + virtual void clear() { + SGGuard g(mutex); + this->queue.clear(); + } + + /** + * + */ + virtual bool empty() { + SGGuard 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 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 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 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 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 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 g(mutex); + return this->queue.size(); + } + +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 queue; +}; + #endif // SGQUEUE_HXX_INCLUDED