root/juggler/branches/1.0/test/Sync/testCond.cpp

Revision 8789, 5.8 kB (checked in by patrickh, 7 years ago)

Copyright update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998, 1999, 2000, 2001, 2002
4  *   by Iowa State University
5  *
6  * Original Authors:
7  *   Allen Bierbaum, Christopher Just,
8  *   Patrick Hartling, Kevin Meinert,
9  *   Carolina Cruz-Neira, Albert Baker
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the
23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24  * Boston, MA 02111-1307, USA.
25  *
26  * -----------------------------------------------------------------
27  * File:          $RCSfile$
28  * Date modified: $Date$
29  * Version:       $Revision$
30  * -----------------------------------------------------------------
31  *
32  *************** <auto-copyright.pl END do not edit this line> ***************/
33
34 #include <iostream>
35 #include <stdio.h>
36 #include <ulocks.h>
37 #include <math.h>
38 #include <time.h>
39 #include <sys/time.h>
40
41 #include <Threads/vjThread.h>
42 #include <Sync/vjCond.h>
43 #include <Kernel/vjDebug.h>
44
45 class SyncIncrementer
46 {
47 public:
48    SyncIncrementer() : value(0), go(false)
49       {;}
50    
51    int start();     // Start the thingie
52    void trigger();   // Trigger increment
53    void sync();      // Wait for completion
54    void main(void* nullParam);   // Main loop
55   
56    void incValue()
57       { value++;}
58    void decValue()
59       { value--;}
60    int getValue()
61       { return value; }
62
63 private:
64    vjCond syncCond;     // condition var
65    bool   go;
66    int    value;
67 };
68
69
70 // ------------------------------ //
71 // -----    MAIN   -------------- //
72 // ------------------------------ //
73 int main(void)
74 {
75    SyncIncrementer syncer;    // The test syncer
76   
77    // Spawn incrementer
78    syncer.start();
79
80    while (1)
81    {         
82       std::cerr << std::setw(5) << vjThread::self() << "P1: Before Trigger"
83                 << std::endl;
84
85          vjDEBUG(vjDBG_ALL, 0) << "main: trigger\n" << vjDEBUG_FLUSH;
86       syncer.trigger();    // Trigger the beginning of frame drawing
87          vjDEBUG(vjDBG_ALL, 3) << "main: trigger done\n" << vjDEBUG_FLUSH;
88
89       std::cerr << std::setw(5) << vjThread::self() << "P1: Between"
90                 << std::endl;
91
92          vjDEBUG(vjDBG_ALL, 0) << "main: sync up\n" << vjDEBUG_FLUSH;
93       syncer.sync();    // Block until drawing is done
94          vjDEBUG(vjDBG_ALL, 0) << "main: sync done\n" << vjDEBUG_FLUSH;
95      
96       syncer.decValue();
97       vjDEBUG(vjDBG_ALL, 0) << "VAL: " << syncer.getValue() << std::endl
98                             << vjDEBUG_FLUSH;
99    }
100
101    return 1;
102 }
103
104
105 /// -------------------- ///
106 /// ---- Members ------- ///
107 /// -------------------- ///
108     
109 //: Start the main function
110 int SyncIncrementer::start()
111 {
112    // Create a new thread to handle the control
113    vjThreadMemberFunctor<SyncIncrementer>* memberFunctor =
114    new vjThreadMemberFunctor<SyncIncrementer>(this, &SyncIncrementer::main, NULL);
115
116    vjThread* control_thread = new vjThread(memberFunctor, 0);
117
118    vjDEBUG(vjDBG_ALL, 0) << "SyncIncrementer::start: Just started main loop.  "
119                          << control_thread << std::endl << vjDEBUG_FLUSH;
120
121    return 1;
122 }
123
124
125 //: Trigger and increment
126 void SyncIncrementer::trigger()
127 {
128      // Allow the processes to draw
129    syncCond.acquire();        // Get exclusive access
130    {
131       std::cerr << std::setw(5) << vjThread::self() << "  P1: Signal"
132                 << std::endl;
133
134       go = true;          // Signal that rendering can happen
135       syncCond.signal();
136          vjDEBUG(vjDBG_ALL, 0) << "Trigger signaled\n" << vjDEBUG_FLUSH;
137          //syncCond.dump();
138    }
139    syncCond.release();
140 }
141    
142 //: Wait for completion
143 void SyncIncrementer::sync()
144 {
145    syncCond.acquire();
146    {   // Wait for triggerRender == false
147       while (go == true)
148       {
149          std::cerr << std::setw(5) << vjThread::self() << "  P1: Wait"
150                    << std::endl;
151          syncCond.wait();
152       }
153       /* Do nothing */
154          vjDEBUG(vjDBG_ALL, 0) << "Sync: Completed. trigger == false\n"
155                                << vjDEBUG_FLUSH;
156          //syncCond.dump();
157       std::cerr << std::setw(5) << vjThread::self() << "  P1: Exit wait"
158                 << std::endl;
159    }
160    syncCond.release();
161 }
162
163
164 //: This is the main loop that incs
165 void SyncIncrementer::main(void* nullParam) {
166    while (1)
167    {
168       syncCond.acquire();
169       {
170          vjDEBUG(vjDBG_ALL, 0) << "Wait for trigger\n" << vjDEBUG_FLUSH;
171          
172          // Wait for trigger == true
173          while (go == false)
174          {
175             std::cerr << std::setw(5) << vjThread::self() << "  P2: Wait"
176                       << std::endl;
177             syncCond.wait();
178          }
179          
180          std::cerr << std::setw(5) << vjThread::self() << "  P2: Wait done"
181                    << std::endl;
182          //syncCond.dump();
183          // THEN --- Do Work --- //
184          vjDEBUG(vjDBG_ALL, 0) << "Incrementing\n" << vjDEBUG_FLUSH;
185        
186          incValue();
187
188          vjDEBUG(vjDBG_ALL, 0) << "Var Incremented - Set trigger FALSE and SIGNAL\n"
189                                << vjDEBUG_FLUSH;
190
191          go = false;   // We are done rendering
192
193          std::cerr << std::setw(5) << vjThread::self() << "  P2: Signal"
194                    << std::endl;
195          syncCond.signal();
196
197          //syncCond.dump();
198       }
199       syncCond.release();
200
201       //cerr << "P2: Out of lock" << endl;
202    }
203 }
204
Note: See TracBrowser for help on using the browser.