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 : Wed Dec 12 23:04:49 2018 |
---|
13 | // Update Count : 53 |
---|
14 | // |
---|
15 | |
---|
16 | #include <fstream.hfa> |
---|
17 | #include <coroutine.hfa> |
---|
18 | #include <stdlib.hfa> // random |
---|
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 { |
---|
26 | Cons & c; |
---|
27 | int N, money, receipt; |
---|
28 | }; |
---|
29 | void main( Prod & prod ) with( prod ) { // starter ::main |
---|
30 | // 1st resume starts here |
---|
31 | for ( i; N ) { // N pairs of values |
---|
32 | int p1 = random( 100 ), p2 = random( 100 ); |
---|
33 | sout | p1 | " " | p2; |
---|
34 | int status = delivery( c, p1, p2 ); |
---|
35 | sout | " $" | money | nl | status; |
---|
36 | receipt += 1; |
---|
37 | } |
---|
38 | stop( c ); |
---|
39 | sout | "prod stops"; |
---|
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 ) { |
---|
47 | &prod.c = &c; |
---|
48 | prod.[N, receipt] = [N, 0]; |
---|
49 | resume( prod ); // activate main |
---|
50 | } |
---|
51 | |
---|
52 | coroutine Cons { |
---|
53 | Prod & p; |
---|
54 | int p1, p2, status; |
---|
55 | bool done; |
---|
56 | }; |
---|
57 | void ?{}( Cons & cons, Prod & p ) { |
---|
58 | &cons.p = &p; |
---|
59 | cons.[status, done ] = [0, false]; |
---|
60 | } |
---|
61 | void ^?{}( Cons & cons ) {} |
---|
62 | void main( Cons & cons ) with( cons ) { // starter prod |
---|
63 | // 1st resume starts here |
---|
64 | int money = 1, receipt; |
---|
65 | for ( ; ! done; ) { |
---|
66 | sout | p1 | " " | p2 | nl | " $" | money; |
---|
67 | status += 1; |
---|
68 | receipt = payment( p, money ); |
---|
69 | sout | " #" | receipt; |
---|
70 | money += 1; |
---|
71 | } |
---|
72 | sout | "cons stops"; |
---|
73 | } |
---|
74 | int delivery( Cons & cons, int p1, int p2 ) { |
---|
75 | cons.[p1, p2] = [p1, p2]; |
---|
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 }; |
---|
86 | srandom( /* getpid() */ 103 ); // fixed seed for testing |
---|
87 | start( prod, 5, cons ); |
---|
88 | sout | "main stops"; |
---|
89 | } |
---|
90 | |
---|
91 | // Local Variables: // |
---|
92 | // tab-width: 4 // |
---|
93 | // compile-command: "cfa prodcons.c" // |
---|
94 | // End: // |
---|