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 Sep 20 17:03:28 2017
|
---|
13 | // Update Count : 40
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <fstream>
|
---|
17 | #include <coroutine>
|
---|
18 | #include <stdlib> // rand48
|
---|
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 ) { // starter ::main
|
---|
30 | // 1st resume starts here
|
---|
31 | for ( int i = 0; i < prod.N; i += 1 ) {
|
---|
32 | int p1 = (unsigned int)rand48() % 100; // non-negative
|
---|
33 | int p2 = (unsigned int)rand48() % 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 | }
|
---|
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 ) {}
|
---|
66 | void 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 | }
|
---|
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 };
|
---|
92 | rand48seed( /* 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: //
|
---|