00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef WT_QUEUE_H
00023 #define WT_QUEUE_H
00024
00025 #include <wefts_fastcond.h>
00026 #include <list>
00027
00028 namespace Wefts {
00029
00056 template <class T>
00057 class Queue {
00058 private:
00059 void p_waitForFree() {
00060 while ( m_list.size() >= m_maxLen )
00061 m_cond.wait();
00062 }
00063
00064 void p_waitForFull() {
00065 while ( m_list.size() == 0 )
00066 m_cond.wait();
00067 }
00068
00069 protected:
00070 FastCondition m_cond;
00071 unsigned long m_maxLen;
00072 std::list<T> m_list;
00073
00074 public:
00081 Queue( unsigned int maxLen = 0) {
00082 m_maxLen = maxLen;
00083 }
00084
00096 bool push( T elem, const bool wait=false)
00097 {
00098 m_cond.lock();
00099 if ( m_maxLen > 0 && m_list.size() >= m_maxLen )
00100 {
00101 if ( wait ) {
00102
00103 p_waitForFree();
00104 }
00105 else {
00106 m_cond.unlock();
00107 return false;
00108 }
00109 }
00110 m_list.push_back( elem );
00111 m_cond.signal();
00112 m_cond.unlock();
00113 return true;
00114 }
00115
00135 bool pop( T &elem, const bool wait=true)
00136 {
00137 m_cond.lock();
00138 if ( m_list.size() == 0 )
00139 {
00140 if ( wait ) {
00141
00142 p_waitForFull();
00143 }
00144 else {
00145 m_cond.unlock();
00146 return false;
00147 }
00148 }
00149 elem = m_list.front();
00150 m_list.pop_front();
00151 m_cond.signal();
00152 m_cond.unlock();
00153 return true;
00154 }
00155
00156 };
00157
00158 }
00159
00160 #endif
00161