[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
|
---|
[6c7b1e7] | 10 | // Last Modified On : Mon Oct 30 23:02:46 2017
|
---|
| 11 | // Update Count : 9
|
---|
[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;
|
---|
| 23 | int elements[20];
|
---|
[1033f5d] | 24 | };
|
---|
| 25 |
|
---|
| 26 | void ?{}( Buffer & buffer ) {
|
---|
[9f865d1] | 27 | buffer.front = buffer.back = buffer.count = 0;
|
---|
[1033f5d] | 28 | }
|
---|
| 29 |
|
---|
| 30 | int query( Buffer & buffer ) { return buffer.count; }
|
---|
| 31 |
|
---|
| 32 | void insert( Buffer & mutex buffer, int elem ) {
|
---|
[4cedd9f] | 33 | if ( buffer.count == 20 ) wait( buffer.empty );
|
---|
[9f865d1] | 34 | buffer.elements[buffer.back] = elem;
|
---|
| 35 | buffer.back = ( buffer.back + 1 ) % 20;
|
---|
| 36 | buffer.count += 1;
|
---|
[4cedd9f] | 37 | signal( buffer.full );
|
---|
[1033f5d] | 38 | }
|
---|
| 39 | int remove( Buffer & mutex buffer ) {
|
---|
[4cedd9f] | 40 | if ( buffer.count == 0 ) wait( buffer.full );
|
---|
[9f865d1] | 41 | int elem = buffer.elements[buffer.front];
|
---|
| 42 | buffer.front = ( buffer.front + 1 ) % 20;
|
---|
| 43 | buffer.count -= 1;
|
---|
[4cedd9f] | 44 | signal( buffer.empty );
|
---|
[9f865d1] | 45 | return elem;
|
---|
[1033f5d] | 46 | }
|
---|
| 47 |
|
---|
| 48 | thread Producer {
|
---|
[9f865d1] | 49 | Buffer & buffer;
|
---|
| 50 | unsigned int N;
|
---|
[1033f5d] | 51 | };
|
---|
| 52 | void main( Producer & prod ) {
|
---|
[9f865d1] | 53 | for ( int i = 1; i <= prod.N; i += 1 ) {
|
---|
[6c7b1e7] | 54 | yield( random( 5 ) );
|
---|
[9f865d1] | 55 | insert( prod.buffer, 1 );
|
---|
| 56 | } // for
|
---|
| 57 | insert( prod.buffer, -1 );
|
---|
[1033f5d] | 58 | }
|
---|
| 59 | void ?{}( Producer & prod, Buffer * buffer, unsigned int N ) {
|
---|
[9f865d1] | 60 | &prod.buffer = buffer;
|
---|
| 61 | prod.N = N;
|
---|
| 62 | }
|
---|
[1033f5d] | 63 |
|
---|
| 64 | thread Consumer {
|
---|
[9f865d1] | 65 | Buffer & buffer;
|
---|
| 66 | int & sum; // summation of producer values
|
---|
[1033f5d] | 67 | };
|
---|
| 68 | void main( Consumer & cons ) {
|
---|
[9f865d1] | 69 | cons.sum = 0;
|
---|
| 70 | for ( ;; ) {
|
---|
[6c7b1e7] | 71 | yield( random( 5 ) );
|
---|
[9f865d1] | 72 | int item = remove( cons.buffer );
|
---|
| 73 | if ( item == -1 ) break; // sentinel ?
|
---|
| 74 | cons.sum += item;
|
---|
| 75 | } // for
|
---|
[1033f5d] | 76 | }
|
---|
| 77 | void ?{}( Consumer & cons, Buffer * buffer, int * sum ) {
|
---|
[9f865d1] | 78 | &cons.buffer = buffer;
|
---|
| 79 | &cons.sum = sum;
|
---|
[1033f5d] | 80 | }
|
---|
| 81 |
|
---|
| 82 | int main() {
|
---|
[9f865d1] | 83 | Buffer buffer;
|
---|
| 84 | enum { Prods = 5, Cons = 5 };
|
---|
| 85 | Producer * prods[Prods];
|
---|
| 86 | Consumer * cons[Cons];
|
---|
| 87 | const int Sentinel = -1;
|
---|
| 88 | int sums[Cons];
|
---|
| 89 | int i;
|
---|
| 90 | processor p;
|
---|
[1033f5d] | 91 |
|
---|
[6c7b1e7] | 92 | //random_seed( getpid() );
|
---|
| 93 | random_seed( 1003 );
|
---|
[1033f5d] | 94 |
|
---|
[9f865d1] | 95 | for ( i = 0; i < Cons; i += 1 ) { // create consumers
|
---|
| 96 | cons[i] = new( &buffer, &sums[i] );
|
---|
| 97 | } // for
|
---|
| 98 | for ( i = 0; i < Prods; i += 1 ) { // create producers
|
---|
| 99 | prods[i] = new( &buffer, 100000u );
|
---|
| 100 | } // for
|
---|
[1033f5d] | 101 |
|
---|
[9f865d1] | 102 | for ( i = 0; i < Prods; i += 1 ) { // wait for producers to finish
|
---|
| 103 | delete( prods[i] );
|
---|
| 104 | } // for
|
---|
| 105 | for ( i = 0; i < Cons; i += 1 ) { // generate sentinal values to stop consumers
|
---|
| 106 | insert( buffer, Sentinel );
|
---|
| 107 | } // for
|
---|
| 108 | int sum = 0;
|
---|
| 109 | for ( i = 0; i < Cons; i += 1 ) { // wait for consumers to finish
|
---|
| 110 | delete( cons[i] );
|
---|
| 111 | sum += sums[i];
|
---|
| 112 | } // for
|
---|
| 113 | sout | "total:" | sum | endl;
|
---|
[1033f5d] | 114 | }
|
---|
[9f865d1] | 115 |
|
---|
| 116 | // Local Variables: //
|
---|
| 117 | // tab-width: 4 //
|
---|
| 118 | // compile-command: "cfa boundedBuffer.c" //
|
---|
| 119 | // End: //
|
---|