[4cedd9f] | 1 | // |
---|
[9f865d1] | 2 | // The contents of this file are covered under the licence agreement in the |
---|
| 3 | // file "LICENCE" distributed with Cforall. |
---|
[4cedd9f] | 4 | // |
---|
| 5 | // boundedBuffer.c -- |
---|
| 6 | // |
---|
[9f865d1] | 7 | // Author : Peter A. Buhr |
---|
| 8 | // Created On : Mon Oct 30 12:45:13 2017 |
---|
| 9 | // Last Modified By : Peter A. Buhr |
---|
[54aba8d] | 10 | // Last Modified On : Tue Jan 2 12:18:18 2018 |
---|
| 11 | // Update Count : 33 |
---|
[4cedd9f] | 12 | // |
---|
[9f865d1] | 13 | |
---|
[1033f5d] | 14 | #include <stdlib> |
---|
[6c7b1e7] | 15 | #include <fstream> // random |
---|
[1033f5d] | 16 | #include <kernel> |
---|
| 17 | #include <thread> |
---|
[6c7b1e7] | 18 | #include <unistd.h> // getpid |
---|
[1033f5d] | 19 | |
---|
| 20 | monitor Buffer { |
---|
[9f865d1] | 21 | condition full, empty; |
---|
| 22 | int front, back, count; |
---|
[1edf37f] | 23 | int elements[20]; |
---|
[1033f5d] | 24 | }; |
---|
| 25 | |
---|
[1edf37f] | 26 | void ?{}( Buffer & buffer ) { |
---|
[9f865d1] | 27 | buffer.front = buffer.back = buffer.count = 0; |
---|
[1033f5d] | 28 | } |
---|
| 29 | |
---|
[1edf37f] | 30 | int query( Buffer & buffer ) { return buffer.count; } |
---|
[1033f5d] | 31 | |
---|
[1edf37f] | 32 | void insert( Buffer & mutex buffer, int elem ) with( buffer ) { |
---|
[971d9f2] | 33 | if ( count == 20 ) wait( empty ); |
---|
| 34 | elements[back] = elem; |
---|
| 35 | back = ( back + 1 ) % 20; |
---|
| 36 | count += 1; |
---|
| 37 | signal( full ); |
---|
[1033f5d] | 38 | } |
---|
[a1ecdd1] | 39 | |
---|
[1edf37f] | 40 | int remove( Buffer & mutex buffer ) with( buffer ) { |
---|
[971d9f2] | 41 | if ( count == 0 ) wait( full ); |
---|
[1edf37f] | 42 | int elem = elements[front]; |
---|
[971d9f2] | 43 | front = ( front + 1 ) % 20; |
---|
| 44 | count -= 1; |
---|
| 45 | signal( empty ); |
---|
[9f865d1] | 46 | return elem; |
---|
[1033f5d] | 47 | } |
---|
| 48 | |
---|
| 49 | thread Producer { |
---|
[1edf37f] | 50 | Buffer & buffer; |
---|
[9f865d1] | 51 | unsigned int N; |
---|
[1033f5d] | 52 | }; |
---|
| 53 | void main( Producer & prod ) { |
---|
[9f865d1] | 54 | for ( int i = 1; i <= prod.N; i += 1 ) { |
---|
[6c7b1e7] | 55 | yield( random( 5 ) ); |
---|
[9f865d1] | 56 | insert( prod.buffer, 1 ); |
---|
| 57 | } // for |
---|
| 58 | insert( prod.buffer, -1 ); |
---|
[1033f5d] | 59 | } |
---|
[1edf37f] | 60 | void ?{}( Producer & prod, Buffer * buffer, unsigned int N ) { |
---|
[9f865d1] | 61 | &prod.buffer = buffer; |
---|
| 62 | prod.N = N; |
---|
[a1ecdd1] | 63 | } |
---|
[1033f5d] | 64 | |
---|
| 65 | thread Consumer { |
---|
[1edf37f] | 66 | Buffer & buffer; |
---|
[9f865d1] | 67 | int & sum; // summation of producer values |
---|
[1033f5d] | 68 | }; |
---|
| 69 | void main( Consumer & cons ) { |
---|
[9f865d1] | 70 | cons.sum = 0; |
---|
| 71 | for ( ;; ) { |
---|
[6c7b1e7] | 72 | yield( random( 5 ) ); |
---|
[9f865d1] | 73 | int item = remove( cons.buffer ); |
---|
| 74 | if ( item == -1 ) break; // sentinel ? |
---|
| 75 | cons.sum += item; |
---|
| 76 | } // for |
---|
[1033f5d] | 77 | } |
---|
[1edf37f] | 78 | void ?{}( Consumer & cons, Buffer * buffer, int * sum ) { |
---|
[9f865d1] | 79 | &cons.buffer = buffer; |
---|
| 80 | &cons.sum = sum; |
---|
[1033f5d] | 81 | } |
---|
| 82 | |
---|
| 83 | int main() { |
---|
[1edf37f] | 84 | Buffer buffer; |
---|
[9f865d1] | 85 | enum { Prods = 5, Cons = 5 }; |
---|
| 86 | Producer * prods[Prods]; |
---|
| 87 | Consumer * cons[Cons]; |
---|
| 88 | const int Sentinel = -1; |
---|
| 89 | int sums[Cons]; |
---|
| 90 | int i; |
---|
| 91 | processor p; |
---|
[1033f5d] | 92 | |
---|
[54aba8d] | 93 | //srandom( getpid() ); |
---|
| 94 | srandom( 1003 ); |
---|
[1033f5d] | 95 | |
---|
[9f865d1] | 96 | for ( i = 0; i < Cons; i += 1 ) { // create consumers |
---|
| 97 | cons[i] = new( &buffer, &sums[i] ); |
---|
| 98 | } // for |
---|
| 99 | for ( i = 0; i < Prods; i += 1 ) { // create producers |
---|
| 100 | prods[i] = new( &buffer, 100000u ); |
---|
| 101 | } // for |
---|
[1033f5d] | 102 | |
---|
[9f865d1] | 103 | for ( i = 0; i < Prods; i += 1 ) { // wait for producers to finish |
---|
| 104 | delete( prods[i] ); |
---|
| 105 | } // for |
---|
| 106 | for ( i = 0; i < Cons; i += 1 ) { // generate sentinal values to stop consumers |
---|
| 107 | insert( buffer, Sentinel ); |
---|
| 108 | } // for |
---|
| 109 | int sum = 0; |
---|
| 110 | for ( i = 0; i < Cons; i += 1 ) { // wait for consumers to finish |
---|
| 111 | delete( cons[i] ); |
---|
| 112 | sum += sums[i]; |
---|
| 113 | } // for |
---|
| 114 | sout | "total:" | sum | endl; |
---|
[1033f5d] | 115 | } |
---|
[9f865d1] | 116 | |
---|
| 117 | // Local Variables: // |
---|
| 118 | // tab-width: 4 // |
---|
| 119 | // compile-command: "cfa boundedBuffer.c" // |
---|
| 120 | // End: // |
---|