[bef2245] | 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 | // fibonacci.c -- 3-state finite-state machine |
---|
| 8 | // |
---|
| 9 | // Author : Colby Parsons |
---|
| 10 | // Created On : Thu July 6 07:29:37 2023 |
---|
[5186775] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Thu Jul 6 21:49:04 2023 |
---|
| 13 | // Update Count : 18 |
---|
[bef2245] | 14 | // |
---|
| 15 | |
---|
| 16 | #include <fstream.hfa> |
---|
| 17 | #include <coroutine.hfa> |
---|
| 18 | #include <stdlib.hfa> |
---|
| 19 | |
---|
[5186775] | 20 | exception fib_num {}; |
---|
[bef2245] | 21 | vtable(fib_num) fib_num_vt; |
---|
[5186775] | 22 | fib_num except{ &fib_num_vt }; |
---|
[bef2245] | 23 | |
---|
| 24 | coroutine Fibonacci { int fn; }; // used for communication |
---|
| 25 | |
---|
| 26 | void main( Fibonacci & fib ) with( fib ) { // called on first resume |
---|
[5186775] | 27 | int fn1, fn2; // retained between resumes |
---|
| 28 | try { |
---|
| 29 | poll( fib ); |
---|
| 30 | suspend; // restart last resume |
---|
| 31 | } catchResume ( fib_num * e ) { |
---|
| 32 | fn = 0; fn1 = fn; // 1st case |
---|
| 33 | } |
---|
| 34 | try { |
---|
| 35 | poll( fib ); |
---|
| 36 | suspend; // restart last resume |
---|
| 37 | } catchResume ( fib_num * e ) { |
---|
| 38 | fn = 1; fn2 = fn1; fn1 = fn; // 2nd case |
---|
| 39 | } |
---|
| 40 | try { |
---|
| 41 | for () { |
---|
| 42 | poll( fib ); |
---|
| 43 | suspend; // restart last resume |
---|
| 44 | } // for |
---|
| 45 | } catchResume ( fib_num * e ) { |
---|
| 46 | fn = fn1 + fn2; fn2 = fn1; fn1 = fn; // general case |
---|
| 47 | } |
---|
[bef2245] | 48 | } |
---|
| 49 | |
---|
| 50 | int main() { |
---|
| 51 | Fibonacci f1, f2; |
---|
[5186775] | 52 | for ( i; 8 ) { |
---|
| 53 | resumeAt( f1, except ); resumeAt( f2, except ); |
---|
| 54 | sout | wd( 5, resume( f1 ).fn ) | wd( 5, resume( f2 ).fn ); |
---|
| 55 | } |
---|
[bef2245] | 56 | } |
---|
| 57 | |
---|
| 58 | // Local Variables: // |
---|
[5186775] | 59 | // compile-command: "cfa fibonacci_nonlocal.cfa" // |
---|
[bef2245] | 60 | // End: // |
---|