// // Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // fibonacci.c -- 3-state finite-state machine // // Author : Colby Parsons // Created On : Thu July 6 07:29:37 2023 // Last Modified By : Peter A. Buhr // Last Modified On : Thu Jul 6 21:49:04 2023 // Update Count : 18 // #include #include #include exception fib_num {}; vtable(fib_num) fib_num_vt; fib_num except{ &fib_num_vt }; coroutine Fibonacci { int fn; }; // used for communication void main( Fibonacci & fib ) with( fib ) { // called on first resume int fn1, fn2; // retained between resumes try { poll( fib ); suspend; // restart last resume } catchResume ( fib_num * e ) { fn = 0; fn1 = fn; // 1st case } try { poll( fib ); suspend; // restart last resume } catchResume ( fib_num * e ) { fn = 1; fn2 = fn1; fn1 = fn; // 2nd case } try { for () { poll( fib ); suspend; // restart last resume } // for } catchResume ( fib_num * e ) { fn = fn1 + fn2; fn2 = fn1; fn1 = fn; // general case } } int main() { Fibonacci f1, f2; for ( i; 8 ) { resumeAt( f1, except ); resumeAt( f2, except ); sout | wd( 5, resume( f1 ).fn ) | wd( 5, resume( f2 ).fn ); } } // Local Variables: // // compile-command: "cfa fibonacci_nonlocal.cfa" // // End: //