Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound 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.1 2003/12/22 01:07:04 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 
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             // this is for compilers that canno expand loops inline
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 /* end of wefts_queue.h */

Generated on Mon Dec 22 04:12:30 2003 for Wefts by doxygen1.2.18