1
#if !defined(RESIP_FIFO_HXX)
5
#include "rutil/AbstractFifo.hxx"
6
#include "rutil/SelectInterruptor.hxx"
12
@brief A templated, threadsafe message-queue class.
14
template < class Msg >
15
class Fifo : public AbstractFifo<Msg*>
18
Fifo(AsyncProcessHandler* interruptor=0);
21
using AbstractFifo<Msg*>::mFifo;
22
using AbstractFifo<Msg*>::mMutex;
23
using AbstractFifo<Msg*>::mCondition;
24
using AbstractFifo<Msg*>::empty;
25
using AbstractFifo<Msg*>::size;
27
/// Add a message to the fifo.
30
typedef typename AbstractFifo<Msg*>::Messages Messages;
31
size_t addMultiple(Messages& msgs);
33
/** Returns the first message available. It will wait if no
34
* messages are available. If a signal interrupts the wait,
35
* it will retry the wait. Signals can therefore not be caught
36
* via getNext. If you need to detect a signal, use block
37
* prior to calling getNext.
42
/** Returns the next message available. Will wait up to
43
* ms milliseconds if no information is available. If
44
* the specified time passes or a signal interrupts the
45
* wait, this method returns 0. This interface provides
46
* no mechanism to distinguish between timeout and
51
void getMultiple(Messages& other, unsigned int max);
52
bool getMultiple(int ms, Messages& other, unsigned int max);
54
/// delete all elements in the queue
56
void setInterruptor(AsyncProcessHandler* interruptor);
59
AsyncProcessHandler* mInterruptor;
60
Fifo(const Fifo& rhs);
61
Fifo& operator=(const Fifo& rhs);
66
Fifo<Msg>::Fifo(AsyncProcessHandler* interruptor) :
68
mInterruptor(interruptor)
80
Fifo<Msg>::setInterruptor(AsyncProcessHandler* interruptor)
82
Lock lock(mMutex); (void)lock;
83
mInterruptor=interruptor;
91
Lock lock(mMutex); (void)lock;
92
while ( ! mFifo.empty() )
97
assert(mFifo.empty());
102
Fifo<Msg>::add(Msg* msg)
104
size_t size = AbstractFifo<Msg*>::add(msg);
105
if(size==1 && mInterruptor)
107
// Only do this when the queue goes from empty to not empty.
108
mInterruptor->handleProcessNotification();
115
Fifo<Msg>::addMultiple(Messages& msgs)
117
size_t inSize = msgs.size();
118
size_t size = AbstractFifo<Msg*>::addMultiple(msgs);
119
if(size==inSize && mInterruptor)
121
// Only do this when the queue goes from empty to not empty.
122
mInterruptor->handleProcessNotification();
129
Fifo<Msg> ::getNext()
131
return AbstractFifo<Msg*>::getNext();
136
Fifo<Msg> ::getNext(int ms)
139
AbstractFifo<Msg*>::getNext(ms, result);
145
Fifo<Msg>::getMultiple(Messages& other, unsigned int max)
147
AbstractFifo<Msg*>::getMultiple(other, max);
152
Fifo<Msg>::getMultiple(int ms, Messages& other, unsigned int max)
154
return AbstractFifo<Msg*>::getMultiple(ms, other, max);
160
/* ====================================================================
161
* The Vovida Software License, Version 1.0
163
* Redistribution and use in source and binary forms, with or without
164
* modification, are permitted provided that the following conditions
167
* 1. Redistributions of source code must retain the above copyright
168
* notice, this list of conditions and the following disclaimer.
170
* 2. Redistributions in binary form must reproduce the above copyright
171
* notice, this list of conditions and the following disclaimer in
172
* the documentation and/or other materials provided with the
175
* 3. The names "VOCAL", "Vovida Open Communication Application Library",
176
* and "Vovida Open Communication Application Library (VOCAL)" must
177
* not be used to endorse or promote products derived from this
178
* software without prior written permission. For written
179
* permission, please contact vocal@vovida.org.
181
* 4. Products derived from this software may not be called "VOCAL", nor
182
* may "VOCAL" appear in their name, without prior written
183
* permission of Vovida Networks, Inc.
185
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
186
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
187
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
188
* NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
189
* NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
190
* IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
191
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
192
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
193
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
194
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
195
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
196
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
199
* ====================================================================
201
* This software consists of voluntary contributions made by Vovida
202
* Networks, Inc. and many individuals on behalf of Vovida Networks,
203
* Inc. For more information on Vovida Networks, Inc., please see
204
* <http://www.vovida.org/>.