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 : Mon Oct 30 23:02:46 2017 |
---|
11 | // Update Count : 9 |
---|
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 ) { |
---|
33 | if ( buffer.count == 20 ) wait( &buffer.empty ); |
---|
34 | buffer.elements[buffer.back] = elem; |
---|
35 | buffer.back = ( buffer.back + 1 ) % 20; |
---|
36 | buffer.count += 1; |
---|
37 | signal( &buffer.full ); |
---|
38 | } |
---|
39 | int remove( Buffer & mutex buffer ) { |
---|
40 | if ( buffer.count == 0 ) wait( &buffer.full ); |
---|
41 | int elem = buffer.elements[buffer.front]; |
---|
42 | buffer.front = ( buffer.front + 1 ) % 20; |
---|
43 | buffer.count -= 1; |
---|
44 | signal( &buffer.empty ); |
---|
45 | return elem; |
---|
46 | } |
---|
47 | |
---|
48 | thread Producer { |
---|
49 | Buffer & buffer; |
---|
50 | unsigned int N; |
---|
51 | }; |
---|
52 | void main( Producer & prod ) { |
---|
53 | for ( int i = 1; i <= prod.N; i += 1 ) { |
---|
54 | yield( random( 5 ) ); |
---|
55 | insert( prod.buffer, 1 ); |
---|
56 | } // for |
---|
57 | insert( prod.buffer, -1 ); |
---|
58 | } |
---|
59 | void ?{}( Producer & prod, Buffer * buffer, unsigned int N ) { |
---|
60 | &prod.buffer = buffer; |
---|
61 | prod.N = N; |
---|
62 | } |
---|
63 | |
---|
64 | thread Consumer { |
---|
65 | Buffer & buffer; |
---|
66 | int & sum; // summation of producer values |
---|
67 | }; |
---|
68 | void main( Consumer & cons ) { |
---|
69 | cons.sum = 0; |
---|
70 | for ( ;; ) { |
---|
71 | yield( random( 5 ) ); |
---|
72 | int item = remove( cons.buffer ); |
---|
73 | if ( item == -1 ) break; // sentinel ? |
---|
74 | cons.sum += item; |
---|
75 | } // for |
---|
76 | } |
---|
77 | void ?{}( Consumer & cons, Buffer * buffer, int * sum ) { |
---|
78 | &cons.buffer = buffer; |
---|
79 | &cons.sum = sum; |
---|
80 | } |
---|
81 | |
---|
82 | int main() { |
---|
83 | Buffer buffer; |
---|
84 | enum { Prods = 5, Cons = 5 }; |
---|
85 | Producer * prods[Prods]; |
---|
86 | Consumer * cons[Cons]; |
---|
87 | const int Sentinel = -1; |
---|
88 | int sums[Cons]; |
---|
89 | int i; |
---|
90 | processor p; |
---|
91 | |
---|
92 | //random_seed( getpid() ); |
---|
93 | random_seed( 1003 ); |
---|
94 | |
---|
95 | for ( i = 0; i < Cons; i += 1 ) { // create consumers |
---|
96 | cons[i] = new( &buffer, &sums[i] ); |
---|
97 | } // for |
---|
98 | for ( i = 0; i < Prods; i += 1 ) { // create producers |
---|
99 | prods[i] = new( &buffer, 100000u ); |
---|
100 | } // for |
---|
101 | |
---|
102 | for ( i = 0; i < Prods; i += 1 ) { // wait for producers to finish |
---|
103 | delete( prods[i] ); |
---|
104 | } // for |
---|
105 | for ( i = 0; i < Cons; i += 1 ) { // generate sentinal values to stop consumers |
---|
106 | insert( buffer, Sentinel ); |
---|
107 | } // for |
---|
108 | int sum = 0; |
---|
109 | for ( i = 0; i < Cons; i += 1 ) { // wait for consumers to finish |
---|
110 | delete( cons[i] ); |
---|
111 | sum += sums[i]; |
---|
112 | } // for |
---|
113 | sout | "total:" | sum | endl; |
---|
114 | } |
---|
115 | |
---|
116 | // Local Variables: // |
---|
117 | // tab-width: 4 // |
---|
118 | // compile-command: "cfa boundedBuffer.c" // |
---|
119 | // End: // |
---|