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