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