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