| 1 | //----------------------------------------------------------------------------------------
 | 
|---|
| 2 | //----------------------------------------------------------------------------------------
 | 
|---|
| 3 | //
 | 
|---|
| 4 | //              DEPRECATED TEST
 | 
|---|
| 5 | //
 | 
|---|
| 6 | //----------------------------------------------------------------------------------------
 | 
|---|
| 7 | //----------------------------------------------------------------------------------------
 | 
|---|
| 8 | 
 | 
|---|
| 9 | #include <fstream>
 | 
|---|
| 10 | #include <kernel>
 | 
|---|
| 11 | #include <monitor>
 | 
|---|
| 12 | #include <stdlib>
 | 
|---|
| 13 | #include <thread>
 | 
|---|
| 14 | 
 | 
|---|
| 15 | static const unsigned long N = 50_000ul;
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #ifndef PREEMPTION_RATE
 | 
|---|
| 18 | #define PREEMPTION_RATE 10_000ul
 | 
|---|
| 19 | #endif
 | 
|---|
| 20 | 
 | 
|---|
| 21 | unsigned int default_preemption() {
 | 
|---|
| 22 |         return 0;
 | 
|---|
| 23 | }
 | 
|---|
| 24 | enum state_t { WAIT, SIGNAL, BARGE };
 | 
|---|
| 25 | 
 | 
|---|
| 26 | monitor global_t {};
 | 
|---|
| 27 | 
 | 
|---|
| 28 | monitor global_data_t {
 | 
|---|
| 29 |         volatile bool done;
 | 
|---|
| 30 |         int counter;
 | 
|---|
| 31 |         state_t state;
 | 
|---|
| 32 | 
 | 
|---|
| 33 |         unsigned short do_signal;
 | 
|---|
| 34 |         unsigned short do_wait2;
 | 
|---|
| 35 |         unsigned short do_wait1;
 | 
|---|
| 36 | };
 | 
|---|
| 37 | 
 | 
|---|
| 38 | void ?{} ( global_data_t & this ) {
 | 
|---|
| 39 |         this.done = false;
 | 
|---|
| 40 |         this.counter = 0;
 | 
|---|
| 41 |         this.state = BARGE;
 | 
|---|
| 42 | 
 | 
|---|
| 43 |         this.do_signal = 6;
 | 
|---|
| 44 |         this.do_wait1  = 1;
 | 
|---|
| 45 |         this.do_wait2  = 3;
 | 
|---|
| 46 | }
 | 
|---|
| 47 | 
 | 
|---|
| 48 | void ^?{} ( global_data_t & this ) {}
 | 
|---|
| 49 | 
 | 
|---|
| 50 | global_t globalA;
 | 
|---|
| 51 | global_t globalB;
 | 
|---|
| 52 | global_data_t globalC;
 | 
|---|
| 53 | 
 | 
|---|
| 54 | condition cond;
 | 
|---|
| 55 | 
 | 
|---|
| 56 | thread Threads {};
 | 
|---|
| 57 | 
 | 
|---|
| 58 | bool logicC( global_t & mutex a, global_t & mutex b, global_data_t & mutex c ) {
 | 
|---|
| 59 |         c.counter++;
 | 
|---|
| 60 | 
 | 
|---|
| 61 |         if( (c.counter % 1000) == 0 ) sout | c.counter | endl;
 | 
|---|
| 62 | 
 | 
|---|
| 63 |         int action = c.counter % 10;
 | 
|---|
| 64 | 
 | 
|---|
| 65 |         if( action == 0 ) {
 | 
|---|
| 66 |                 c.do_signal = max( random( 10 ), 1);
 | 
|---|
| 67 |                 c.do_wait1 = random( c.do_signal );
 | 
|---|
| 68 |                 c.do_wait2 = random( c.do_signal );
 | 
|---|
| 69 | 
 | 
|---|
| 70 |                 if(c.do_wait1 == c.do_wait2) sout | "Same" | endl;
 | 
|---|
| 71 |         }
 | 
|---|
| 72 | 
 | 
|---|
| 73 |         if( action == c.do_wait1 || action == c.do_wait2 ) {
 | 
|---|
| 74 |                 c.state = WAIT;
 | 
|---|
| 75 |                 wait( cond );
 | 
|---|
| 76 | 
 | 
|---|
| 77 |                 if(c.state != SIGNAL) {
 | 
|---|
| 78 |                         sout | "ERROR Barging detected" | c.counter | endl;
 | 
|---|
| 79 |                         abort();
 | 
|---|
| 80 |                 }
 | 
|---|
| 81 |         }
 | 
|---|
| 82 |         else if( action == c.do_signal ) {
 | 
|---|
| 83 |                 c.state = SIGNAL;
 | 
|---|
| 84 | 
 | 
|---|
| 85 |                 signal( cond );
 | 
|---|
| 86 |                 signal( cond );
 | 
|---|
| 87 |         }
 | 
|---|
| 88 |         else {
 | 
|---|
| 89 |                 c.state = BARGE;
 | 
|---|
| 90 |         }
 | 
|---|
| 91 | 
 | 
|---|
| 92 |         if( c.counter >= N ) c.done = true;
 | 
|---|
| 93 |         return !c.done;
 | 
|---|
| 94 | }
 | 
|---|
| 95 | 
 | 
|---|
| 96 | bool logicB( global_t & mutex a, global_t & mutex b ) {
 | 
|---|
| 97 |         return logicC(a, b, globalC);
 | 
|---|
| 98 | }
 | 
|---|
| 99 | 
 | 
|---|
| 100 | bool logicA( global_t & mutex a ) {
 | 
|---|
| 101 |         return logicB(a, globalB);
 | 
|---|
| 102 | }
 | 
|---|
| 103 | 
 | 
|---|
| 104 | void main( Threads & this ) {
 | 
|---|
| 105 |         while( logicA(globalA) ) { yield(); };
 | 
|---|
| 106 | }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | static thread_desc * volatile the_threads;
 | 
|---|
| 109 | 
 | 
|---|
| 110 | int main(int argc, char* argv[]) {
 | 
|---|
| 111 |         random_seed(0);
 | 
|---|
| 112 |         processor p;
 | 
|---|
| 113 |         {
 | 
|---|
| 114 |                 Threads t[17];
 | 
|---|
| 115 |                 the_threads = (thread_desc*)t;
 | 
|---|
| 116 |         }
 | 
|---|
| 117 | }
 | 
|---|