source: src/tests/concurrent/examples/boundedBufferINT.c@ a02842f

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since a02842f was 58caf150, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

updates

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