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