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