[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 |
---|
[f8cd310] | 12 | // Last Modified On : Fri Mar 22 13:41:10 2019 |
---|
| 13 | // Update Count : 54 |
---|
[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 { |
---|
[18cf979] | 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 |
---|
[18cf979] | 32 | int p1 = random( 100 ), p2 = random( 100 ); |
---|
[200fcb3] | 33 | sout | p1 | " " | p2; |
---|
[18cf979] | 34 | int status = delivery( c, p1, p2 ); |
---|
| 35 | sout | " $" | money | nl | status; |
---|
[971d9f2] | 36 | receipt += 1; |
---|
[9bae71f] | 37 | } |
---|
[18cf979] | 38 | stop( c ); |
---|
[200fcb3] | 39 | sout | "prod stops"; |
---|
[9bae71f] | 40 | } |
---|
| 41 | int payment( Prod & prod, int money ) { |
---|
| 42 | prod.money = money; |
---|
| 43 | resume( prod ); // main 1st time, then |
---|
| 44 | return prod.receipt; // prod in delivery |
---|
| 45 | } |
---|
| 46 | void start( Prod & prod, int N, Cons &c ) { |
---|
[18cf979] | 47 | &prod.c = &c; |
---|
| 48 | prod.[N, receipt] = [N, 0]; |
---|
[9bae71f] | 49 | resume( prod ); // activate main |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | coroutine Cons { |
---|
[18cf979] | 53 | Prod & p; |
---|
[9bae71f] | 54 | int p1, p2, status; |
---|
| 55 | bool done; |
---|
| 56 | }; |
---|
| 57 | void ?{}( Cons & cons, Prod & p ) { |
---|
[18cf979] | 58 | &cons.p = &p; |
---|
| 59 | cons.[status, done ] = [0, false]; |
---|
[9bae71f] | 60 | } |
---|
| 61 | void ^?{}( Cons & cons ) {} |
---|
[971d9f2] | 62 | void main( Cons & cons ) with( cons ) { // starter prod |
---|
[9bae71f] | 63 | // 1st resume starts here |
---|
| 64 | int money = 1, receipt; |
---|
[971d9f2] | 65 | for ( ; ! done; ) { |
---|
[18cf979] | 66 | sout | p1 | " " | p2 | nl | " $" | money; |
---|
[971d9f2] | 67 | status += 1; |
---|
[18cf979] | 68 | receipt = payment( p, money ); |
---|
[200fcb3] | 69 | sout | " #" | receipt; |
---|
[9bae71f] | 70 | money += 1; |
---|
| 71 | } |
---|
[200fcb3] | 72 | sout | "cons stops"; |
---|
[9bae71f] | 73 | } |
---|
| 74 | int delivery( Cons & cons, int p1, int p2 ) { |
---|
[18cf979] | 75 | cons.[p1, p2] = [p1, p2]; |
---|
[9bae71f] | 76 | resume( cons ); // main 1st time, then |
---|
| 77 | return cons.status; // cons in payment |
---|
| 78 | } |
---|
| 79 | void stop( Cons & cons ) { |
---|
| 80 | cons.done = true; |
---|
| 81 | resume( cons ); // activate payment |
---|
| 82 | } |
---|
| 83 | int main() { |
---|
| 84 | Prod prod; |
---|
| 85 | Cons cons = { prod }; |
---|
[54aba8d] | 86 | srandom( /* getpid() */ 103 ); // fixed seed for testing |
---|
[9bae71f] | 87 | start( prod, 5, cons ); |
---|
[200fcb3] | 88 | sout | "main stops"; |
---|
[9bae71f] | 89 | } |
---|
| 90 | |
---|
| 91 | // Local Variables: // |
---|
| 92 | // tab-width: 4 // |
---|
[f8cd310] | 93 | // compile-command: "cfa prodcons.cfa" // |
---|
[9bae71f] | 94 | // End: // |
---|