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