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