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