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