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