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