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