source:
src/tests/coroutine.c@
0c6596f
| Last change on this file since 0c6596f was 83a071f9, checked in by , 8 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 | |
| 12 | // Last Modified On : Thu Jun 8 07:37:12 2017 | |
| 13 | // Update Count : 5 | |
| [242a902] | 14 | // |
| [ec95d11] | 15 | |
| [c15b805] | 16 | #include <fstream> |
| [4a3334cf] | 17 | #include <coroutine> |
| [c15b805] | 18 | |
| [e04b636] | 19 | coroutine Fibonacci { |
| [ec95d11] | 20 | int fn; // used for communication |
| [c15b805] | 21 | }; |
| 22 | ||
| [242a902] | 23 | void ?{}( Fibonacci & this ) { |
| 24 | this.fn = 0; | |
| [80d9e49] | 25 | } |
| 26 | ||
| [83a071f9] | 27 | void main( Fibonacci & this ) { |
| [ec95d11] | 28 | int fn1, fn2; // retained between resumes |
| [83a071f9] | 29 | this.fn = 0; // case 0 |
| 30 | fn1 = this.fn; | |
| [ec95d11] | 31 | suspend(); // return to last resume |
| 32 | ||
| [83a071f9] | 33 | this.fn = 1; // case 1 |
| [ec95d11] | 34 | fn2 = fn1; |
| [83a071f9] | 35 | fn1 = this.fn; |
| [ec95d11] | 36 | suspend(); // return to last resume |
| 37 | ||
| 38 | for ( ;; ) { // general case | |
| [83a071f9] | 39 | this.fn = fn1 + fn2; |
| [ec95d11] | 40 | fn2 = fn1; |
| [83a071f9] | 41 | fn1 = this.fn; |
| [ec95d11] | 42 | suspend(); // return to last resume |
| 43 | } // for | |
| [c15b805] | 44 | } |
| 45 | ||
| [83a071f9] | 46 | int next( Fibonacci & this ) { |
| [ec95d11] | 47 | resume( this ); // transfer to last suspend |
| [83a071f9] | 48 | return this.fn; |
| [c15b805] | 49 | } |
| 50 | ||
| [9129a84] | 51 | int main() { |
| [ec95d11] | 52 | Fibonacci f1, f2; |
| 53 | for ( int i = 1; i <= 10; i += 1 ) { | |
| [83a071f9] | 54 | sout | next( f1 ) | ' ' | next( f2 ) | endl; |
| [ec95d11] | 55 | } // for |
| [0e76cf4f] | 56 | } |
| [ec95d11] | 57 | |
| 58 | // Local Variables: // | |
| 59 | // tab-width: 4 // | |
| 60 | // compile-command: "cfa fibonacci.c" // | |
| 61 | // End: // |
Note:
See TracBrowser
for help on using the repository browser.