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