source: tests/concurrent/examples/boundedBufferEXT.cfa @ c8238c0

ADTast-experimental
Last change on this file since c8238c0 was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 4 years ago

Converting the project to use the new syntax for otype, dtype and ttytpe.

Changed prelude (gen), libcfa and test suite to use it. Added a simple deprecation rule of the old syntax to the parser; we might wish to support both syntaxes "officially," like with an extra CLI switch, but this measure should serve as a simple reminder for our team to try the new syntax.

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