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