[9fe39530] | 1 | //--------------------------------------------------------- |
---|
| 2 | // Barging test |
---|
| 3 | // Ensures that no barging can occur between : |
---|
| 4 | // - the frontend of the signal_block and the signaled thread |
---|
| 5 | // - the signaled threadand the backend of the signal_block |
---|
| 6 | //--------------------------------------------------------- |
---|
| 7 | |
---|
| 8 | |
---|
[2c9ebab] | 9 | #include <fstream> |
---|
| 10 | #include <kernel> |
---|
| 11 | #include <monitor> |
---|
| 12 | #include <stdlib> |
---|
| 13 | #include <thread> |
---|
[0f56058] | 14 | #include <time> |
---|
[cd99ef1] | 15 | |
---|
| 16 | #ifndef PREEMPTION_RATE |
---|
[8ad6533] | 17 | #define PREEMPTION_RATE 10`ms |
---|
[e1c1829] | 18 | #endif |
---|
[2c9ebab] | 19 | |
---|
[8ad6533] | 20 | Duration default_preemption() { |
---|
[cd99ef1] | 21 | return PREEMPTION_RATE; |
---|
| 22 | } |
---|
| 23 | |
---|
[b9da9585] | 24 | #ifdef LONG_TEST |
---|
| 25 | static const unsigned long N = 150_000ul; |
---|
| 26 | #else |
---|
| 27 | static const unsigned long N = 5_000ul; |
---|
| 28 | #endif |
---|
| 29 | |
---|
[2c9ebab] | 30 | enum state_t { WAITED, SIGNAL, BARGE }; |
---|
| 31 | |
---|
| 32 | monitor global_data_t { |
---|
[ccd349d] | 33 | thread_desc * last_thread; |
---|
| 34 | thread_desc * last_signaller; |
---|
[2c9ebab] | 35 | }; |
---|
| 36 | |
---|
[00e80b6] | 37 | void ?{} ( global_data_t & this ) { |
---|
| 38 | this.last_thread = NULL; |
---|
| 39 | this.last_signaller = NULL; |
---|
[2c9ebab] | 40 | } |
---|
| 41 | |
---|
[8638cef] | 42 | void ^?{} ( global_data_t & mutex this ) {} |
---|
[2c9ebab] | 43 | |
---|
| 44 | global_data_t globalA, globalB; |
---|
| 45 | |
---|
| 46 | condition cond; |
---|
| 47 | |
---|
| 48 | volatile bool done; |
---|
| 49 | |
---|
| 50 | //------------------------------------------------------------------------------ |
---|
[83a071f9] | 51 | void wait_op( global_data_t & mutex a, global_data_t & mutex b, unsigned i ) { |
---|
[94ddede] | 52 | wait( cond, (uintptr_t)active_thread() ); |
---|
[2c9ebab] | 53 | |
---|
[6c7b1e7] | 54 | yield( random( 10 ) ); |
---|
[2c9ebab] | 55 | |
---|
[83a071f9] | 56 | if(a.last_thread != a.last_signaller || b.last_thread != b.last_signaller ) { |
---|
| 57 | sout | "ERROR Barging detected, expected" | a.last_signaller | b.last_signaller | "got" | a.last_thread | b.last_thread | endl; |
---|
[2c9ebab] | 58 | abort(); |
---|
| 59 | } |
---|
| 60 | |
---|
[94ddede] | 61 | a.last_thread = b.last_thread = active_thread(); |
---|
[2c9ebab] | 62 | |
---|
[6c7b1e7] | 63 | yield( random( 10 ) ); |
---|
[2c9ebab] | 64 | } |
---|
| 65 | |
---|
| 66 | thread Waiter {}; |
---|
[83a071f9] | 67 | void main( Waiter & this ) { |
---|
[2c9ebab] | 68 | for( int i = 0; i < N; i++ ) { |
---|
[83a071f9] | 69 | wait_op( globalA, globalB, i ); |
---|
[2c9ebab] | 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | //------------------------------------------------------------------------------ |
---|
[83a071f9] | 74 | void signal_op( global_data_t & mutex a, global_data_t & mutex b ) { |
---|
[6c7b1e7] | 75 | yield( random( 10 ) ); |
---|
[2c9ebab] | 76 | |
---|
[94ddede] | 77 | [a.last_thread, b.last_thread, a.last_signaller, b.last_signaller] = active_thread(); |
---|
[2c9ebab] | 78 | |
---|
[4cedd9f] | 79 | if( !is_empty( cond ) ) { |
---|
[2c9ebab] | 80 | |
---|
[4cedd9f] | 81 | thread_desc * next = front( cond ); |
---|
[2c9ebab] | 82 | |
---|
[4cedd9f] | 83 | if( ! signal_block( cond ) ) { |
---|
[ccd349d] | 84 | sout | "ERROR expected to be able to signal" | endl; |
---|
| 85 | abort(); |
---|
| 86 | } |
---|
| 87 | |
---|
[6c7b1e7] | 88 | yield( random( 10 ) ); |
---|
[ccd349d] | 89 | |
---|
[83a071f9] | 90 | if(a.last_thread != next || b.last_thread != next) { |
---|
| 91 | sout | "ERROR Barging detected, expected" | next | "got" | a.last_thread | b.last_thread | endl; |
---|
[2c9ebab] | 92 | abort(); |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | thread Signaller {}; |
---|
[83a071f9] | 99 | void main( Signaller & this ) { |
---|
[2c9ebab] | 100 | while( !done ) { |
---|
[83a071f9] | 101 | signal_op( globalA, globalB ); |
---|
[2c9ebab] | 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | //------------------------------------------------------------------------------ |
---|
[83a071f9] | 106 | void barge_op( global_data_t & mutex a ) { |
---|
[94ddede] | 107 | a.last_thread = active_thread(); |
---|
[2c9ebab] | 108 | } |
---|
| 109 | |
---|
| 110 | thread Barger {}; |
---|
[83a071f9] | 111 | void main( Barger & this ) { |
---|
[2c9ebab] | 112 | for( unsigned i = 0; !done; i++ ) { |
---|
| 113 | //Choose some monitor to barge into with some irregular pattern |
---|
| 114 | bool choose_a = (i % 13) > (i % 17); |
---|
[83a071f9] | 115 | if ( choose_a ) barge_op( globalA ); |
---|
| 116 | else barge_op( globalB ); |
---|
[2c9ebab] | 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | //------------------------------------------------------------------------------ |
---|
| 121 | |
---|
| 122 | int main(int argc, char* argv[]) { |
---|
[54aba8d] | 123 | srandom( time( NULL ) ); |
---|
[2c9ebab] | 124 | done = false; |
---|
| 125 | processor p; |
---|
| 126 | { |
---|
| 127 | Signaller s[4]; |
---|
| 128 | Barger b[13]; |
---|
| 129 | sout | "Starting waiters" | endl; |
---|
| 130 | { |
---|
| 131 | Waiter w[3]; |
---|
| 132 | } |
---|
| 133 | sout | "Waiters done" | endl; |
---|
| 134 | done = true; |
---|
| 135 | } |
---|
[736fe25] | 136 | } |
---|