source: src/tests/concurrent/examples/boundedBufferINT.c @ da7fe39

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since da7fe39 was 58caf150, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

updates

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