wefts_queue.h

Go to the documentation of this file.
00001 /* 00002 wefts_queue.h 00003 Waitable template FIFO structure. 00004 00005 $Id: wefts_queue.h,v 1.6 2004/08/14 21:38:35 jonnymind Exp $ 00006 --------------------------------------------- 00007 Begin : 2003-08-03 17:50 00008 Author : Giancarlo Niccolai 00009 00010 Last modified because: 00011 00012 */ 00013 00014 /************************************************************************** 00015 * This program is free software; you can redistribute it and/or modify * 00016 * it under the terms of the GNU Library General Public License as * 00017 * published by the Free Software Foundation; either version 2.1 of the * 00018 * License, or (at your option) any later version. * 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 { 00059 private: 00060 void p_waitForFree() { 00061 while ( m_list.size() >= m_maxLen ) 00062 m_cond.wait(); 00063 } 00064 00065 void p_waitForFull() { 00066 while ( m_list.size() == 0 ) 00067 m_cond.wait(); 00068 } 00069 00070 protected: 00071 FastCondition m_cond; 00072 unsigned long m_maxLen; 00073 int m_size; 00074 std::list<T> m_list; 00075 00076 public: 00083 Queue( unsigned int maxLen = 0): 00084 m_size( 0 ), 00085 m_maxLen( 0 ) 00086 {} 00087 00099 bool push( T elem, const bool wait=false) 00100 { 00101 m_cond.lock(); 00102 if ( m_maxLen > 0 && m_size >= m_maxLen ) 00103 { 00104 if ( wait ) { 00105 // this is for compilers that canno expand loops inline 00106 p_waitForFree(); 00107 } 00108 else { 00109 m_cond.unlock(); 00110 return false; 00111 } 00112 } 00113 m_size++; 00114 m_list.push_back( elem ); 00115 m_cond.signal(); 00116 m_cond.unlock(); 00117 return true; 00118 } 00119 00120 00140 bool pop( T &elem, const bool wait=true) 00141 { 00142 m_cond.lock(); 00143 if ( m_size == 0 ) 00144 { 00145 if ( wait ) { 00146 // this is for compilers that canno expand loops inline 00147 p_waitForFull(); 00148 } 00149 else { 00150 m_cond.unlock(); 00151 return false; 00152 } 00153 } 00154 m_size --; 00155 elem = m_list.front(); 00156 m_list.pop_front(); 00157 m_cond.signal(); 00158 m_cond.unlock(); 00159 return true; 00160 } 00161 00170 bool empty() const 00171 { 00172 Queue<T> *Self = const_cast<Queue<T>*>(this); 00173 Self->m_cond.lock(); 00174 bool res = (m_size == 0); 00175 Self->m_cond.unlock(); 00176 return res; 00177 } 00178 00179 int size() const 00180 { 00181 Queue<T> *Self = const_cast<Queue<T>*>(this); 00182 Self->m_cond.lock(); 00183 int res = m_size; 00184 Self->m_cond.unlock(); 00185 return res; 00186 } 00187 }; 00188 00189 00197 /* 00198 template<class T> class Queue< T* >: private Queue< void*> 00199 { 00200 public: 00201 typedef Queue< void *> Base; 00202 00203 Queue( int maxsize=0 ): Base( maxsize ) {} 00204 00205 bool push( T* elem, const bool wait=false) 00206 { 00207 return Base::push( elem, wait ); 00208 } 00209 00210 bool pop( T* &elem, const bool wait = false ) 00211 { 00212 return Base::pop( elem, wait ); 00213 } 00214 00215 }; 00216 */ 00217 00218 } 00219 00220 #endif 00221 /* end of wefts_queue.h */

Generated on Tue Oct 5 14:57:00 2004 for Wefts by doxygen 1.3.7