/src/resiprocate/rutil/ProducerFifoBuffer.hxx
Line | Count | Source |
1 | | #ifndef ProducerFifoBuffer_Include_Guard |
2 | | #define ProducerFifoBuffer_Include_Guard |
3 | | |
4 | | #include "rutil/Fifo.hxx" |
5 | | |
6 | | namespace resip |
7 | | { |
8 | | |
9 | | /** |
10 | | Class for buffering messages placed in a Fifo, to be used by a single |
11 | | producer. By adding messages in bulk, lock contention is reduced. |
12 | | @todo Refactor so we can use this with AbstractFifo<T>? This is presently not |
13 | | the case because AbstractFifo does not expose its API publicly. |
14 | | */ |
15 | | template<typename T> |
16 | | class ProducerFifoBuffer |
17 | | { |
18 | | public: |
19 | | ProducerFifoBuffer(Fifo<T>& fifo, |
20 | | size_t bufferSize) : |
21 | 0 | mFifo(fifo), |
22 | 0 | mBufferSize(bufferSize) |
23 | 0 | {} |
24 | | |
25 | | ~ProducerFifoBuffer() |
26 | 0 | { |
27 | 0 | flush(); |
28 | 0 | } |
29 | | |
30 | | void add(T* msg) |
31 | 0 | { |
32 | 0 | mBuffer.push_back(msg); |
33 | 0 | if(mBuffer.size()>=mBufferSize) |
34 | 0 | { |
35 | 0 | flush(); |
36 | 0 | } |
37 | 0 | } |
38 | | |
39 | | void flush() |
40 | 0 | { |
41 | 0 | if(mBuffer.size() > 0) |
42 | 0 | { |
43 | 0 | mFifo.addMultiple(mBuffer); |
44 | 0 | } |
45 | 0 | } |
46 | | |
47 | | inline size_t getBufferSize() const |
48 | | {return mBufferSize;} |
49 | | |
50 | | inline void setBufferSize(size_t pBufferSize) |
51 | | { |
52 | | mBufferSize = pBufferSize; |
53 | | if(mBuffer.size()>=mBufferSize) |
54 | | { |
55 | | flush(); |
56 | | } |
57 | | } |
58 | | |
59 | 0 | inline const Fifo<T>& getFifo() const {return mFifo;} |
60 | | inline Fifo<T>& getFifo() {return mFifo;} |
61 | | |
62 | | protected: |
63 | | Fifo<T>& mFifo; |
64 | | // The type used by AbstractFifo<T>::addMultiple() |
65 | | typename Fifo<T>::Messages mBuffer; |
66 | | size_t mBufferSize; |
67 | | }; |
68 | | } |
69 | | #endif |
70 | | |
71 | | |
72 | | /* ==================================================================== |
73 | | * The Vovida Software License, Version 1.0 |
74 | | * |
75 | | * Redistribution and use in source and binary forms, with or without |
76 | | * modification, are permitted provided that the following conditions |
77 | | * are met: |
78 | | * |
79 | | * 1. Redistributions of source code must retain the above copyright |
80 | | * notice, this list of conditions and the following disclaimer. |
81 | | * |
82 | | * 2. Redistributions in binary form must reproduce the above copyright |
83 | | * notice, this list of conditions and the following disclaimer in |
84 | | * the documentation and/or other materials provided with the |
85 | | * distribution. |
86 | | * |
87 | | * 3. The names "VOCAL", "Vovida Open Communication Application Library", |
88 | | * and "Vovida Open Communication Application Library (VOCAL)" must |
89 | | * not be used to endorse or promote products derived from this |
90 | | * software without prior written permission. For written |
91 | | * permission, please contact vocal@vovida.org. |
92 | | * |
93 | | * 4. Products derived from this software may not be called "VOCAL", nor |
94 | | * may "VOCAL" appear in their name, without prior written |
95 | | * permission of Vovida Networks, Inc. |
96 | | * |
97 | | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED |
98 | | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
99 | | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND |
100 | | * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA |
101 | | * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES |
102 | | * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, |
103 | | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
104 | | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
105 | | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
106 | | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
107 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
108 | | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
109 | | * DAMAGE. |
110 | | * |
111 | | * ==================================================================== |
112 | | * |
113 | | * This software consists of voluntary contributions made by Vovida |
114 | | * Networks, Inc. and many individuals on behalf of Vovida Networks, |
115 | | * Inc. For more information on Vovida Networks, Inc., please see |
116 | | * <http://www.vovida.org/>. |
117 | | * |
118 | | */ |