source: tests/coroutine/prodcons.c @ 73abe95

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprno_listpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since 73abe95 was 73abe95, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Replace extension-less headers with .hfa

  • Property mode set to 100644
File size: 2.3 KB
RevLine 
[73abe95]1//
[9bae71f]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.
[73abe95]6//
7// prodcons.c --
8//
[9bae71f]9// Author           : Peter A. Buhr
10// Created On       : Mon Sep 18 12:23:39 2017
11// Last Modified By : Peter A. Buhr
[54aba8d]12// Last Modified On : Tue Jan  2 12:17:01 2018
13// Update Count     : 47
[73abe95]14//
[9bae71f]15
[73abe95]16#include <fstream.hfa>
17#include <coroutine.hfa>
18#include <stdlib.hfa>                                                                           // random
[9bae71f]19#include <unistd.h>                                                                             // getpid
20
21coroutine Cons;                                                                                 // forward
22int delivery( Cons & cons, int p1, int p2 );
23void stop( Cons & cons );
24
25coroutine Prod {
[971d9f2]26        Cons * c;
[9bae71f]27        int N, money, receipt;
28};
[971d9f2]29void main( Prod & prod ) with( prod ) {                                 // starter ::main
[9bae71f]30        // 1st resume starts here
[971d9f2]31        for ( int i = 0; i < N; i += 1 ) {
[6c7b1e7]32                int p1 = random( 100 );
33                int p2 = random( 100 );
[9bae71f]34                sout | p1 | " " | p2 | endl;
[971d9f2]35                int status = delivery( *c, p1, p2 );
36                sout | " $" | money | endl;
[9bae71f]37                sout | status | endl;
[971d9f2]38                receipt += 1;
[9bae71f]39        }
[971d9f2]40        stop( *c );
[9bae71f]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 ) {}
[971d9f2]66void main( Cons & cons ) with( cons ) {                                 // starter prod
[9bae71f]67        // 1st resume starts here
68        int money = 1, receipt;
[971d9f2]69        for ( ; ! done; ) {
70                sout | p1 | " " | p2 | endl;
[9bae71f]71                sout | " $" | money | endl;
[971d9f2]72                status += 1;
73                receipt = payment( *p, money );
[9bae71f]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 };
[54aba8d]92        srandom( /* getpid() */ 103 );                                          // fixed seed for testing
[9bae71f]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.