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