[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 |
---|
[58caf150] | 10 | // Last Modified On : Fri Apr 20 22:25:14 2018 |
---|
| 11 | // Update Count : 6 |
---|
[88f15ae] | 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 | |
---|
[58caf150] | 22 | enum { BufferSize = 50 }; |
---|
[88f15ae] | 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 | |
---|
[58caf150] | 36 | forall( otype T ) // forward |
---|
| 37 | T remove( Buffer(T) & mutex buffer ); |
---|
| 38 | |
---|
[88f15ae] | 39 | forall( otype T ) |
---|
| 40 | void insert( Buffer(T) & mutex buffer, T elem ) with( buffer ) { |
---|
| 41 | if ( count == BufferSize ) waitfor( remove ); |
---|
| 42 | elements[back] = elem; |
---|
| 43 | back = ( back + 1 ) % BufferSize; |
---|
| 44 | count += 1; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | forall( otype T ) |
---|
| 48 | T remove( Buffer(T) & mutex buffer ) with( buffer ) { |
---|
| 49 | if ( count == 0 ) waitfor( insert ); |
---|
| 50 | T elem = elements[front]; |
---|
| 51 | front = ( front + 1 ) % BufferSize; |
---|
| 52 | count -= 1; |
---|
| 53 | return elem; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | const int Sentinel = -1; |
---|
| 57 | |
---|
| 58 | thread Producer { |
---|
| 59 | Buffer(int) & buffer; |
---|
| 60 | unsigned int N; |
---|
| 61 | }; |
---|
| 62 | void main( Producer & prod ) with( prod ) { |
---|
| 63 | for ( int i = 1; i <= N; i += 1 ) { |
---|
| 64 | yield( random( 5 ) ); |
---|
| 65 | insert( buffer, 1 ); |
---|
| 66 | } // for |
---|
| 67 | } |
---|
| 68 | void ?{}( Producer & prod, Buffer(int) * buffer, int N ) { |
---|
| 69 | &prod.buffer = buffer; |
---|
| 70 | prod.N = N; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | thread Consumer { |
---|
| 74 | Buffer(int) & buffer; |
---|
| 75 | int & sum; // summation of producer values |
---|
| 76 | }; |
---|
| 77 | void main( Consumer & cons ) with( cons ) { |
---|
| 78 | sum = 0; |
---|
| 79 | for ( ;; ) { |
---|
| 80 | yield( random( 5 ) ); |
---|
| 81 | int item = remove( buffer ); |
---|
| 82 | if ( item == Sentinel ) break; // sentinel ? |
---|
| 83 | sum += item; |
---|
| 84 | } // for |
---|
| 85 | } |
---|
| 86 | void ?{}( Consumer & cons, Buffer(int) * buffer, int & sum ) { |
---|
| 87 | &cons.buffer = buffer; |
---|
| 88 | &cons.sum = ∑ |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | int main() { |
---|
| 92 | Buffer(int) buffer; |
---|
| 93 | enum { Prods = 4, Cons = 5 }; |
---|
| 94 | Producer * prods[Prods]; |
---|
| 95 | Consumer * cons[Cons]; |
---|
| 96 | int sums[Cons]; |
---|
| 97 | int i; |
---|
| 98 | processor p; |
---|
| 99 | |
---|
| 100 | //srandom( getpid() ); |
---|
| 101 | srandom( 1003 ); |
---|
| 102 | |
---|
| 103 | for ( i = 0; i < Cons; i += 1 ) { // create consumers |
---|
| 104 | cons[i] = new( &buffer, sums[i] ); |
---|
| 105 | } // for |
---|
| 106 | for ( i = 0; i < Prods; i += 1 ) { // create producers |
---|
| 107 | prods[i] = new( &buffer, 100000 ); |
---|
| 108 | } // for |
---|
| 109 | |
---|
| 110 | for ( i = 0; i < Prods; i += 1 ) { // wait for producers to finish |
---|
| 111 | delete( prods[i] ); |
---|
| 112 | } // for |
---|
| 113 | for ( i = 0; i < Cons; i += 1 ) { // generate sentinal values to stop consumers |
---|
| 114 | insert( buffer, Sentinel ); |
---|
| 115 | } // for |
---|
| 116 | int sum = 0; |
---|
| 117 | for ( i = 0; i < Cons; i += 1 ) { // wait for consumers to finish |
---|
| 118 | delete( cons[i] ); |
---|
| 119 | sum += sums[i]; |
---|
| 120 | } // for |
---|
| 121 | sout | "total:" | sum | endl; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | // Local Variables: // |
---|
| 125 | // tab-width: 4 // |
---|
| 126 | // compile-command: "cfa boundedBufferEXT.c" // |
---|
| 127 | // End: // |
---|