| 1 | #include <fstream> | 
|---|
| 2 | #include <kernel> | 
|---|
| 3 | #include <monitor> | 
|---|
| 4 | #include <thread> | 
|---|
| 5 | #include <time> | 
|---|
| 6 |  | 
|---|
| 7 | #ifdef LONG_TEST | 
|---|
| 8 | static const unsigned long N = 300_000ul; | 
|---|
| 9 | #else | 
|---|
| 10 | static const unsigned long N = 10_000ul; | 
|---|
| 11 | #endif | 
|---|
| 12 |  | 
|---|
| 13 | #ifndef PREEMPTION_RATE | 
|---|
| 14 | #define PREEMPTION_RATE 10`ms | 
|---|
| 15 | #endif | 
|---|
| 16 |  | 
|---|
| 17 | Duration default_preemption() { | 
|---|
| 18 | return PREEMPTION_RATE; | 
|---|
| 19 | } | 
|---|
| 20 |  | 
|---|
| 21 | enum state_t { WAIT, SIGNAL, BARGE }; | 
|---|
| 22 |  | 
|---|
| 23 | monitor global_t {}; | 
|---|
| 24 | global_t mut; | 
|---|
| 25 |  | 
|---|
| 26 | monitor global_data_t; | 
|---|
| 27 | void ?{}( global_data_t & this ); | 
|---|
| 28 | void ^?{} ( global_data_t & this ); | 
|---|
| 29 |  | 
|---|
| 30 | monitor global_data_t { | 
|---|
| 31 | int counter; | 
|---|
| 32 | state_t state; | 
|---|
| 33 | } data; | 
|---|
| 34 |  | 
|---|
| 35 | condition cond; | 
|---|
| 36 |  | 
|---|
| 37 | volatile bool all_done; | 
|---|
| 38 |  | 
|---|
| 39 | void ?{}( global_data_t & this ) { | 
|---|
| 40 | this.counter == 0; | 
|---|
| 41 | this.state = BARGE; | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | void ^?{} ( global_data_t & this ) {} | 
|---|
| 45 |  | 
|---|
| 46 | //------------------------------------------------------------------------------ | 
|---|
| 47 | // Barging logic | 
|---|
| 48 | void barge( global_data_t & mutex d ) { | 
|---|
| 49 | d.state = BARGE; | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | thread Barger {}; | 
|---|
| 53 |  | 
|---|
| 54 | void main( Barger & this ) { | 
|---|
| 55 | while( !all_done ) { | 
|---|
| 56 | barge( data ); | 
|---|
| 57 | yield(); | 
|---|
| 58 | } | 
|---|
| 59 | } | 
|---|
| 60 |  | 
|---|
| 61 | //------------------------------------------------------------------------------ | 
|---|
| 62 | // Waiting logic | 
|---|
| 63 | bool wait( global_t & mutex m, global_data_t & mutex d ) { | 
|---|
| 64 | wait( cond ); | 
|---|
| 65 | if( d.state != SIGNAL ) { | 
|---|
| 66 | sout | "ERROR barging!" | endl; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | d.counter++; | 
|---|
| 70 |  | 
|---|
| 71 | if( (d.counter % 1000) == 0 ) sout | d.counter | endl; | 
|---|
| 72 |  | 
|---|
| 73 | return d.counter < N; | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | thread Waiter {}; | 
|---|
| 77 |  | 
|---|
| 78 | void main( Waiter & this ) { | 
|---|
| 79 | while( wait( mut, data ) ) { yield(); } | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 |  | 
|---|
| 83 | //------------------------------------------------------------------------------ | 
|---|
| 84 | // Signalling logic | 
|---|
| 85 | void signal( condition & cond, global_t & mutex a, global_data_t & mutex b ) { | 
|---|
| 86 | b.state = SIGNAL; | 
|---|
| 87 | signal( cond ); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | void logic( global_t & mutex a ) { | 
|---|
| 91 | signal( cond, a, data ); | 
|---|
| 92 |  | 
|---|
| 93 | yield( random( 10 ) ); | 
|---|
| 94 |  | 
|---|
| 95 | //This is technically a mutual exclusion violation but the mutex monitor protects us | 
|---|
| 96 | bool running = data.counter < N && data.counter > 0; | 
|---|
| 97 | if( data.state != SIGNAL && running ) { | 
|---|
| 98 | sout | "ERROR Eager signal" | data.state | endl; | 
|---|
| 99 | } | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | thread Signaller {}; | 
|---|
| 103 |  | 
|---|
| 104 | void main( Signaller & this ) { | 
|---|
| 105 | while( !all_done ) { | 
|---|
| 106 | logic( mut ); | 
|---|
| 107 | yield(); | 
|---|
| 108 | } | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | //------------------------------------------------------------------------------ | 
|---|
| 112 | // Main loop | 
|---|
| 113 | int main(int argc, char* argv[]) { | 
|---|
| 114 | srandom( time( NULL ) ); | 
|---|
| 115 | all_done = false; | 
|---|
| 116 | processor p; | 
|---|
| 117 | { | 
|---|
| 118 | Signaller s; | 
|---|
| 119 | Barger b[17]; | 
|---|
| 120 | { | 
|---|
| 121 | Waiter w[4]; | 
|---|
| 122 | } | 
|---|
| 123 | sout | "All waiter done" | endl; | 
|---|
| 124 | all_done = true; | 
|---|
| 125 | } | 
|---|
| 126 | } | 
|---|