|
reSIProcate/rutil
9694
|
00001 #if !defined(RESIP_FIFO_HXX) 00002 #define RESIP_FIFO_HXX 00003 00004 #include <cassert> 00005 #include "rutil/AbstractFifo.hxx" 00006 #include "rutil/SelectInterruptor.hxx" 00007 00008 namespace resip 00009 { 00010 00014 template < class Msg > 00015 class Fifo : public AbstractFifo<Msg*> 00016 { 00017 public: 00018 Fifo(AsyncProcessHandler* interruptor=0); 00019 virtual ~Fifo(); 00020 00021 using AbstractFifo<Msg*>::mFifo; 00022 using AbstractFifo<Msg*>::mMutex; 00023 using AbstractFifo<Msg*>::mCondition; 00024 using AbstractFifo<Msg*>::empty; 00025 using AbstractFifo<Msg*>::size; 00026 00028 size_t add(Msg* msg); 00029 00030 typedef typename AbstractFifo<Msg*>::Messages Messages; 00031 size_t addMultiple(Messages& msgs); 00032 00039 Msg* getNext(); 00040 00041 00049 Msg* getNext(int ms); 00050 00051 void getMultiple(Messages& other, unsigned int max); 00052 bool getMultiple(int ms, Messages& other, unsigned int max); 00053 00055 virtual void clear(); 00056 void setInterruptor(AsyncProcessHandler* interruptor); 00057 00058 private: 00059 AsyncProcessHandler* mInterruptor; 00060 Fifo(const Fifo& rhs); 00061 Fifo& operator=(const Fifo& rhs); 00062 }; 00063 00064 00065 template <class Msg> 00066 Fifo<Msg>::Fifo(AsyncProcessHandler* interruptor) : 00067 AbstractFifo<Msg*>(), 00068 mInterruptor(interruptor) 00069 { 00070 } 00071 00072 template <class Msg> 00073 Fifo<Msg>::~Fifo() 00074 { 00075 clear(); 00076 } 00077 00078 template <class Msg> 00079 void 00080 Fifo<Msg>::setInterruptor(AsyncProcessHandler* interruptor) 00081 { 00082 Lock lock(mMutex); (void)lock; 00083 mInterruptor=interruptor; 00084 } 00085 00086 00087 template <class Msg> 00088 void 00089 Fifo<Msg>::clear() 00090 { 00091 Lock lock(mMutex); (void)lock; 00092 while ( ! mFifo.empty() ) 00093 { 00094 delete mFifo.front(); 00095 mFifo.pop_front(); 00096 } 00097 assert(mFifo.empty()); 00098 } 00099 00100 template <class Msg> 00101 size_t 00102 Fifo<Msg>::add(Msg* msg) 00103 { 00104 size_t size = AbstractFifo<Msg*>::add(msg); 00105 if(size==1 && mInterruptor) 00106 { 00107 // Only do this when the queue goes from empty to not empty. 00108 mInterruptor->handleProcessNotification(); 00109 } 00110 return size; 00111 } 00112 00113 template <class Msg> 00114 size_t 00115 Fifo<Msg>::addMultiple(Messages& msgs) 00116 { 00117 size_t inSize = msgs.size(); 00118 size_t size = AbstractFifo<Msg*>::addMultiple(msgs); 00119 if(size==inSize && mInterruptor) 00120 { 00121 // Only do this when the queue goes from empty to not empty. 00122 mInterruptor->handleProcessNotification(); 00123 } 00124 return size; 00125 } 00126 00127 template <class Msg> 00128 Msg* 00129 Fifo<Msg> ::getNext() 00130 { 00131 return AbstractFifo<Msg*>::getNext(); 00132 } 00133 00134 template <class Msg> 00135 Msg* 00136 Fifo<Msg> ::getNext(int ms) 00137 { 00138 Msg* result(0); 00139 AbstractFifo<Msg*>::getNext(ms, result); 00140 return result; 00141 } 00142 00143 template <class Msg> 00144 void 00145 Fifo<Msg>::getMultiple(Messages& other, unsigned int max) 00146 { 00147 AbstractFifo<Msg*>::getMultiple(other, max); 00148 } 00149 00150 template <class Msg> 00151 bool 00152 Fifo<Msg>::getMultiple(int ms, Messages& other, unsigned int max) 00153 { 00154 return AbstractFifo<Msg*>::getMultiple(ms, other, max); 00155 } 00156 } // namespace resip 00157 00158 #endif 00159 00160 /* ==================================================================== 00161 * The Vovida Software License, Version 1.0 00162 * 00163 * Redistribution and use in source and binary forms, with or without 00164 * modification, are permitted provided that the following conditions 00165 * are met: 00166 * 00167 * 1. Redistributions of source code must retain the above copyright 00168 * notice, this list of conditions and the following disclaimer. 00169 * 00170 * 2. Redistributions in binary form must reproduce the above copyright 00171 * notice, this list of conditions and the following disclaimer in 00172 * the documentation and/or other materials provided with the 00173 * distribution. 00174 * 00175 * 3. The names "VOCAL", "Vovida Open Communication Application Library", 00176 * and "Vovida Open Communication Application Library (VOCAL)" must 00177 * not be used to endorse or promote products derived from this 00178 * software without prior written permission. For written 00179 * permission, please contact vocal@vovida.org. 00180 * 00181 * 4. Products derived from this software may not be called "VOCAL", nor 00182 * may "VOCAL" appear in their name, without prior written 00183 * permission of Vovida Networks, Inc. 00184 * 00185 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED 00186 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00187 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND 00188 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA 00189 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES 00190 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, 00191 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00192 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00193 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00194 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00195 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 00196 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 00197 * DAMAGE. 00198 * 00199 * ==================================================================== 00200 * 00201 * This software consists of voluntary contributions made by Vovida 00202 * Networks, Inc. and many individuals on behalf of Vovida Networks, 00203 * Inc. For more information on Vovida Networks, Inc., please see 00204 * <http://www.vovida.org/>. 00205 * 00206 */
1.7.5.1