[f80ab45] | 1 | #include <fstream>
|
---|
[5ea06d6] | 2 | #include <kernel>
|
---|
| 3 | #include <monitor>
|
---|
| 4 | #include <thread>
|
---|
| 5 |
|
---|
[e1c1829] | 6 | #ifndef N
|
---|
[0322865c] | 7 | #define N 10_000
|
---|
[e1c1829] | 8 | #endif
|
---|
[5ea06d6] | 9 |
|
---|
[102a58b] | 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;
|
---|
[5ea06d6] | 23 |
|
---|
| 24 | condition cond;
|
---|
| 25 |
|
---|
[102a58b] | 26 | volatile bool all_done;
|
---|
[5ea06d6] | 27 |
|
---|
[102a58b] | 28 | void ?{}( global_data_t * this ) {
|
---|
| 29 | this->counter == 0;
|
---|
| 30 | this->state = BARGE;
|
---|
[5ea06d6] | 31 | }
|
---|
| 32 |
|
---|
[102a58b] | 33 | void ^?{} ( global_data_t * this ) {}
|
---|
[5ea06d6] | 34 |
|
---|
[102a58b] | 35 | //------------------------------------------------------------------------------
|
---|
| 36 | // Barging logic
|
---|
| 37 | void barge( global_data_t * mutex d ) {
|
---|
| 38 | d->state = BARGE;
|
---|
[5ea06d6] | 39 | }
|
---|
| 40 |
|
---|
[102a58b] | 41 | thread Barger {};
|
---|
[5ea06d6] | 42 |
|
---|
[102a58b] | 43 | void main( Barger * this ) {
|
---|
[0322865c] | 44 | while( !all_done ) {
|
---|
[102a58b] | 45 | barge( &data );
|
---|
[0322865c] | 46 | yield();
|
---|
[102a58b] | 47 | }
|
---|
[5ea06d6] | 48 | }
|
---|
| 49 |
|
---|
[102a58b] | 50 | //------------------------------------------------------------------------------
|
---|
| 51 | // Waiting logic
|
---|
| 52 | bool wait( global_t * mutex m, global_data_t * mutex d ) {
|
---|
| 53 | wait( &cond );
|
---|
| 54 | if( d->state != SIGNAL ) {
|
---|
[0322865c] | 55 | sout | "ERROR barging!" | endl;
|
---|
[102a58b] | 56 | }
|
---|
| 57 |
|
---|
| 58 | d->counter++;
|
---|
| 59 |
|
---|
| 60 | if( (d->counter % 1000) == 0 ) sout | d->counter | endl;
|
---|
| 61 |
|
---|
| 62 | return d->counter < N;
|
---|
[5ea06d6] | 63 | }
|
---|
| 64 |
|
---|
[102a58b] | 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 |
|
---|
[4c5b972] | 82 | yield( (unsigned)rand48() % 10 );
|
---|
[5ea06d6] | 83 |
|
---|
[102a58b] | 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 ) {
|
---|
[0322865c] | 87 | sout | "ERROR Eager signal" | data.state | endl;
|
---|
[102a58b] | 88 | }
|
---|
[5ea06d6] | 89 | }
|
---|
| 90 |
|
---|
[102a58b] | 91 | thread Signaller {};
|
---|
| 92 |
|
---|
| 93 | void main( Signaller * this ) {
|
---|
[0322865c] | 94 | while( !all_done ) {
|
---|
[102a58b] | 95 | logic( &mut );
|
---|
[0322865c] | 96 | yield();
|
---|
[102a58b] | 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | //------------------------------------------------------------------------------
|
---|
| 101 | // Main loop
|
---|
[5ea06d6] | 102 | int main(int argc, char* argv[]) {
|
---|
[102a58b] | 103 | all_done = false;
|
---|
[5ea06d6] | 104 | processor p;
|
---|
| 105 | {
|
---|
[102a58b] | 106 | Signaller s;
|
---|
| 107 | Barger b[17];
|
---|
| 108 | {
|
---|
| 109 | Waiter w[4];
|
---|
| 110 | }
|
---|
| 111 | sout | "All waiter done" | endl;
|
---|
| 112 | all_done = true;
|
---|
[0322865c] | 113 | }
|
---|
[5ea06d6] | 114 | }
|
---|