| 1 | //--------------------------------------------------------- | 
|---|
| 2 | // Multi wait test | 
|---|
| 3 | // Ensures that no deadlock from waiting/signalling conditions | 
|---|
| 4 | //--------------------------------------------------------- | 
|---|
| 5 |  | 
|---|
| 6 |  | 
|---|
| 7 | #include <fstream> | 
|---|
| 8 | #include <kernel> | 
|---|
| 9 | #include <monitor> | 
|---|
| 10 | #include <stdlib> | 
|---|
| 11 | #include <thread> | 
|---|
| 12 | #include <time> | 
|---|
| 13 |  | 
|---|
| 14 | #include "long_tests.h" | 
|---|
| 15 |  | 
|---|
| 16 | #ifndef PREEMPTION_RATE | 
|---|
| 17 | #define PREEMPTION_RATE 10`ms | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | Duration default_preemption() { | 
|---|
| 21 | return PREEMPTION_RATE; | 
|---|
| 22 | } | 
|---|
| 23 |  | 
|---|
| 24 | #ifdef TEST_LONG | 
|---|
| 25 | static const unsigned long N = 375_000ul; | 
|---|
| 26 | #else | 
|---|
| 27 | static const unsigned long N = 2_500ul; | 
|---|
| 28 | #endif | 
|---|
| 29 |  | 
|---|
| 30 | monitor global_t {}; | 
|---|
| 31 |  | 
|---|
| 32 | global_t globalA; | 
|---|
| 33 | global_t globalB; | 
|---|
| 34 | global_t globalC; | 
|---|
| 35 |  | 
|---|
| 36 | condition condAB, condAC, condBC, condABC; | 
|---|
| 37 |  | 
|---|
| 38 | thread Signaler {}; | 
|---|
| 39 | thread WaiterAB {}; | 
|---|
| 40 | thread WaiterAC {}; | 
|---|
| 41 | thread WaiterBC {}; | 
|---|
| 42 | thread WaiterABC{}; | 
|---|
| 43 |  | 
|---|
| 44 | volatile int waiter_left; | 
|---|
| 45 |  | 
|---|
| 46 | //---------------------------------------------------------------------------------------------------- | 
|---|
| 47 | // Tools | 
|---|
| 48 | void signal( condition & cond, global_t & mutex a, global_t & mutex b ) { | 
|---|
| 49 | signal( cond ); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | void signal( condition & cond, global_t & mutex a, global_t & mutex b, global_t & mutex c ) { | 
|---|
| 53 | signal( cond ); | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | void wait( condition & cond, global_t & mutex a, global_t & mutex b ) { | 
|---|
| 57 | wait( cond ); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | void wait( condition & cond, global_t & mutex a, global_t & mutex b, global_t & mutex c ) { | 
|---|
| 61 | wait( cond ); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | //---------------------------------------------------------------------------------------------------- | 
|---|
| 65 | // Signaler | 
|---|
| 66 | void main( Signaler & this ) { | 
|---|
| 67 |  | 
|---|
| 68 | while( waiter_left != 0 ) { | 
|---|
| 69 | unsigned action = random( 4 ); | 
|---|
| 70 | switch( action ) { | 
|---|
| 71 | case 0: | 
|---|
| 72 | signal( condABC, globalA, globalB, globalC ); | 
|---|
| 73 | break; | 
|---|
| 74 | case 1: | 
|---|
| 75 | signal( condAB , globalA, globalB ); | 
|---|
| 76 | break; | 
|---|
| 77 | case 2: | 
|---|
| 78 | signal( condBC , globalB, globalC ); | 
|---|
| 79 | break; | 
|---|
| 80 | case 3: | 
|---|
| 81 | signal( condAC , globalA, globalC ); | 
|---|
| 82 | break; | 
|---|
| 83 | default: | 
|---|
| 84 | sout | "Something went wrong" | endl; | 
|---|
| 85 | abort(); | 
|---|
| 86 | } | 
|---|
| 87 | yield(); | 
|---|
| 88 | } | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | //---------------------------------------------------------------------------------------------------- | 
|---|
| 92 | // Waiter ABC | 
|---|
| 93 | void main( WaiterABC & this ) { | 
|---|
| 94 | for( int i = 0; TEST(i < N); i++ ) { | 
|---|
| 95 | wait( condABC, globalA, globalB, globalC ); | 
|---|
| 96 | KICK_WATCHDOG; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | __sync_fetch_and_sub_4( &waiter_left, 1); | 
|---|
| 100 | } | 
|---|
| 101 |  | 
|---|
| 102 | //---------------------------------------------------------------------------------------------------- | 
|---|
| 103 | // Waiter AB | 
|---|
| 104 | void main( WaiterAB & this ) { | 
|---|
| 105 | for( int i = 0; TEST(i < N); i++ ) { | 
|---|
| 106 | wait( condAB , globalA, globalB ); | 
|---|
| 107 | KICK_WATCHDOG; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | __sync_fetch_and_sub_4( &waiter_left, 1); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | //---------------------------------------------------------------------------------------------------- | 
|---|
| 114 | // Waiter AC | 
|---|
| 115 | void main( WaiterAC & this ) { | 
|---|
| 116 | for( int i = 0; TEST(i < N); i++ ) { | 
|---|
| 117 | wait( condAC , globalA, globalC ); | 
|---|
| 118 | KICK_WATCHDOG; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | __sync_fetch_and_sub_4( &waiter_left, 1); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | //---------------------------------------------------------------------------------------------------- | 
|---|
| 125 | // Waiter BC | 
|---|
| 126 | void main( WaiterBC & this ) { | 
|---|
| 127 | for( int i = 0; TEST(i < N); i++ ) { | 
|---|
| 128 | wait( condBC , globalB, globalC ); | 
|---|
| 129 | KICK_WATCHDOG; | 
|---|
| 130 | } | 
|---|
| 131 |  | 
|---|
| 132 | __sync_fetch_and_sub_4( &waiter_left, 1); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | //---------------------------------------------------------------------------------------------------- | 
|---|
| 136 | // Main | 
|---|
| 137 | int main(int argc, char* argv[]) { | 
|---|
| 138 | srandom( time( NULL ) ); | 
|---|
| 139 | waiter_left = 4; | 
|---|
| 140 | processor p[2]; | 
|---|
| 141 | sout | "Starting" | endl; | 
|---|
| 142 | { | 
|---|
| 143 | Signaler  e; | 
|---|
| 144 | { | 
|---|
| 145 | WaiterABC a; | 
|---|
| 146 | WaiterAB  b; | 
|---|
| 147 | WaiterBC  c; | 
|---|
| 148 | WaiterAC  d; | 
|---|
| 149 | } | 
|---|
| 150 | } | 
|---|
| 151 | sout | "Done" | endl; | 
|---|
| 152 | } | 
|---|