source: tests/concurrent/examples/boundedBufferINT.cfa @ 474d610

arm-ehenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-expr
Last change on this file since 474d610 was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 16 months 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
RevLine 
[4cedd9f]1//
[2e457d8]2// Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo
3//
[9f865d1]4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
[4cedd9f]6//
7// boundedBuffer.c --
8//
[9f865d1]9// Author           : Peter A. Buhr
10// Created On       : Mon Oct 30 12:45:13 2017
11// Last Modified By : Peter A. Buhr
[3aa1d22]12// Last Modified On : Fri Jun 21 08:20:46 2019
13// Update Count     : 90
[4cedd9f]14//
[9f865d1]15
[2e457d8]16#include <stdlib.hfa>                                                                   // random
[73abe95]17#include <fstream.hfa>
18#include <kernel.hfa>
19#include <thread.hfa>
[6c7b1e7]20#include <unistd.h>                                                                             // getpid
[1033f5d]21
[58caf150]22//Duration default_preemption() { return 0; }
23
24enum { BufferSize = 50 };
25
[fd54fef]26forall( T ) {
[623c16a]27        monitor Buffer {
28                condition full, empty;
29                int front, back, count;
30                T elements[BufferSize];
31        }; // Buffer
[1033f5d]32
[623c16a]33        void ?{}( Buffer(T) & buffer ) with( buffer ) { [front, back, count] = 0; }
[1033f5d]34
[623c16a]35        int query( Buffer(T) & buffer ) { return buffer.count; } // read-only, no mutual exclusion
[1033f5d]36
[623c16a]37        void insert( Buffer(T) & mutex buffer, T elem ) with( buffer ) {
38                if ( count == BufferSize ) wait( empty );
39                elements[back] = elem;
40                back = ( back + 1 ) % BufferSize;
41                count += 1;
42                signal( full );
43        } // insert
[a1ecdd1]44
[623c16a]45        T remove( Buffer(T) & mutex buffer ) with( buffer ) {
46                if ( count == 0 ) wait( full );
47                T elem = elements[front];
48                front = ( front + 1 ) % BufferSize;
49                count -= 1;
50                signal( empty );
51                return elem;
52        } // remove
[1033f5d]53}
54
[3aa1d22]55enum { Sentinel = -1 };
[58caf150]56
[1033f5d]57thread Producer {
[58caf150]58        Buffer(int) & buffer;
[9f865d1]59        unsigned int N;
[1033f5d]60};
[58caf150]61void main( Producer & prod ) with( prod ) {
[3aa1d22]62        for ( i; 1 ~= N ) {
[6c7b1e7]63                yield( random( 5 ) );
[58caf150]64                insert( buffer, 1 );
[9f865d1]65        } // for
[1033f5d]66}
[58caf150]67void ?{}( Producer & prod, Buffer(int) * buffer, int N ) {
[9f865d1]68        &prod.buffer = buffer;
69        prod.N = N;
[a1ecdd1]70}
[1033f5d]71
72thread Consumer {
[58caf150]73        Buffer(int) & buffer;
74        int & sum;                                                                                      // summation of producer values
[1033f5d]75};
[58caf150]76void main( Consumer & cons ) with( cons ) {
77        sum = 0;
[adb6b30f]78        for () {
[6c7b1e7]79                yield( random( 5 ) );
[58caf150]80                int item = remove( buffer );
81          if ( item == Sentinel ) break;                                        // sentinel ?
82                sum += item;
[9f865d1]83        } // for
[1033f5d]84}
[58caf150]85void ?{}( Consumer & cons, Buffer(int) * buffer, int & sum ) {
[9f865d1]86        &cons.buffer = buffer;
[58caf150]87        &cons.sum = &sum;
[1033f5d]88}
89
90int main() {
[58caf150]91        Buffer(int) buffer;
92        enum { Prods = 4, Cons = 5 };
[9f865d1]93        Producer * prods[Prods];
94        Consumer * cons[Cons];
95        int sums[Cons];
96        int i;
97        processor p;
[1033f5d]98
[54aba8d]99        //srandom( getpid() );
100        srandom( 1003 );
[1033f5d]101
[3aa1d22]102        for ( i; Cons ) {                                                                       // create consumers
[58caf150]103                cons[i] = new( &buffer, sums[i] );
[9f865d1]104        } // for
[3aa1d22]105        for ( i; Prods ) {                                                                      // create producers
[58caf150]106                prods[i] = new( &buffer, 100000 );
[9f865d1]107        } // for
[1033f5d]108
[3aa1d22]109        for ( i; Prods ) {                                                                      // wait for producers to finish
[9f865d1]110                delete( prods[i] );
111        } // for
[3aa1d22]112        for ( i; Cons ) {                                                                       // generate sentinal values to stop consumers
[9f865d1]113                insert( buffer, Sentinel );
114        } // for
115        int sum = 0;
[3aa1d22]116        for ( i; Cons ) {                                                                       // wait for consumers to finish
[9f865d1]117                delete( cons[i] );
118                sum += sums[i];
119        } // for
[200fcb3]120        sout | "total:" | sum;
[1033f5d]121}
[9f865d1]122
123// Local Variables: //
124// tab-width: 4 //
[f8cd310]125// compile-command: "cfa boundedBufferINT.cfa" //
[9f865d1]126// End: //
Note: See TracBrowser for help on using the repository browser.