/[resiprocate]/main/sip/resiprocate/test/testFifo.cxx
ViewVC logotype

Diff of /main/sip/resiprocate/test/testFifo.cxx

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3819 by dlb, Wed Oct 6 11:18:51 2004 UTC revision 3820 by bbramwel, Thu Jan 20 04:36:08 2005 UTC
# Line 4  Line 4 
4  #include "resiprocate/os/FiniteFifo.hxx"  #include "resiprocate/os/FiniteFifo.hxx"
5  #include "resiprocate/os/TimeLimitFifo.hxx"  #include "resiprocate/os/TimeLimitFifo.hxx"
6  #include "resiprocate/os/Data.hxx"  #include "resiprocate/os/Data.hxx"
7    #include "resiprocate/os/ThreadIf.hxx"
8  #include <unistd.h>  #include <unistd.h>
9    
10    // #define VERBOSE
11    
12  using namespace resip;  using namespace resip;
13  using namespace std;  using namespace std;
14    
# Line 19  Line 22 
22        Data mVal;        Data mVal;
23  };  };
24    
25    class Consumer: public ThreadIf
26    {
27      public:
28        Consumer(TimeLimitFifo<Foo>&);
29        ~Consumer() {};
30    
31        void thread();
32    
33      private:
34        TimeLimitFifo<Foo>& mFifo;
35    };
36    
37    class Producer: public ThreadIf
38    {
39      public:
40        Producer(TimeLimitFifo<Foo>&);
41        ~Producer() {};
42    
43        void thread();
44    
45      private:
46        TimeLimitFifo<Foo>& mFifo;
47    };
48    
49    Consumer::Consumer(TimeLimitFifo<Foo>& f) :
50        mFifo(f)
51    {}
52    
53    void Consumer::thread()
54    {
55        static unsigned wakeups[6] = { 1, 2, 3, 0, 1, 3 };
56        unsigned int w = 0;
57    
58        while(!mShutdown) {
59            if (mFifo.messageAvailable())
60                mFifo.getNext();
61            else
62            {
63                unsigned wakeup = wakeups[w];
64                w = (w + 1) % 6;
65    #ifdef VERBOSE
66                cerr << "Consumer sleeping for " << wakeup << " seconds with mSize " << mFifo.size() << endl;
67    #endif
68                if (wakeup > 0)
69                    sleep(wakeup);
70            }
71        }
72    }
73    
74    Producer::Producer(TimeLimitFifo<Foo>& f) :
75        mFifo(f)
76    {}
77    
78    void Producer::thread()
79    {
80        static unsigned wakeups[6] = { 0, 1, 0, 2, 3, 1 };
81        unsigned int w = 0;
82    
83        for (unsigned long n = 0; n < 0x1ffff; n++) {
84            if (mFifo.wouldAccept(TimeLimitFifo<Foo>::EnforceTimeDepth))
85                mFifo.add(new Foo(Data(n)), TimeLimitFifo<Foo>::EnforceTimeDepth);
86            else
87            {
88                unsigned wakeup = wakeups[w];
89                w = (w + 1) % 6;
90    #ifdef VERBOSE
91                cerr << "Producer sleeping for " << wakeup << " seconds at " << n << " with mSize " << mFifo.size() << endl;
92    #endif
93                if (wakeup > 0)
94                    sleep(wakeup);
95            }
96        }
97    }
98    
99  bool  bool
100  isNear(int value, int reference, int epsilon=250)  isNear(int value, int reference, int epsilon=250)
101  {  {
# Line 47  Line 124 
124        
125        assert(!tlf.empty());        assert(!tlf.empty());
126        assert(tlf.size() == 1);        assert(tlf.size() == 1);
127    #ifdef VERBOSE
128        cerr << tlf.timeDepth() << endl;        cerr << tlf.timeDepth() << endl;
129    #endif
130        assert(tlf.timeDepth() == 0);        assert(tlf.timeDepth() == 0);
131    
132        sleep(2);        sleep(2);
# Line 80  Line 159 
159        delete fp;        delete fp;
160        c = tlf.add(new Foo("third"), TimeLimitFifo<Foo>::EnforceTimeDepth);        c = tlf.add(new Foo("third"), TimeLimitFifo<Foo>::EnforceTimeDepth);
161        assert(c);        assert(c);
   
       while (!tlf.empty())  
       {  
          delete tlf.getNext();  
       }  
162     }     }
163    
164     {     {
# Line 108  Line 182 
182        delete fp;        delete fp;
183        c = tlfNS.add(new Foo("third"), TimeLimitFifo<Foo>::EnforceTimeDepth);        c = tlfNS.add(new Foo("third"), TimeLimitFifo<Foo>::EnforceTimeDepth);
184        assert(c);        assert(c);
   
       while (!tlfNS.empty())  
       {  
          delete tlfNS.getNext();  
       }  
185     }     }
186    
187     {     {
# Line 145  Line 214 
214    
215        c = tlfNS.add(new Foo("first"), TimeLimitFifo<Foo>::EnforceTimeDepth);        c = tlfNS.add(new Foo("first"), TimeLimitFifo<Foo>::EnforceTimeDepth);
216        assert(c);        assert(c);
   
       while (!tlfNS.empty())  
       {  
          delete tlfNS.getNext();  
       }  
217     }     }
218    
219     {     {
# Line 187  Line 251 
251    
252        c = tlfNS.add(new Foo("first"), TimeLimitFifo<Foo>::EnforceTimeDepth);        c = tlfNS.add(new Foo("first"), TimeLimitFifo<Foo>::EnforceTimeDepth);
253        assert(c);        assert(c);
   
       while (!tlfNS.empty())  
       {  
          delete tlfNS.getNext();  
       }  
254     }     }
255    
256     {     {
# Line 209  Line 268 
268           assert(c);           assert(c);
269           sleep(1);           sleep(1);
270        }        }
271       }
272    
       while (!tlfNS.empty())  
273        {        {
274           delete tlfNS.getNext();         TimeLimitFifo<Foo> tlfNS(20, 5000);
275        }         Producer prod(tlfNS);
276           Consumer cons(tlfNS);
277    
278           cons.run();
279           prod.run();
280    #ifdef VERBOSE
281           cerr << "Producer and consumer threads are running" << endl;
282    #endif
283           prod.join();
284    #ifdef VERBOSE
285           cerr << "Producer thread finished" << endl;
286    #endif
287           cons.shutdown();
288           cons.join();
289    #ifdef VERBOSE
290           cerr << "Consumer thread finished" << endl;
291    #endif
292     }     }
     
293     cerr << "All OK" << endl;     cerr << "All OK" << endl;
294     return 0;     return 0;
295  }  }

Legend:
Removed from v.3819  
changed lines
  Added in v.3820

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27