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