source: src/tests/concurrent/examples/boundedBufferEXT.c@ a02842f

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since a02842f was b9f383f, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

named threads in thread tests
fixed bounded buffer expect
added error check for waitfor with no parameters
added missing parameters to bounded buffer EXT

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