reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
ValueFifo.hxx
Go to the documentation of this file.
1#ifndef ValueFifo_hxx
2#define ValueFifo_hxx
3
4#include <cerrno>
5#include <iosfwd>
6#include <deque>
7
8#include "rutil/Condition.hxx"
9#include "rutil/Mutex.hxx"
10#include "rutil/Lock.hxx"
13
14namespace resip
15{
16
22template <class T>
24{
25 public:
27
28 ValueFifo(const Data& name) :
29 myFifoSize(0),
30 myTimerSize(0)
31 {
32 }
33
35 {
36 }
37
38 void add(const T& t)
39 {
40 resip::Lock lock(myMutex);
41 myList.push_back(t);
42 myFifoSize++;
43 wakeup();
44 }
45
46 TimerId addDelayMs(const T& t, int offsetInMs)
47 {
48 resip::Lock lock(myMutex);
49 if (offsetInMs < 0)
50 {
51 offsetInMs = 0;
52 }
53
54 bool doWakeup = false;
55 if (myTimerQueue.empty() ||
56 offsetInMs < myTimerQueue.getTimeout())
57 {
58 doWakeup = true;
59 }
60
61 TimerId id = myTimerQueue.addRelative(t, offsetInMs);
62 myTimerSize++;
63
64 //wakeup if new timer is sooner than next timer that would have
65 //fired, or no timer set
66 if (doWakeup)
67 {
68
69 wakeup();
70 }
71 return id;
72 }
73
74 bool cancel(TimerId id)
75 {
76 resip::Lock lock(myMutex);
77 if (myTimerQueue.cancel(id))
78 {
79 myTimerSize--;
80 return true;
81 }
82 return false;
83 }
84
86 {
87 resip::Lock lock(myMutex);
88
89 while (!messageAvailableNoLock())
90 {
91 if (myTimerQueue.empty())
92 {
93 myCondition.wait(lock);
94 }
95 else
96 {
97 myCondition.wait_for(lock, std::chrono::milliseconds(myTimerQueue.getTimeout()));
98 }
99 }
100
101 while (myTimerQueue.available())
102 {
103 myList.push_back(myTimerQueue.getNext());
104 myFifoSize++;
105 myTimerSize--;
106 }
107
108 resip_assert (myFifoSize > 0);
109 resip_assert (!myList.empty());
110
111 T firstMessage = myList.front();
112
113 myList.pop_front(); //dcm -- should do this with a guard to avoid extra copy
114 myFifoSize--;
115 return firstMessage;
116 }
117
118 void clear()
119 {
120 resip::Lock lock(myMutex);
121 myFifoSize = 0;
122 myTimerSize = 0;
123 myTimerQueue.clear();
124 myList.clear();
125 }
126
127 //size includes timer events
128 unsigned int size() const
129 {
130 resip::Lock lock(myMutex);
131 return myFifoSize + myTimerSize;
132 }
133
134 bool empty() const
135 {
136 return myFifoSize + myTimerSize == 0;
137 }
138
140 {
141 resip::Lock lock(myMutex);
142 return messageAvailableNoLock();
143 }
144
145 virtual size_t getCountDepth() const
146 {
147 return size();
148 }
149
150 virtual time_t getTimeDepth() const
151 {
152 return 0;
153 }
154
155 virtual time_t expectedWaitTimeMilliSec() const
156 {
157 return 0;
158 }
159
160 virtual time_t averageServiceTimeMicroSec() const
161 {
162 return 1;
163 }
164
165 private:
166 bool messageAvailableNoLock()
167 {
168 return myFifoSize > 0 || myTimerQueue.available();
169 }
170
171 void wakeup()
172 {
173 myCondition.notify_one();
174 }
175
176 std::deque<T> myList;
177
178 CancelableTimerQueue<T> myTimerQueue;
179
180 unsigned long myFifoSize;
181 unsigned long myTimerSize;
182
183 mutable resip::Mutex myMutex;
184 resip::Condition myCondition;
185
186 // no value semantics
187 ValueFifo(const ValueFifo&);
188 ValueFifo& operator=(const ValueFifo&);
189};
190
191}
192#endif
193
194/* ====================================================================
195 * The Vovida Software License, Version 1.0
196 *
197 * Copyright (c) 2004 PurpleComm, Inc. All rights reserved.
198 *
199 * Redistribution and use in source and binary forms, with or without
200 * modification, are permitted provided that the following conditions
201 * are met:
202 *
203 * 1. Redistributions of source code must retain the above copyright
204 * notice, this list of conditions and the following disclaimer.
205 *
206 * 2. Redistributions in binary form must reproduce the above copyright
207 * notice, this list of conditions and the following disclaimer in
208 * the documentation and/or other materials provided with the
209 * distribution.
210 *
211 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
212 * and "Vovida Open Communication Application Library (VOCAL)" must
213 * not be used to endorse or promote products derived from this
214 * software without prior written permission. For written
215 * permission, please contact vocal@vovida.org.
216 *
217 * 4. Products derived from this software may not be called "VOCAL", nor
218 * may "VOCAL" appear in their name, without prior written
219 * permission of Vovida Networks, Inc.
220 *
221 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
222 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
223 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
224 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
225 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
226 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
227 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
228 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
229 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
230 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
231 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
232 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
233 * DAMAGE.
234 *
235 */
236
#define resip_assert(x)
This file needs to work for both C and C++ code.
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
Interface for providing metrics on FIFOs, primarily used by CongestionManager.
CancelableTimerQueue< T >::Id TimerId
Definition ValueFifo.hxx:26
virtual time_t getTimeDepth() const
Returns the difference in time between the youngest and oldest item in the FIFO in seconds.
ValueFifo(const Data &name)
Definition ValueFifo.hxx:28
unsigned int size() const
bool cancel(TimerId id)
Definition ValueFifo.hxx:74
virtual time_t expectedWaitTimeMilliSec() const
Returns the expected time it will take to service all messages currently in the queue (in milli-secon...
bool empty() const
bool messageAvailable()
void add(const T &t)
Definition ValueFifo.hxx:38
virtual time_t averageServiceTimeMicroSec() const
Returns the average time it takes for individual messages to be serviced (in micro-seconds)
TimerId addDelayMs(const T &t, int offsetInMs)
Definition ValueFifo.hxx:46
virtual size_t getCountDepth() const
Returns the number of elements in the FIFO.
dcm! – add UnusedChecking(_enum) below;
std::mutex Mutex
A semaphore that can be locked by only one thread at a time.
Definition Mutex.hxx:37
std::unique_lock< Mutex > Lock
A convenience class to lock a Lockable (such as a Mutex) on object creation, and unlock on destructio...
Definition Lock.hxx:28
std::condition_variable Condition
A condition variable that can be signaled or waited on, wraps POSIX/Windows implementations depending...
Definition Condition.hxx:51