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