/src/resiprocate/rutil/Fifo.hxx
Line | Count | Source (jump to first uncovered line) |
1 | | #if !defined(RESIP_FIFO_HXX) |
2 | | #define RESIP_FIFO_HXX |
3 | | |
4 | | #include "rutil/ResipAssert.h" |
5 | | #include "rutil/AbstractFifo.hxx" |
6 | | #include "rutil/SelectInterruptor.hxx" |
7 | | |
8 | | namespace resip |
9 | | { |
10 | | |
11 | | /** |
12 | | @brief A templated, threadsafe message-queue class. |
13 | | */ |
14 | | template < class Msg > |
15 | | class Fifo : public AbstractFifo<Msg*> |
16 | | { |
17 | | public: |
18 | | Fifo(AsyncProcessHandler* interruptor=0); |
19 | | virtual ~Fifo(); |
20 | | |
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; |
26 | | |
27 | | /// Add a message to the fifo. |
28 | | size_t add(Msg* msg); |
29 | | |
30 | | typedef typename AbstractFifo<Msg*>::Messages Messages; |
31 | | size_t addMultiple(Messages& msgs); |
32 | | |
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. |
38 | | */ |
39 | | Msg* getNext(); |
40 | | |
41 | | |
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 |
47 | | * interrupt. |
48 | | */ |
49 | | Msg* getNext(int ms); |
50 | | |
51 | | void getMultiple(Messages& other, unsigned int max); |
52 | | bool getMultiple(int ms, Messages& other, unsigned int max); |
53 | | |
54 | | /// delete all elements in the queue |
55 | | virtual void clear(); |
56 | | void setInterruptor(AsyncProcessHandler* interruptor); |
57 | | |
58 | | private: |
59 | | AsyncProcessHandler* mInterruptor; |
60 | | Fifo(const Fifo& rhs); |
61 | | Fifo& operator=(const Fifo& rhs); |
62 | | }; |
63 | | |
64 | | |
65 | | template <class Msg> |
66 | | Fifo<Msg>::Fifo(AsyncProcessHandler* interruptor) : |
67 | | AbstractFifo<Msg*>(), |
68 | | mInterruptor(interruptor) |
69 | | { |
70 | | } |
71 | | |
72 | | template <class Msg> |
73 | | Fifo<Msg>::~Fifo() |
74 | | { |
75 | | clear(); |
76 | | } |
77 | | |
78 | | template <class Msg> |
79 | | void |
80 | | Fifo<Msg>::setInterruptor(AsyncProcessHandler* interruptor) |
81 | | { |
82 | | Lock lock(mMutex); (void)lock; |
83 | | mInterruptor=interruptor; |
84 | | } |
85 | | |
86 | | |
87 | | template <class Msg> |
88 | | void |
89 | | Fifo<Msg>::clear() |
90 | | { |
91 | | Lock lock(mMutex); (void)lock; |
92 | | while ( ! mFifo.empty() ) |
93 | | { |
94 | | delete mFifo.front(); |
95 | | mFifo.pop_front(); |
96 | | } |
97 | | resip_assert(mFifo.empty()); |
98 | | } |
99 | | |
100 | | template <class Msg> |
101 | | size_t |
102 | | Fifo<Msg>::add(Msg* msg) |
103 | | { |
104 | | size_t size = AbstractFifo<Msg*>::add(msg); |
105 | | if(size==1 && mInterruptor) |
106 | | { |
107 | | // Only do this when the queue goes from empty to not empty. |
108 | | mInterruptor->handleProcessNotification(); |
109 | | } |
110 | | return size; |
111 | | } |
112 | | |
113 | | template <class Msg> |
114 | | size_t |
115 | | Fifo<Msg>::addMultiple(Messages& msgs) |
116 | 0 | { |
117 | 0 | size_t inSize = msgs.size(); |
118 | 0 | size_t size = AbstractFifo<Msg*>::addMultiple(msgs); |
119 | 0 | if(size==inSize && inSize != 0 && mInterruptor) |
120 | 0 | { |
121 | | // Only do this when the queue goes from empty to not empty. |
122 | 0 | mInterruptor->handleProcessNotification(); |
123 | 0 | } |
124 | 0 | return size; |
125 | 0 | } |
126 | | |
127 | | template <class Msg> |
128 | | Msg* |
129 | | Fifo<Msg> ::getNext() |
130 | | { |
131 | | return AbstractFifo<Msg*>::getNext(); |
132 | | } |
133 | | |
134 | | template <class Msg> |
135 | | Msg* |
136 | | Fifo<Msg> ::getNext(int ms) |
137 | | { |
138 | | Msg* result(0); |
139 | | AbstractFifo<Msg*>::getNext(ms, result); |
140 | | return result; |
141 | | } |
142 | | |
143 | | template <class Msg> |
144 | | void |
145 | | Fifo<Msg>::getMultiple(Messages& other, unsigned int max) |
146 | | { |
147 | | AbstractFifo<Msg*>::getMultiple(other, max); |
148 | | } |
149 | | |
150 | | template <class Msg> |
151 | | bool |
152 | | Fifo<Msg>::getMultiple(int ms, Messages& other, unsigned int max) |
153 | | { |
154 | | return AbstractFifo<Msg*>::getMultiple(ms, other, max); |
155 | | } |
156 | | } // namespace resip |
157 | | |
158 | | #endif |
159 | | |
160 | | /* ==================================================================== |
161 | | * The Vovida Software License, Version 1.0 |
162 | | * |
163 | | * Redistribution and use in source and binary forms, with or without |
164 | | * modification, are permitted provided that the following conditions |
165 | | * are met: |
166 | | * |
167 | | * 1. Redistributions of source code must retain the above copyright |
168 | | * notice, this list of conditions and the following disclaimer. |
169 | | * |
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 |
173 | | * distribution. |
174 | | * |
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. |
180 | | * |
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. |
184 | | * |
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 |
197 | | * DAMAGE. |
198 | | * |
199 | | * ==================================================================== |
200 | | * |
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/>. |
205 | | * |
206 | | */ |