source: src/tests/prodcons.c @ 0873968c

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

change random name

  • Property mode set to 100644
File size: 2.4 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2017 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// prodcons.c --
8//
9// Author           : Peter A. Buhr
10// Created On       : Mon Sep 18 12:23:39 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Oct 30 23:06:05 2017
13// Update Count     : 42
14//
15
16#include <fstream>
17#include <coroutine>
18#include <stdlib>                                                                               // random
19#include <unistd.h>                                                                             // getpid
20
21coroutine Cons;                                                                                 // forward
22int delivery( Cons & cons, int p1, int p2 );
23void stop( Cons & cons );
24
25coroutine Prod {
26        Cons *c;
27        int N, money, receipt;
28};
29void main( Prod & prod ) {                                                              // starter ::main
30        // 1st resume starts here
31        for ( int i = 0; i < prod.N; i += 1 ) {
32                int p1 = random( 100 );
33                int p2 = random( 100 );
34                sout | p1 | " " | p2 | endl;
35                int status = delivery( *prod.c, p1, p2 );
36                sout | " $" | prod.money | endl;
37                sout | status | endl;
38                prod.receipt += 1;
39        }
40        stop( *prod.c );
41        sout | "prod stops" | endl;
42}
43int payment( Prod & prod, int money ) {
44        prod.money = money;
45        resume( prod );                                                                         // main 1st time, then
46        return prod.receipt;                                                            // prod in delivery
47}
48void start( Prod & prod, int N, Cons &c ) {
49        prod.N = N;
50        prod.c = &c;
51        prod.receipt = 0;
52        resume( prod );                                                                         // activate main
53}
54
55coroutine Cons {
56        Prod * p;
57        int p1, p2, status;
58        bool done;
59};
60void ?{}( Cons & cons, Prod & p ) {
61        cons.p = &p;
62        cons.status = 0;
63        cons.done = false;
64}
65void ^?{}( Cons & cons ) {}
66void main( Cons & cons ) {                                                              // starter prod
67        // 1st resume starts here
68        int money = 1, receipt;
69        for ( ; ! cons.done; ) {
70                sout | cons.p1 | " " | cons.p2 | endl;
71                sout | " $" | money | endl;
72                cons.status += 1;
73                receipt = payment( *cons.p, money );
74                sout | " #" | receipt | endl;
75                money += 1;
76        }
77        sout | "cons stops" | endl;
78}
79int delivery( Cons & cons, int p1, int p2 ) {
80        cons.p1 = p1;
81        cons.p2 = p2;
82        resume( cons );                                                                         // main 1st time, then
83        return cons.status;                                                                     // cons in payment
84}
85void stop( Cons & cons ) {
86        cons.done = true;
87        resume( cons );                                                                         // activate payment
88}
89int main() {
90        Prod prod;
91        Cons cons = { prod };
92        random_seed( /* getpid() */ 103 );                                      // fixed seed for testing
93        start( prod, 5, cons );
94        sout | "main stops" | endl;
95}
96
97// Local Variables: //
98// tab-width: 4 //
99// compile-command: "cfa prodcons.c" //
100// End: //
Note: See TracBrowser for help on using the repository browser.