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