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