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