[2c9ebab] | 1 | #include <fstream> |
---|
| 2 | #include <kernel> |
---|
| 3 | #include <monitor> |
---|
| 4 | #include <stdlib> |
---|
| 5 | #include <thread> |
---|
| 6 | |
---|
[e1c1829] | 7 | #ifndef N |
---|
[f3c1737] | 8 | #define N 10_000 |
---|
[e1c1829] | 9 | #endif |
---|
[2c9ebab] | 10 | |
---|
| 11 | enum state_t { WAITED, SIGNAL, BARGE }; |
---|
| 12 | |
---|
| 13 | monitor global_data_t { |
---|
[ccd349d] | 14 | thread_desc * last_thread; |
---|
| 15 | thread_desc * last_signaller; |
---|
[2c9ebab] | 16 | }; |
---|
| 17 | |
---|
| 18 | void ?{} ( global_data_t * this ) { |
---|
[ccd349d] | 19 | this->last_thread = NULL; |
---|
| 20 | this->last_signaller = NULL; |
---|
[2c9ebab] | 21 | } |
---|
| 22 | |
---|
| 23 | void ^?{} ( global_data_t * this ) {} |
---|
| 24 | |
---|
| 25 | global_data_t globalA, globalB; |
---|
| 26 | |
---|
| 27 | condition cond; |
---|
| 28 | |
---|
| 29 | volatile bool done; |
---|
| 30 | |
---|
| 31 | //------------------------------------------------------------------------------ |
---|
| 32 | void wait_op( global_data_t * mutex a, global_data_t * mutex b, unsigned i ) { |
---|
[1c273d0] | 33 | wait( &cond, (uintptr_t)this_thread ); |
---|
[2c9ebab] | 34 | |
---|
| 35 | yield( ((unsigned)rand48()) % 10 ); |
---|
| 36 | |
---|
[ccd349d] | 37 | if(a->last_thread != a->last_signaller || b->last_thread != b->last_signaller ) { |
---|
| 38 | sout | "ERROR Barging detected, expected" | a->last_signaller | b->last_signaller | "got" | a->last_thread | b->last_thread | endl; |
---|
[2c9ebab] | 39 | abort(); |
---|
| 40 | } |
---|
| 41 | |
---|
[1c273d0] | 42 | a->last_thread = b->last_thread = this_thread; |
---|
[2c9ebab] | 43 | |
---|
| 44 | yield( ((unsigned)rand48()) % 10 ); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | thread Waiter {}; |
---|
| 48 | void main( Waiter* this ) { |
---|
| 49 | for( int i = 0; i < N; i++ ) { |
---|
| 50 | wait_op( &globalA, &globalB, i ); |
---|
| 51 | } |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | //------------------------------------------------------------------------------ |
---|
| 55 | void signal_op( global_data_t * mutex a, global_data_t * mutex b ) { |
---|
| 56 | yield( ((unsigned)rand48()) % 10 ); |
---|
| 57 | |
---|
[1c273d0] | 58 | a->last_thread = b->last_thread = a->last_signaller = b->last_signaller = this_thread; |
---|
[2c9ebab] | 59 | |
---|
[ccd349d] | 60 | if( !is_empty( &cond ) ) { |
---|
[2c9ebab] | 61 | |
---|
[ccd349d] | 62 | thread_desc * next = front( &cond ); |
---|
[2c9ebab] | 63 | |
---|
[ccd349d] | 64 | if( ! signal_block( &cond ) ) { |
---|
| 65 | sout | "ERROR expected to be able to signal" | endl; |
---|
| 66 | abort(); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | yield( ((unsigned)rand48()) % 10 ); |
---|
| 70 | |
---|
| 71 | if(a->last_thread != next || b->last_thread != next) { |
---|
| 72 | sout | "ERROR Barging detected, expected" | next | "got" | a->last_thread | b->last_thread | endl; |
---|
[2c9ebab] | 73 | abort(); |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | thread Signaller {}; |
---|
| 80 | void main( Signaller* this ) { |
---|
| 81 | while( !done ) { |
---|
| 82 | signal_op( &globalA, &globalB ); |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | //------------------------------------------------------------------------------ |
---|
| 87 | void barge_op( global_data_t * mutex a ) { |
---|
[1c273d0] | 88 | a->last_thread = this_thread; |
---|
[2c9ebab] | 89 | } |
---|
| 90 | |
---|
| 91 | thread Barger {}; |
---|
| 92 | void main( Barger* this ) { |
---|
| 93 | for( unsigned i = 0; !done; i++ ) { |
---|
| 94 | //Choose some monitor to barge into with some irregular pattern |
---|
| 95 | bool choose_a = (i % 13) > (i % 17); |
---|
| 96 | barge_op( choose_a ? &globalA : &globalB ); |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | //------------------------------------------------------------------------------ |
---|
| 101 | |
---|
| 102 | int main(int argc, char* argv[]) { |
---|
| 103 | rand48seed(0); |
---|
| 104 | done = false; |
---|
| 105 | processor p; |
---|
| 106 | { |
---|
| 107 | Signaller s[4]; |
---|
| 108 | Barger b[13]; |
---|
| 109 | sout | "Starting waiters" | endl; |
---|
| 110 | { |
---|
| 111 | Waiter w[3]; |
---|
| 112 | } |
---|
| 113 | sout | "Waiters done" | endl; |
---|
| 114 | done = true; |
---|
| 115 | } |
---|
[736fe25] | 116 | } |
---|