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