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