Changeset 58caf150
- Timestamp:
- Apr 20, 2018, 10:59:51 PM (7 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, with_gc
- Children:
- c8ad5d9
- Parents:
- f14d956
- Location:
- src/tests/concurrent/examples
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/tests/concurrent/examples/boundedBufferEXT.c
rf14d956 r58caf150 8 8 // Created On : Wed Apr 18 22:52:12 2018 9 9 // Last Modified By : Peter A. Buhr 10 // Last Modified On : Wed Apr 18 22:56:17201811 // Update Count : 210 // Last Modified On : Fri Apr 20 22:25:14 2018 11 // Update Count : 6 12 12 // 13 13 … … 20 20 //Duration default_preemption() { return 0; } 21 21 22 enum { BufferSize = 5 };22 enum { BufferSize = 50 }; 23 23 24 24 forall( otype T ) … … 33 33 forall( otype T ) 34 34 int query( Buffer(T) & buffer ) { return buffer.count; } 35 36 forall( otype T ) // forward 37 T remove( Buffer(T) & mutex buffer ); 35 38 36 39 forall( otype T ) -
src/tests/concurrent/examples/boundedBufferINT.c
rf14d956 r58caf150 8 8 // Created On : Mon Oct 30 12:45:13 2017 9 9 // Last Modified By : Peter A. Buhr 10 // Last Modified On : Tue Jan 2 12:18:18 201811 // Update Count : 3310 // Last Modified On : Fri Apr 20 22:18:18 2018 11 // Update Count : 78 12 12 // 13 13 14 #include <stdlib> 15 #include <fstream> // random14 #include <stdlib> // random 15 #include <fstream> 16 16 #include <kernel> 17 17 #include <thread> 18 18 #include <unistd.h> // getpid 19 19 20 //Duration default_preemption() { return 0; } 21 22 enum { BufferSize = 50 }; 23 24 forall( otype T ) 20 25 monitor Buffer { 21 26 condition full, empty; 22 27 int front, back, count; 23 int elements[20];28 T elements[BufferSize]; 24 29 }; 25 30 26 void ?{}( Buffer & buffer ) { 27 buffer.front = buffer.back = buffer.count = 0; 28 } 31 forall( otype T ) 32 void ?{}( Buffer(T) & buffer ) with( buffer ) { [front, back, count] = 0; } 29 33 30 int query( Buffer & buffer ) { return buffer.count; } 34 forall( otype T ) 35 int query( Buffer(T) & buffer ) { return buffer.count; } 31 36 32 void insert( Buffer & mutex buffer, int elem ) with( buffer ) { 33 if ( count == 20 ) wait( empty ); 37 forall( otype T ) 38 void insert( Buffer(T) & mutex buffer, T elem ) with( buffer ) { 39 if ( count == BufferSize ) wait( empty ); 34 40 elements[back] = elem; 35 back = ( back + 1 ) % 20;41 back = ( back + 1 ) % BufferSize; 36 42 count += 1; 37 43 signal( full ); 38 44 } 39 45 40 int remove( Buffer & mutex buffer ) with( buffer ) { 46 forall( otype T ) 47 T remove( Buffer(T) & mutex buffer ) with( buffer ) { 41 48 if ( count == 0 ) wait( full ); 42 intelem = elements[front];43 front = ( front + 1 ) % 20;49 T elem = elements[front]; 50 front = ( front + 1 ) % BufferSize; 44 51 count -= 1; 45 52 signal( empty ); … … 47 54 } 48 55 56 const int Sentinel = -1; 57 49 58 thread Producer { 50 Buffer & buffer;59 Buffer(int) & buffer; 51 60 unsigned int N; 52 61 }; 53 void main( Producer & prod ) {54 for ( int i = 1; i <= prod.N; i += 1 ) {62 void main( Producer & prod ) with( prod ) { 63 for ( int i = 1; i <= N; i += 1 ) { 55 64 yield( random( 5 ) ); 56 insert( prod.buffer, 1 );65 insert( buffer, 1 ); 57 66 } // for 58 insert( prod.buffer, -1 );59 67 } 60 void ?{}( Producer & prod, Buffer * buffer, unsignedint N ) {68 void ?{}( Producer & prod, Buffer(int) * buffer, int N ) { 61 69 &prod.buffer = buffer; 62 70 prod.N = N; … … 64 72 65 73 thread Consumer { 66 Buffer & buffer;67 int & sum; // summation of producer values74 Buffer(int) & buffer; 75 int & sum; // summation of producer values 68 76 }; 69 void main( Consumer & cons ) {70 cons.sum = 0;77 void main( Consumer & cons ) with( cons ) { 78 sum = 0; 71 79 for ( ;; ) { 72 80 yield( random( 5 ) ); 73 int item = remove( cons.buffer );74 if ( item == -1 ) break;// sentinel ?75 cons.sum += item;81 int item = remove( buffer ); 82 if ( item == Sentinel ) break; // sentinel ? 83 sum += item; 76 84 } // for 77 85 } 78 void ?{}( Consumer & cons, Buffer * buffer, int *sum ) {86 void ?{}( Consumer & cons, Buffer(int) * buffer, int & sum ) { 79 87 &cons.buffer = buffer; 80 &cons.sum = sum;88 &cons.sum = ∑ 81 89 } 82 90 83 91 int main() { 84 Buffer buffer;85 enum { Prods = 5, Cons = 5 };92 Buffer(int) buffer; 93 enum { Prods = 4, Cons = 5 }; 86 94 Producer * prods[Prods]; 87 95 Consumer * cons[Cons]; 88 const int Sentinel = -1;89 96 int sums[Cons]; 90 97 int i; … … 94 101 srandom( 1003 ); 95 102 96 for ( i = 0; i < Cons; i += 1 ) { // create consumers97 cons[i] = new( &buffer, &sums[i] );103 for ( i = 0; i < Cons; i += 1 ) { // create consumers 104 cons[i] = new( &buffer, sums[i] ); 98 105 } // for 99 for ( i = 0; i < Prods; i += 1 ) { // create producers100 prods[i] = new( &buffer, 100000 u);106 for ( i = 0; i < Prods; i += 1 ) { // create producers 107 prods[i] = new( &buffer, 100000 ); 101 108 } // for 102 109 103 for ( i = 0; i < Prods; i += 1 ) { // wait for producers to finish110 for ( i = 0; i < Prods; i += 1 ) { // wait for producers to finish 104 111 delete( prods[i] ); 105 112 } // for 106 for ( i = 0; i < Cons; i += 1 ) { // generate sentinal values to stop consumers113 for ( i = 0; i < Cons; i += 1 ) { // generate sentinal values to stop consumers 107 114 insert( buffer, Sentinel ); 108 115 } // for 109 116 int sum = 0; 110 for ( i = 0; i < Cons; i += 1 ) { // wait for consumers to finish117 for ( i = 0; i < Cons; i += 1 ) { // wait for consumers to finish 111 118 delete( cons[i] ); 112 119 sum += sums[i]; … … 117 124 // Local Variables: // 118 125 // tab-width: 4 // 119 // compile-command: "cfa boundedBuffer .c" //126 // compile-command: "cfa boundedBufferINT.c" // 120 127 // End: //
Note: See TracChangeset
for help on using the changeset viewer.