~ubuntu-branches/ubuntu/saucy/resiprocate/saucy-proposed

« back to all changes in this revision

Viewing changes to resip/stack/ValueFifo.hxx

  • Committer: Package Import Robot
  • Author(s): Daniel Pocock
  • Date: 2012-05-17 19:29:59 UTC
  • Revision ID: package-import@ubuntu.com-20120517192959-vv00m77isztdy64q
Tags: upstream-1.8.2
ImportĀ upstreamĀ versionĀ 1.8.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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"
 
11
#include "rutil/AbstractFifo.hxx"
 
12
#include "resip/stack/CancelableTimerQueue.hxx"
 
13
 
 
14
namespace resip
 
15
{
 
16
 
 
17
/**
 
18
   @internal
 
19
 
 
20
   Distinct from resip::Fifo; by value and has cancellable timers.
 
21
*/
 
22
template <class T>
 
23
class ValueFifo : public resip::FifoStatsInterface
 
24
{
 
25
   public:
 
26
      typedef typename CancelableTimerQueue<T>::Id TimerId;
 
27
 
 
28
      ValueFifo(const Data& name) :
 
29
         myFifoSize(0), 
 
30
         myTimerSize(0) 
 
31
      {
 
32
      }
 
33
 
 
34
      ~ValueFifo() 
 
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
      
 
85
      T getNext()
 
86
      {
 
87
         resip::Lock lock(myMutex);
 
88
 
 
89
         while (!messageAvailableNoLock())
 
90
         {
 
91
            if (myTimerQueue.empty())
 
92
            {
 
93
               myCondition.wait(&myMutex);
 
94
            }
 
95
            else
 
96
            {
 
97
               myCondition.wait(&myMutex, myTimerQueue.getTimeout());
 
98
            }            
 
99
         }
 
100
 
 
101
         while (myTimerQueue.available())
 
102
         {
 
103
            myList.push_back(myTimerQueue.getNext());
 
104
            myFifoSize++;
 
105
            myTimerSize--;
 
106
         }
 
107
 
 
108
         assert (myFifoSize > 0);
 
109
         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
 
 
139
      bool messageAvailable()
 
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.signal();
 
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