00001 #ifndef PCBuffer_CPP
00002 #define PCBuffer_CPP
00003
00004 #include <math.h>
00005 #include <iostream.h>
00006
00007 #include "PCBuffer.hpp"
00008 #include "Basic_PThread.cpp"
00009 #include "Mutex.cpp"
00010 #include "RWLock.cpp"
00011 #include "Semaphore.cpp"
00012 #include "BoundedBuffer.cpp"
00013 #include "Timer.cpp"
00014 #include "TimerPulse.cpp"
00015
00016 #define ADD ( 0.500000000 )
00017 #define WAITING ( 500000000 )
00018 #define BILLION ( 1000000000 )
00019
00020 #define RANDOM 250
00021 #define TSEC 0
00022
00023 #define TT Timer
00024
00025 class Timing : public TT {
00026 private:
00027 ULONG res;
00028 double current;
00029 double add;
00030 public:
00031
00032 Timing( ULONG clk = 0 )
00033 : TT( clk, TSEC ), current( 0.0f )
00034 {
00035 res = clk;
00036 }
00037
00038 virtual void start()
00039 {
00040 fixprio( 30 );
00041 register double r = (double)res * 1000 / BILLION + TSEC;
00042 QBColor();
00043 printf( "\nTimer started with a resolution of %6.2f ms.", r );
00044 printf( "\nEach clock timer ticks are represented by '#'\n" );
00045 fflush( stdout );
00046
00047
00048 TT::start();
00049 }
00050
00051 virtual void timedRun( int signo )
00052 {
00053 current += ADD;
00054 printf( "#" );
00055 }
00056
00057 inline double getTime() { return current; }
00058
00059 };
00060
00061 Timing timer( WAITING );
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 Producer::Producer( int n, int i )
00080 : Basic_PThread(), item( i ), name( n ),
00081 started( 0 ), elapsed( 0.0 ), total( 0.0 ) { }
00082
00083 void Producer::run()
00084 {
00085 QBColor();
00086 printf( "\nProducer %u started. ", name );
00087 fixprio( 8 );
00088 sched_yield();
00089
00090 for(;;)
00091 {
00092 if ( ++item > STEPS ) break;
00093
00094 register size_t where = item % BBSIZE;
00095
00096 slot[ where ]->P();
00097 sender.P();
00098
00099 started = timer.getCycles();
00100
00101 boundedBuffer.enqueue( item );
00102
00103 QBColor();
00104 printf(
00105 "\nProducer %u produced component %2u in slot %2u. ",
00106 name, item, where
00107 );
00108
00109 elapsed = timer.getElapsed( started );
00110 total += elapsed;
00111
00112 elapsed *= 1000;
00113 printf( "\nProducer %u took %4.2f ms. ", name, elapsed );
00114 fflush( stdout );
00115
00116 sender.V();
00117 message.V();
00118
00119 }
00120
00121 QBColor( 12 );
00122 printf( "\nProducer %u terminates. Total time spent %6.2f ms. \n", name, total*1000 );
00123 QBColor();
00124 fflush( stdout );
00125 ++done;
00126 }
00127
00128 Consumer::Consumer( int n )
00129 : Basic_PThread(), name( n ), slotno( 0 ),
00130 started( 0 ), elapsed( 0.0 ), total( 0.0 ) { }
00131
00132 void Consumer::run()
00133 {
00134 printf( "\nConsumer %u started. ", name );
00135 fixprio( 8 );
00136 sched_yield();
00137
00138 while( item < STEPS )
00139 {
00140 message.P();
00141
00142 if ( item > STEPS ) break;
00143
00144 receiver.P();
00145
00146 started = timer.getCycles();
00147
00148 ++item;
00149
00150 char msg = boundedBuffer.dequeue();
00151
00152 register size_t where = item % BBSIZE;
00153
00154 printf(
00155 "\nConsumer %u took message %2u from slot %2u in step %2u. ",
00156 name, msg, where, item
00157 );
00158 fflush( stdout );
00159
00160 elapsed = timer.getElapsed( started );
00161 total += elapsed;
00162
00163 elapsed *= 1000;
00164 printf( "\nConsumer %u took %4.2f ms. ", name, elapsed );
00165 fflush( stdout );
00166
00167 slot[ where ]->V();
00168 receiver.V();
00169 sched_yield();
00170
00171 }
00172
00173 QBColor( 12 );
00174 printf( "\nConsumer %u terminates. Total time spent %6.2f ms. \n", name, total*1000 );
00175 QBColor();
00176 fflush( stdout );
00177
00178 if ( item == STEPS ) {
00179 ++item;
00180 message.V();
00181 }
00182
00183 ++done;
00184 }
00185
00186
00187 int main() {
00188 NoColor = 1;
00189
00190 slot = new Semaphore*[ BBSIZE+1 ];
00191
00192 for( int k = 0; k <= BBSIZE; k++ )
00193 {
00194 char buf[80];
00195 sprintf( buf, "slot%d", k );
00196
00197 int c = (9+k)%15;
00198
00199 if ( c < 8 ) c += 10;
00200 slot[k] = new Semaphore( 1, buf, c );
00201 }
00202
00203 fixprio( 10 );
00204
00205 Producer p1( 1 );
00206 Consumer c1( 1 );
00207 Consumer c2( 2 );
00208
00209 p1.start( 10 );
00210 c1.start( 10 );
00211 c2.start( 10 );
00212
00213 timer.start();
00214 _uint64 prgStart = timer.getCycles();
00215
00216 p1.join();
00217 c1.join();
00218 c2.join();
00219
00220 fixprio( 6 );
00221 sched_yield();
00222
00223 int i = 0;
00224 while( done < 3 )
00225 {
00226 if ( ++i > 10 ) exit( 0 );
00227 printf( "M " );
00228 sched_yield();
00229 }
00230
00231 QBColor( 14 );
00232 printf( "Total program took %6.2f ms. Last time the timer was triggered %6.2f ms \n",
00233 (double)timer.getElapsed( prgStart ) * 1000, timer.getTime() * 1000 );
00234
00235 QBColor();
00236 printf( "\n\nDone!\n");
00237
00238 delete slot;
00239
00240 exit( 0 );
00241 return 0;
00242 }
00243
00244 #endif