source: tests/concurrent/examples/boundedBufferINT.cfa @ 107b01a

aaron-thesisarm-ehcleanup-dtorsdeferred_resnjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexer
Last change on this file since 107b01a was 107b01a, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Several changes to the makefiles

  • change .c tests to .cfa
  • add require for libtool in configure
  • libtoolize to fix some warnings
  • Property mode set to 100644
File size: 2.9 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 : Tue Dec 11 21:55:45 2018
11// Update Count     : 84
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
22enum { BufferSize = 50 };
23
24forall( 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
53const int Sentinel = -1;
54
55thread Producer {
56        Buffer(int) & buffer;
57        unsigned int N;
58};
59void 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}
65void ?{}( Producer & prod, Buffer(int) * buffer, int N ) {
66        &prod.buffer = buffer;
67        prod.N = N;
68}
69
70thread Consumer {
71        Buffer(int) & buffer;
72        int & sum;                                                                                      // summation of producer values
73};
74void 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}
83void ?{}( Consumer & cons, Buffer(int) * buffer, int & sum ) {
84        &cons.buffer = buffer;
85        &cons.sum = &sum;
86}
87
88int 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;
119}
120
121// Local Variables: //
122// tab-width: 4 //
123// compile-command: "cfa boundedBufferINT.c" //
124// End: //
Note: See TracBrowser for help on using the repository browser.