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