source: tests/concurrent/examples/boundedBufferEXT.cfa @ 80a9898

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 80a9898 was 107b01a, checked in by Thierry Delisle <tdelisle@…>, 6 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// 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 : Tue Dec 11 21:55:02 2018
11// Update Count     : 9
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                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
52const int Sentinel = -1;
53
54thread Producer {
55        Buffer(int) & buffer;
56        unsigned int N;
57};
58void 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}
64void ?{}( Producer & prod, Buffer(int) * buffer, int N ) {
65        &prod.buffer = buffer;
66        prod.N = N;
67}
68
69thread Consumer {
70        Buffer(int) & buffer;
71        int & sum;                                                                                      // summation of producer values
72};
73void 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}
82void ?{}( Consumer & cons, Buffer(int) * buffer, int & sum ) {
83        &cons.buffer = buffer;
84        &cons.sum = &sum;
85}
86
87int 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;
118}
119
120// Local Variables: //
121// tab-width: 4 //
122// compile-command: "cfa boundedBufferEXT.c" //
123// End: //
Note: See TracBrowser for help on using the repository browser.