Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

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.3 2004/03/08 20:28:53 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 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             // this is for compilers that canno expand loops inline
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 
00116 
00136    bool pop( T &elem, const bool wait=true)
00137    {
00138       m_cond.lock();
00139       if ( m_list.size() == 0 )
00140       {
00141          if ( wait ) {
00142             // this is for compilers that canno expand loops inline
00143             p_waitForFull();
00144          }
00145          else {
00146             m_cond.unlock();
00147             return false;
00148          }
00149       }
00150       elem = m_list.front();
00151       m_list.pop_front();
00152       m_cond.signal();
00153       m_cond.unlock();
00154       return true;
00155    }
00156 
00157 };
00158 
00159 }
00160 
00161 #endif
00162 /* end of wefts_queue.h */

Generated on Sat Mar 13 21:05:43 2004 for Wefts by doxygen 1.3.5