Changeset 19801aa
- Timestamp:
- May 9, 2017, 4:24:55 PM (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:
- 66ef082
- Parents:
- f408e1a
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/tests/sched-int-wait.c
rf408e1a r19801aa 4 4 #include <stdlib> 5 5 #include <thread> 6 7 static const int N = 10_000; 6 8 7 9 monitor global_t {}; … … 13 15 condition condAB, condAC, condBC, condABC; 14 16 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 17 thread Signaler {}; 26 18 thread WaiterAB {}; 27 19 thread WaiterAC {}; … … 29 21 thread WaiterABC{}; 30 22 31 volatile bool done;23 volatile int waiter_left; 32 24 33 25 //---------------------------------------------------------------------------------------------------- … … 53 45 void main( Signaler* this ) { 54 46 55 while( true ) { 56 int action = (unsigned)rand48() % 4; 57 bool finished = true; 58 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 } 68 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 47 while( waiter_left != 0 ) { 48 unsigned action = (unsigned)rand48() % 4; 74 49 switch( action ) { 75 50 case 0: … … 89 64 abort(); 90 65 } 66 yield(); 91 67 } 92 68 } … … 95 71 // Waiter ABC 96 72 void main( WaiterABC* this ) { 97 while( !done) {73 for( int i = 0; i < N; i++ ) { 98 74 wait( &condABC, &globalA, &globalB, &globalC ); 99 75 } 76 77 __sync_fetch_and_sub_4( &waiter_left, 1); 100 78 } 101 79 … … 103 81 // Waiter AB 104 82 void main( WaiterAB* this ) { 105 while( !done) {83 for( int i = 0; i < N; i++ ) { 106 84 wait( &condAB , &globalA, &globalB ); 107 85 } 86 87 __sync_fetch_and_sub_4( &waiter_left, 1); 108 88 } 109 89 … … 111 91 // Waiter AC 112 92 void main( WaiterAC* this ) { 113 while( !done) {93 for( int i = 0; i < N; i++ ) { 114 94 wait( &condAC , &globalA, &globalC ); 115 95 } 96 97 __sync_fetch_and_sub_4( &waiter_left, 1); 116 98 } 117 99 … … 119 101 // Waiter BC 120 102 void main( WaiterBC* this ) { 121 while( !done) {103 for( int i = 0; i < N; i++ ) { 122 104 wait( &condBC , &globalB, &globalC ); 123 105 } 106 107 __sync_fetch_and_sub_4( &waiter_left, 1); 124 108 } 125 109 … … 127 111 // Main 128 112 int main(int argc, char* argv[]) { 129 done = false;113 waiter_left = 4; 130 114 processor p; 131 115 { 132 WaiterABC a; 133 WaiterAB b; 134 WaiterBC c; 135 WaiterAC d; 116 Signaler e; 136 117 { 137 Signaler e; 118 WaiterABC a; 119 WaiterAB b; 120 WaiterBC c; 121 WaiterAC d; 138 122 } 139 done = true;140 signal( &condABC, &globalA, &globalB, &globalC );141 signal( &condAB , &globalA, &globalB );142 signal( &condBC , &globalB, &globalC );143 signal( &condAC , &globalA, &globalC );144 123 } 145 124 }
Note: See TracChangeset
for help on using the changeset viewer.