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