- Timestamp:
- May 4, 2017, 11:05:00 AM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 102a58b
- Parents:
- 42356f4
- Location:
- src/tests
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/tests/.expect/concurrent/sched-int-multi2.txt
r42356f4 r4845ae2 1 Waiting 12 Waiting 23 Waiting 34 Waiting 45 Signaling ABC6 Signaling AB7 Signaling BC8 Signaling AC9 Waking 410 Waking 311 Waking 212 Waking 1 -
src/tests/sched-int-multi2.c
r42356f4 r4845ae2 2 2 #include <kernel> 3 3 #include <monitor> 4 #include <stdlib> 4 5 #include <thread> 5 6 … … 12 13 condition condAB, condAC, condBC, condABC; 13 14 14 thread Signaler {}; 15 thread Signaler { 16 int signals[4]; 17 }; 18 19 void ?{}( Signaler * this ){ 20 this->signals[0] = 0; 21 this->signals[1] = 0; 22 this->signals[2] = 0; 23 this->signals[3] = 0; 24 } 25 15 26 thread WaiterAB {}; 16 27 thread WaiterAC {}; … … 18 29 thread WaiterABC{}; 19 30 20 int state; 21 22 /* 23 multi phase 24 */ 31 volatile bool done; 25 32 26 33 //---------------------------------------------------------------------------------------------------- … … 35 42 36 43 void wait( condition * cond, global_t * mutex a, global_t * mutex b ) { 37 state++;38 sout | "Waiting" | state | endl;39 44 wait( cond ); 40 sout | "Waking" | state | endl;41 state--;42 45 } 43 46 44 47 void wait( condition * cond, global_t * mutex a, global_t * mutex b, global_t * mutex c ) { 45 state++;46 sout | "Waiting" | state | endl;47 48 wait( cond ); 48 sout | "Waking" | state | endl;49 state--;50 49 } 51 50 52 51 //---------------------------------------------------------------------------------------------------- 53 52 // Signaler 54 // signals respectively AB, AC, BC, ABC 55 void signalerABC( global_t * mutex a, global_t * mutex b, global_t * mutex c ) { 56 sout | "Signaling ABC" | endl; 57 signal( &condABC, a, b, c ); 58 sout | "Signaling AB" | endl; 59 signal( &condAB , a, b ); 60 sout | "Signaling BC" | endl; 61 signal( &condBC , b, c ); 62 sout | "Signaling AC" | endl; 63 signal( &condAC , a, c ); 64 } 53 void main( Signaler* this ) { 65 54 66 void signalerAB( global_t * mutex a, global_t * mutex b, global_t * c) {67 signalerABC(a, b, c);68 } 55 while( true ) { 56 int action = (unsigned)rand48() % 4; 57 bool finished = true; 69 58 70 void signalerA( global_t * mutex a, global_t * b, global_t * c ) { 71 signalerAB (a, b, c); 72 } 59 for(int i = 0; i < 4; i++) { 60 if( this->signals[action] < 10_000 ) { 61 finished = false; 62 break; 63 } 64 else { 65 action = (action + 1) % 4; 66 } 67 } 73 68 74 void main( Signaler* this ) { 75 while( state != 4 ) { yield(); } 76 signalerA( &globalA, &globalB, &globalC ); 69 this->signals[action]++; 70 if( finished ) break; 71 72 //sout | action | this->signals[0] | this->signals[1] | this->signals[2] | this->signals[3] | endl; 73 74 switch( action ) { 75 case 0: 76 signal( &condABC, &globalA, &globalB, &globalC ); 77 break; 78 case 1: 79 signal( &condAB , &globalA, &globalB ); 80 break; 81 case 2: 82 signal( &condBC , &globalB, &globalC ); 83 break; 84 case 3: 85 signal( &condAC , &globalA, &globalC ); 86 break; 87 default: 88 sout | "Something went wrong" | endl; 89 abort(); 90 } 91 } 77 92 } 78 93 … … 80 95 // Waiter ABC 81 96 void main( WaiterABC* this ) { 82 while( state != 0 ) { yield(); } 83 wait( &condABC, &globalA, &globalB, &globalC ); 97 while( !done ) { 98 wait( &condABC, &globalA, &globalB, &globalC ); 99 } 84 100 } 85 101 … … 87 103 // Waiter AB 88 104 void main( WaiterAB* this ) { 89 while( state != 1 ) { yield(); } 90 wait( &condAB , &globalA, &globalB ); 105 while( !done ) { 106 wait( &condAB , &globalA, &globalB ); 107 } 91 108 } 92 109 … … 94 111 // Waiter AC 95 112 void main( WaiterAC* this ) { 96 while( state != 2 ) { yield(); } 97 wait( &condAC , &globalA, &globalC ); 113 while( !done ) { 114 wait( &condAC , &globalA, &globalC ); 115 } 98 116 } 99 117 … … 101 119 // Waiter BC 102 120 void main( WaiterBC* this ) { 103 while( state != 3 ) { yield(); } 104 wait( &condBC , &globalB, &globalC ); 121 while( !done ) { 122 wait( &condBC , &globalB, &globalC ); 123 } 105 124 } 106 125 … … 108 127 // Main 109 128 int main(int argc, char* argv[]) { 110 state = 0;129 done = false; 111 130 processor p; 112 131 { … … 115 134 WaiterBC c; 116 135 WaiterAC d; 117 Signaler e; 136 { 137 Signaler e; 138 } 139 done = true; 140 signal( &condABC, &globalA, &globalB, &globalC ); 141 signal( &condAB , &globalA, &globalB ); 142 signal( &condBC , &globalB, &globalC ); 143 signal( &condAC , &globalA, &globalC ); 118 144 } 119 145 }
Note: See TracChangeset
for help on using the changeset viewer.