| 1 | #include <fstream>
|
|---|
| 2 | #include <kernel>
|
|---|
| 3 | #include <monitor>
|
|---|
| 4 | #include <stdlib>
|
|---|
| 5 | #include <thread>
|
|---|
| 6 |
|
|---|
| 7 | static const int N = 10_000;
|
|---|
| 8 |
|
|---|
| 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 |
|
|---|
| 17 | thread Signaler {};
|
|---|
| 18 | thread WaiterAB {};
|
|---|
| 19 | thread WaiterAC {};
|
|---|
| 20 | thread WaiterBC {};
|
|---|
| 21 | thread WaiterABC{};
|
|---|
| 22 |
|
|---|
| 23 | volatile int waiter_left;
|
|---|
| 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 ) {
|
|---|
| 46 |
|
|---|
| 47 | while( waiter_left != 0 ) {
|
|---|
| 48 | unsigned action = (unsigned)rand48() % 4;
|
|---|
| 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 | }
|
|---|
| 66 | yield();
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | //----------------------------------------------------------------------------------------------------
|
|---|
| 71 | // Waiter ABC
|
|---|
| 72 | void main( WaiterABC* this ) {
|
|---|
| 73 | for( int i = 0; i < N; i++ ) {
|
|---|
| 74 | wait( &condABC, &globalA, &globalB, &globalC );
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | __sync_fetch_and_sub_4( &waiter_left, 1);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | //----------------------------------------------------------------------------------------------------
|
|---|
| 81 | // Waiter AB
|
|---|
| 82 | void main( WaiterAB* this ) {
|
|---|
| 83 | for( int i = 0; i < N; i++ ) {
|
|---|
| 84 | wait( &condAB , &globalA, &globalB );
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | __sync_fetch_and_sub_4( &waiter_left, 1);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | //----------------------------------------------------------------------------------------------------
|
|---|
| 91 | // Waiter AC
|
|---|
| 92 | void main( WaiterAC* this ) {
|
|---|
| 93 | for( int i = 0; i < N; i++ ) {
|
|---|
| 94 | wait( &condAC , &globalA, &globalC );
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | __sync_fetch_and_sub_4( &waiter_left, 1);
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | //----------------------------------------------------------------------------------------------------
|
|---|
| 101 | // Waiter BC
|
|---|
| 102 | void main( WaiterBC* this ) {
|
|---|
| 103 | for( int i = 0; i < N; i++ ) {
|
|---|
| 104 | wait( &condBC , &globalB, &globalC );
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | __sync_fetch_and_sub_4( &waiter_left, 1);
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | //----------------------------------------------------------------------------------------------------
|
|---|
| 111 | // Main
|
|---|
| 112 | int main(int argc, char* argv[]) {
|
|---|
| 113 | waiter_left = 4;
|
|---|
| 114 | processor p;
|
|---|
| 115 | sout | "Starting" | endl;
|
|---|
| 116 | {
|
|---|
| 117 | Signaler e;
|
|---|
| 118 | {
|
|---|
| 119 | WaiterABC a;
|
|---|
| 120 | WaiterAB b;
|
|---|
| 121 | WaiterBC c;
|
|---|
| 122 | WaiterAC d;
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | sout | "Done" | endl;
|
|---|
| 126 | }
|
|---|