source: src/tests/coroutine/fibonacci.c @ b91e8c1

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since b91e8c1 was af1ed1ad, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

formatting

  • Property mode set to 100644
File size: 1.3 KB
Line 
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 --
8//
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 Mar 22 22:45:44 2018
13// Update Count     : 15
14//
15
16#include <fstream>
17#include <coroutine>
18
19coroutine Fibonacci { int fn; };                                                // used for communication
20
21void ?{}( Fibonacci & fib ) with( fib ) { fn = 0; }
22
23// main automatically called on first resume
24void main( Fibonacci & fib ) with( fib ) {
25        int fn1, fn2;                                                                           // retained between resumes
26        fn = 0;  fn1 = fn;                                                                      // 1st case
27        suspend();                                                                                      // restart last resume
28        fn = 1;  fn2 = fn1;  fn1 = fn;                                          // 2nd case
29        suspend();                                                                                      // restart last resume
30        for ( ;; ) {
31                fn = fn1 + fn2; fn2 = fn1;  fn1 = fn;                   // general case
32                suspend();                                                                              // restart last resume
33        } // for
34}
35
36int next( Fibonacci & fib ) with( fib ) {
37        resume( fib );                                                                          // restart last suspend
38        return fn;
39}
40
41int main() {
42        Fibonacci f1, f2;
43        for ( int i = 1; i <= 10; i += 1 ) {
44                sout | next( f1 ) | next( f2 ) | endl;
45        } // for
46}
47
48// Local Variables: //
49// tab-width: 4 //
50// compile-command: "cfa fibonacci.c" //
51// End: //
Note: See TracBrowser for help on using the repository browser.