source: src/tests/coroutine/coroutine.c @ 099a40d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 099a40d was 557435e, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Moved concurrent objects

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