X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fthreads%2FSGQueue.hxx;h=80877375e96fa391c57c449e54aa1d670d73ad4e;hb=da07871bc60569a02c1dd12aee754d5c85a55738;hp=9d4dcf31259840442a6f697c23aacd730b52956f;hpb=79d72b6292858b957d706171939136e627b29773;p=simgear.git diff --git a/simgear/threads/SGQueue.hxx b/simgear/threads/SGQueue.hxx index 9d4dcf31..80877375 100644 --- a/simgear/threads/SGQueue.hxx +++ b/simgear/threads/SGQueue.hxx @@ -3,15 +3,11 @@ #include -#if defined ( SG_HAVE_STD_INCLUDES ) -# include -#else -# include -#endif - +#include #include -#include "SGThread.hxx" -#include "SGGuard.hxx" +#include +#include +#include /** * 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 +template class SGLockedQueue : public SGQueue { public: @@ -92,8 +95,8 @@ public: * @return bool True if queue is empty, otherwisr false. */ virtual bool empty() { - SGGuard g(mutex); - return fifo.empty(); + OpenThreads::ScopedLock g(mutex); + return this->fifo.empty(); } /** @@ -102,8 +105,8 @@ public: * @param T object to add. */ virtual void push( const T& item ) { - SGGuard g(mutex); - fifo.push( item ); + OpenThreads::ScopedLock g(mutex); + this->fifo.push( item ); } /** @@ -112,9 +115,9 @@ public: * @return T next available object. */ virtual T front() { - SGGuard g(mutex); - assert( ! fifo.empty() ); - T item = fifo.front(); + OpenThreads::ScopedLock g(mutex); + assert( ! this->fifo.empty() ); + T item = this->fifo.front(); return item; } @@ -124,18 +127,29 @@ public: * @return T next available object. */ virtual T pop() { - SGGuard g(mutex); + OpenThreads::ScopedLock 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 g(mutex); + return this->fifo.size(); + } + private: /** @@ -165,14 +179,14 @@ public: /** * Destroy this queue. */ - ~SGBlockingQueue() { mutex.unlock(); } + ~SGBlockingQueue() {} /** * */ virtual bool empty() { - SGGuard g(mutex); - return fifo.empty(); + OpenThreads::ScopedLock g(mutex); + return this->fifo.empty(); } /** @@ -181,8 +195,8 @@ public: * @param T object to add. */ virtual void push( const T& item ) { - SGGuard g(mutex); - fifo.push( item ); + OpenThreads::ScopedLock g(mutex); + this->fifo.push( item ); not_empty.signal(); } @@ -193,12 +207,12 @@ public: * @return T next available object. */ virtual T front() { - SGGuard g(mutex); + OpenThreads::ScopedLock g(mutex); - assert(fifo.empty() != true); + assert(this->fifo.empty() != true); //if (fifo.empty()) throw ?? - T item = fifo.front(); + T item = this->fifo.front(); return item; } @@ -209,30 +223,40 @@ public: * @return T next available object. */ virtual T pop() { - SGGuard g(mutex); + OpenThreads::ScopedLock 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 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.