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