source:
src/tests/coroutine/fibonacci.c
@
cdc4d43
Last change on this file since cdc4d43 was 71b50f36, checked in by , 7 years ago | |
---|---|
|
|
File size: 1.3 KB |
Rev | Line | |
---|---|---|
[242a902] | 1 | // |
[ec95d11] | 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. | |
[242a902] | 6 | // |
[71b50f36] | 7 | // fibonacci.c -- 3-state finite-state machine |
8 | ||
[242a902] | 9 | // |
[ec95d11] | 10 | // Author : Thierry Delisle |
11 | // Created On : Thu Jun 8 07:29:37 2017 | |
12 | // Last Modified By : Peter A. Buhr | |
[71b50f36] | 13 | // Last Modified On : Fri Apr 27 08:55:31 2018 |
14 | // Update Count : 19 | |
[242a902] | 15 | // |
[ec95d11] | 16 | |
[c15b805] | 17 | #include <fstream> |
[4a3334cf] | 18 | #include <coroutine> |
[c15b805] | 19 | |
[971d9f2] | 20 | coroutine Fibonacci { int fn; }; // used for communication |
[c15b805] | 21 | |
[71b50f36] | 22 | void main( Fibonacci & fib ) with( fib ) { // called on first resume |
[f802e46] | 23 | int fn1, fn2; // retained between resumes |
[af1ed1ad] | 24 | fn = 0; fn1 = fn; // 1st case |
[f802e46] | 25 | suspend(); // restart last resume |
[af1ed1ad] | 26 | fn = 1; fn2 = fn1; fn1 = fn; // 2nd case |
[f802e46] | 27 | suspend(); // restart last resume |
[971d9f2] | 28 | for ( ;; ) { |
[71b50f36] | 29 | fn = fn1 + fn2; fn2 = fn1; fn1 = fn; // general case |
[f802e46] | 30 | suspend(); // restart last resume |
[ec95d11] | 31 | } // for |
[c15b805] | 32 | } |
33 | ||
[971d9f2] | 34 | int next( Fibonacci & fib ) with( fib ) { |
35 | resume( fib ); // restart last suspend | |
36 | return fn; | |
[c15b805] | 37 | } |
38 | ||
[9129a84] | 39 | int main() { |
[ec95d11] | 40 | Fibonacci f1, f2; |
41 | for ( int i = 1; i <= 10; i += 1 ) { | |
[f802e46] | 42 | sout | next( f1 ) | next( f2 ) | endl; |
[ec95d11] | 43 | } // for |
[0e76cf4f] | 44 | } |
[ec95d11] | 45 | |
46 | // Local Variables: // | |
47 | // tab-width: 4 // | |
48 | // compile-command: "cfa fibonacci.c" // | |
49 | // End: // |
Note: See TracBrowser
for help on using the repository browser.