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