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