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