source: tests/concurrent/examples/boundedBufferINT.cfa @ 4487667

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 4487667 was f8cd310, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

update file names in emacs compile directive

  • 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
[f8cd310]12// Last Modified On : Fri Mar 22 13:41:52 2019
13// Update Count     : 88
[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
[623c16a]26forall( otype T ) {
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
[58caf150]55const int Sentinel = -1;
56
[1033f5d]57thread Producer {
[58caf150]58        Buffer(int) & buffer;
[9f865d1]59        unsigned int N;
[1033f5d]60};
[58caf150]61void main( Producer & prod ) with( prod ) {
62        for ( int i = 1; i <= N; i += 1 ) {
[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
[58caf150]102        for ( i = 0; i < Cons; i += 1 ) {                                       // create consumers
103                cons[i] = new( &buffer, sums[i] );
[9f865d1]104        } // for
[58caf150]105        for ( i = 0; i < Prods; i += 1 ) {                                      // create producers
106                prods[i] = new( &buffer, 100000 );
[9f865d1]107        } // for
[1033f5d]108
[58caf150]109        for ( i = 0; i < Prods; i += 1 ) {                                      // wait for producers to finish
[9f865d1]110                delete( prods[i] );
111        } // for
[58caf150]112        for ( i = 0; i < Cons; i += 1 ) {                                       // generate sentinal values to stop consumers
[9f865d1]113                insert( buffer, Sentinel );
114        } // for
115        int sum = 0;
[58caf150]116        for ( i = 0; i < Cons; i += 1 ) {                                       // 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.