[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 |
---|
[ae4af81] | 12 | // Last Modified On : Sat Aug 18 12:55:10 2018 |
---|
| 13 | // Update Count : 51 |
---|
[73abe95] | 14 | // |
---|
[9bae71f] | 15 | |
---|
[73abe95] | 16 | #include <fstream.hfa> |
---|
| 17 | #include <coroutine.hfa> |
---|
[ae4af81] | 18 | #include <stdlib.hfa> // random |
---|
[9bae71f] | 19 | #include <unistd.h> // getpid |
---|
| 20 | |
---|
| 21 | coroutine Cons; // forward |
---|
| 22 | int delivery( Cons & cons, int p1, int p2 ); |
---|
| 23 | void stop( Cons & cons ); |
---|
| 24 | |
---|
| 25 | coroutine Prod { |
---|
[971d9f2] | 26 | Cons * c; |
---|
[9bae71f] | 27 | int N, money, receipt; |
---|
| 28 | }; |
---|
[971d9f2] | 29 | void main( Prod & prod ) with( prod ) { // starter ::main |
---|
[9bae71f] | 30 | // 1st resume starts here |
---|
[ae4af81] | 31 | for ( i; N ) { // N pairs of values |
---|
[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 | } |
---|
| 43 | int payment( Prod & prod, int money ) { |
---|
| 44 | prod.money = money; |
---|
| 45 | resume( prod ); // main 1st time, then |
---|
| 46 | return prod.receipt; // prod in delivery |
---|
| 47 | } |
---|
| 48 | void 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 | |
---|
| 55 | coroutine Cons { |
---|
| 56 | Prod * p; |
---|
| 57 | int p1, p2, status; |
---|
| 58 | bool done; |
---|
| 59 | }; |
---|
| 60 | void ?{}( Cons & cons, Prod & p ) { |
---|
| 61 | cons.p = &p; |
---|
| 62 | cons.status = 0; |
---|
| 63 | cons.done = false; |
---|
| 64 | } |
---|
| 65 | void ^?{}( Cons & cons ) {} |
---|
[971d9f2] | 66 | void 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 | } |
---|
| 79 | int 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 | } |
---|
| 85 | void stop( Cons & cons ) { |
---|
| 86 | cons.done = true; |
---|
| 87 | resume( cons ); // activate payment |
---|
| 88 | } |
---|
| 89 | int 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: // |
---|