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