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