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