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