Changeset a1ecdd1
- Timestamp:
- Dec 13, 2017, 9:11:25 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, resolv-new, with_gc
- Children:
- 1edf37f, 59073fd
- Parents:
- edb6f79
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/tests/concurrent/examples/boundedBuffer.c
redb6f79 ra1ecdd1 8 8 // Created On : Mon Oct 30 12:45:13 2017 9 9 // Last Modified By : Peter A. Buhr 10 // Last Modified On : Tue Dec 5 23:01:51201711 // Update Count : 1010 // Last Modified On : Wed Dec 13 21:01:49 2017 11 // Update Count : 27 12 12 // 13 13 … … 18 18 #include <unistd.h> // getpid 19 19 20 forall( otype T ) 20 21 monitor Buffer { 21 22 condition full, empty; 22 23 int front, back, count; 23 intelements[20];24 T elements[20]; 24 25 }; 25 26 26 void ?{}( Buffer & buffer ) { 27 forall( otype T ) 28 void ?{}( Buffer(T) & buffer ) { 27 29 buffer.front = buffer.back = buffer.count = 0; 28 30 } 29 31 30 int query( Buffer & buffer ) { return buffer.count; } 32 forall( otype T ) 33 int query( Buffer(T) & buffer ) { return buffer.count; } 31 34 32 void insert( Buffer & mutex buffer, int elem ) with( buffer ) { 35 forall( otype T ) 36 void insert( Buffer(T) & mutex buffer, T elem ) with( buffer ) { 33 37 if ( count == 20 ) wait( empty ); 34 38 elements[back] = elem; … … 37 41 signal( full ); 38 42 } 39 int remove( Buffer & mutex buffer ) with( buffer ) { 43 44 forall( otype T ) 45 T remove( Buffer(T) & mutex buffer ) with( buffer ) { 40 46 if ( count == 0 ) wait( full ); 41 intelem = elements[front];47 T elem = elements[front]; 42 48 front = ( front + 1 ) % 20; 43 49 count -= 1; … … 47 53 48 54 thread Producer { 49 Buffer & buffer;55 Buffer(int) & buffer; 50 56 unsigned int N; 51 57 }; … … 57 63 insert( prod.buffer, -1 ); 58 64 } 59 void ?{}( Producer & prod, Buffer * buffer, unsigned int N ) {65 void ?{}( Producer & prod, Buffer(int) * buffer, unsigned int N ) { 60 66 &prod.buffer = buffer; 61 67 prod.N = N; 62 68 } 63 69 64 70 thread Consumer { 65 Buffer & buffer;71 Buffer(int) & buffer; 66 72 int & sum; // summation of producer values 67 73 }; … … 75 81 } // for 76 82 } 77 void ?{}( Consumer & cons, Buffer * buffer, int * sum ) {83 void ?{}( Consumer & cons, Buffer(int) * buffer, int * sum ) { 78 84 &cons.buffer = buffer; 79 85 &cons.sum = sum; … … 81 87 82 88 int main() { 83 Buffer buffer;89 Buffer(int) buffer; 84 90 enum { Prods = 5, Cons = 5 }; 85 91 Producer * prods[Prods];
Note: See TracChangeset
for help on using the changeset viewer.