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