[f80ab45] | 1 | #include <fstream> |
---|
[5ea06d6] | 2 | #include <kernel> |
---|
| 3 | #include <monitor> |
---|
| 4 | #include <thread> |
---|
| 5 | |
---|
[cd99ef1] | 6 | static const unsigned long N = 10_000ul; |
---|
| 7 | |
---|
| 8 | #ifndef PREEMPTION_RATE |
---|
| 9 | #define PREEMPTION_RATE 10_000ul |
---|
[e1c1829] | 10 | #endif |
---|
[5ea06d6] | 11 | |
---|
[cd99ef1] | 12 | unsigned int default_preemption() { |
---|
| 13 | return PREEMPTION_RATE; |
---|
| 14 | } |
---|
| 15 | |
---|
[102a58b] | 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; |
---|
[5ea06d6] | 29 | |
---|
| 30 | condition cond; |
---|
| 31 | |
---|
[102a58b] | 32 | volatile bool all_done; |
---|
[5ea06d6] | 33 | |
---|
[102a58b] | 34 | void ?{}( global_data_t * this ) { |
---|
| 35 | this->counter == 0; |
---|
| 36 | this->state = BARGE; |
---|
[5ea06d6] | 37 | } |
---|
| 38 | |
---|
[102a58b] | 39 | void ^?{} ( global_data_t * this ) {} |
---|
[5ea06d6] | 40 | |
---|
[102a58b] | 41 | //------------------------------------------------------------------------------ |
---|
| 42 | // Barging logic |
---|
| 43 | void barge( global_data_t * mutex d ) { |
---|
| 44 | d->state = BARGE; |
---|
[5ea06d6] | 45 | } |
---|
| 46 | |
---|
[102a58b] | 47 | thread Barger {}; |
---|
[5ea06d6] | 48 | |
---|
[102a58b] | 49 | void main( Barger * this ) { |
---|
[0322865c] | 50 | while( !all_done ) { |
---|
[102a58b] | 51 | barge( &data ); |
---|
[0322865c] | 52 | yield(); |
---|
[102a58b] | 53 | } |
---|
[5ea06d6] | 54 | } |
---|
| 55 | |
---|
[102a58b] | 56 | //------------------------------------------------------------------------------ |
---|
| 57 | // Waiting logic |
---|
| 58 | bool wait( global_t * mutex m, global_data_t * mutex d ) { |
---|
| 59 | wait( &cond ); |
---|
| 60 | if( d->state != SIGNAL ) { |
---|
[0322865c] | 61 | sout | "ERROR barging!" | endl; |
---|
[102a58b] | 62 | } |
---|
| 63 | |
---|
| 64 | d->counter++; |
---|
| 65 | |
---|
| 66 | if( (d->counter % 1000) == 0 ) sout | d->counter | endl; |
---|
| 67 | |
---|
| 68 | return d->counter < N; |
---|
[5ea06d6] | 69 | } |
---|
| 70 | |
---|
[102a58b] | 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 | |
---|
[4c5b972] | 88 | yield( (unsigned)rand48() % 10 ); |
---|
[5ea06d6] | 89 | |
---|
[102a58b] | 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 ) { |
---|
[0322865c] | 93 | sout | "ERROR Eager signal" | data.state | endl; |
---|
[102a58b] | 94 | } |
---|
[5ea06d6] | 95 | } |
---|
| 96 | |
---|
[102a58b] | 97 | thread Signaller {}; |
---|
| 98 | |
---|
| 99 | void main( Signaller * this ) { |
---|
[0322865c] | 100 | while( !all_done ) { |
---|
[102a58b] | 101 | logic( &mut ); |
---|
[0322865c] | 102 | yield(); |
---|
[102a58b] | 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | //------------------------------------------------------------------------------ |
---|
| 107 | // Main loop |
---|
[5ea06d6] | 108 | int main(int argc, char* argv[]) { |
---|
[102a58b] | 109 | all_done = false; |
---|
[5ea06d6] | 110 | processor p; |
---|
| 111 | { |
---|
[102a58b] | 112 | Signaller s; |
---|
| 113 | Barger b[17]; |
---|
| 114 | { |
---|
| 115 | Waiter w[4]; |
---|
| 116 | } |
---|
| 117 | sout | "All waiter done" | endl; |
---|
| 118 | all_done = true; |
---|
[0322865c] | 119 | } |
---|
[5ea06d6] | 120 | } |
---|