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