head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.35; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.50; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.53.58; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.54; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# SGQueue.hxx

    00001 #ifndef SGQUEUE_HXX_INCLUDED
    00002 #define SGQUEUE_HXX_INCLUDED 1
    00003
    00004 #include <simgear/compiler.h>
    00005
    00006 #if defined ( SG_HAVE_STD_INCLUDES )
    00007 #  include <cassert>
    00008 #else
    00009 #  include <assert.h>
    00010 #endif
    00011
    00012 #include <queue>
    00013 #include "SGThread.hxx"
    00014 #include "SGGuard.hxx"
    00015
    00021 template<class T>
    00022 class SGQueue
    00023 {
    00024 public:
    00025
    00029     SGQueue() {}
    00030
    00034     virtual ~SGQueue() {}
    00035
    00041     virtual bool empty() = 0;
    00042
    00048     virtual void push( const T& item ) = 0;
    00049
    00055     virtual T pop() = 0;
    00056
    00057 protected:
    00061     std::queue<T> fifo;
    00062 };
    00063
    00067 template<class T, class SGLOCK=SGMutex>
    00068 class SGLockedQueue : public SGQueue<T>
    00069 {
    00070 public:
    00071
    00075     SGLockedQueue() {}
    00076
    00080     ~SGLockedQueue() {}
    00081
    00087     virtual bool empty() {
    00088         SGGuard<SGLOCK> g(mutex);
    00089         return fifo.empty();
    00090     }
    00091
    00097     virtual void push( const T& item ) {
    00098         SGGuard<SGLOCK> g(mutex);
    00099         fifo.push( item );
    00100     }
    00101
    00107     virtual T pop() {
    00108         SGGuard<SGLOCK> g(mutex);
    00109         //if (fifo.empty()) throw NoSuchElementException();
    00110         assert( ! fifo.empty() );
    00111 //      if (fifo.empty())
    00112 //      {
    00113 //          mutex.unlock();
    00114 //          pthread_exit( PTHREAD_CANCELED );
    00115 //      }
    00116         T item = fifo.front();
    00117         fifo.pop();
    00118         return item;
    00119     }
    00120 private:
    00121
    00125     SGLOCK mutex;
    00126
    00127 private:
    00128     // Prevent copying.
    00129     SGLockedQueue(const SGLockedQueue&);
    00130     SGLockedQueue& operator= (const SGLockedQueue&);
    00131 };
    00132
    00137 template<class T>
    00138 class SGBlockingQueue : public SGQueue<T>
    00139 {
    00140 public:
    00144     SGBlockingQueue() {}
    00145
    00149     ~SGBlockingQueue() { mutex.unlock(); }
    00150
    00154     virtual bool empty() {
    00155         SGGuard<SGMutex> g(mutex);
    00156         return fifo.empty();
    00157     }
    00158
    00164     virtual void push( const T& item ) {
    00165         SGGuard<SGMutex> g(mutex);
    00166         fifo.push( item );
    00167         not_empty.signal();
    00168     }
    00169
    00176     virtual T pop() {
    00177         SGGuard<SGMutex> g(mutex);
    00178
    00179         while (fifo.empty())
    00180             not_empty.wait(mutex);
    00181
    00182         assert(fifo.empty() != true);
    00183         //if (fifo.empty()) throw ??
    00184
    00185         T item = fifo.front();
    00186         fifo.pop();
    00187         return item;
    00188     }
    00189
    00190 private:
    00191
    00195     SGMutex mutex;
    00196
    00200     SGCondition not_empty;
    00201
    00202 private:
    00203     // Prevent copying.
    00204     SGBlockingQueue( const SGBlockingQueue& );
    00205     SGBlockingQueue& operator=( const SGBlockingQueue& );
    00206 };
    00207
    00208 #endif // SGQUEUE_HXX_INCLUDED

@ 1.3 log @Oops, try again. @ text @d131 1 a131 1