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