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

« back to all changes in this revision

Viewing changes to rutil/FiniteFifo.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
#if !defined(RESIP_FiniteFifo_hxx)
 
2
#define RESIP_FiniteFifo_hxx 
 
3
 
 
4
#include "rutil/AbstractFifo.hxx"
 
5
 
 
6
// efficiency note: use a circular buffer do avoid list node allocation
 
7
 
 
8
// what happens to timers that can't be queued?
 
9
 
 
10
namespace resip
 
11
{
 
12
 
 
13
/**
 
14
   @brief A templated, threadsafe message-queue class with a fixed size.
 
15
   @ingroup message_passing
 
16
   @deprecated
 
17
   @todo remove class
 
18
*/
 
19
template < class Msg >
 
20
class FiniteFifo : public AbstractFifo<Msg*>
 
21
{
 
22
   public:
 
23
      FiniteFifo(unsigned int maxSize);
 
24
      virtual ~FiniteFifo();
 
25
 
 
26
      using AbstractFifo<Msg*>::mFifo;
 
27
      using AbstractFifo<Msg*>::mMutex;
 
28
      using AbstractFifo<Msg*>::mCondition;
 
29
      using AbstractFifo<Msg*>::empty;
 
30
      using AbstractFifo<Msg*>::size;
 
31
 
 
32
      // Add a message to the fifo.
 
33
      // return true if succeed, false if full
 
34
      bool add(Msg* msg);
 
35
 
 
36
      /** Returns the first message available. It will wait if no
 
37
       *  messages are available. If a signal interrupts the wait,
 
38
       *  it will retry the wait. Signals can therefore not be caught
 
39
       *  via getNext. If you need to detect a signal, use block
 
40
       *  prior to calling getNext.
 
41
       */
 
42
      Msg* getNext();
 
43
 
 
44
      /** Returns the next message available. Will wait up to
 
45
       *  ms milliseconds if no information is available. If
 
46
       *  the specified time passes or a signal interrupts the
 
47
       *  wait, this method returns 0. This interface provides
 
48
       *  no mechanism to distinguish between timeout and
 
49
       *  interrupt.
 
50
       */
 
51
      Msg* getNext(int ms);
 
52
   private:
 
53
      unsigned int mMaxSize;
 
54
};
 
55
 
 
56
template <class Msg>
 
57
FiniteFifo<Msg>::FiniteFifo(unsigned int maxSize)
 
58
   : AbstractFifo<Msg*>(),
 
59
   mMaxSize(maxSize)
 
60
{
 
61
}
 
62
 
 
63
template <class Msg>
 
64
FiniteFifo<Msg>::~FiniteFifo()
 
65
{
 
66
   Lock lock(mMutex); (void)lock;
 
67
   while ( ! mFifo.empty() )
 
68
   {
 
69
      delete mFifo.front();
 
70
      mFifo.pop_front();
 
71
   }
 
72
}
 
73
 
 
74
template <class Msg>
 
75
bool
 
76
FiniteFifo<Msg>::add(Msg* msg)
 
77
{
 
78
   Lock lock(mMutex); (void)lock;
 
79
   if (mFifo.size() >= mMaxSize)
 
80
   {
 
81
      return false;
 
82
   }
 
83
   else
 
84
   {
 
85
      mFifo.push_back(msg);
 
86
      mCondition.signal();
 
87
      return true;
 
88
   }
 
89
}
 
90
 
 
91
template <class Msg>
 
92
Msg*
 
93
FiniteFifo<Msg> ::getNext()
 
94
{
 
95
   return AbstractFifo<Msg*>::getNext();
 
96
}
 
97
 
 
98
template <class Msg>
 
99
Msg*
 
100
FiniteFifo<Msg> ::getNext(int ms)
 
101
{
 
102
   Msg* result(0);
 
103
   AbstractFifo<Msg*>::getNext(ms, result);
 
104
   return result;
 
105
}
 
106
 
 
107
} // namespace resip
 
108
 
 
109
#endif
 
110
 
 
111
/* ====================================================================
 
112
 * The Vovida Software License, Version 1.0 
 
113
 * 
 
114
 * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
 
115
 * 
 
116
 * Redistribution and use in source and binary forms, with or without
 
117
 * modification, are permitted provided that the following conditions
 
118
 * are met:
 
119
 * 
 
120
 * 1. Redistributions of source code must retain the above copyright
 
121
 *    notice, this list of conditions and the following disclaimer.
 
122
 * 
 
123
 * 2. Redistributions in binary form must reproduce the above copyright
 
124
 *    notice, this list of conditions and the following disclaimer in
 
125
 *    the documentation and/or other materials provided with the
 
126
 *    distribution.
 
127
 * 
 
128
 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
 
129
 *    and "Vovida Open Communication Application Library (VOCAL)" must
 
130
 *    not be used to endorse or promote products derived from this
 
131
 *    software without prior written permission. For written
 
132
 *    permission, please contact vocal@vovida.org.
 
133
 *
 
134
 * 4. Products derived from this software may not be called "VOCAL", nor
 
135
 *    may "VOCAL" appear in their name, without prior written
 
136
 *    permission of Vovida Networks, Inc.
 
137
 * 
 
138
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 
139
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 
140
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
 
141
 * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
 
142
 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
 
143
 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
 
144
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 
145
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 
146
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 
147
 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
148
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 
149
 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 
150
 * DAMAGE.
 
151
 * 
 
152
 * ====================================================================
 
153
 * 
 
154
 * This software consists of voluntary contributions made by Vovida
 
155
 * Networks, Inc. and many individuals on behalf of Vovida Networks,
 
156
 * Inc.  For more information on Vovida Networks, Inc., please see
 
157
 * <http://www.vovida.org/>.
 
158
 *
 
159
 */