Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tests/coroutine.c

    rec95d11 re04b636  
    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 Jun  8 07:37:12 2017
    13 // Update Count     : 5
    14 //
    15 
    161#include <fstream>
    172#include <coroutine>
    183
    194coroutine Fibonacci {
    20         int fn;                                         // used for communication
     5      int fn; // used for communication
    216};
    227
    23 void ?{}( Fibonacci * this ) {
    24         this->fn = 0;
     8void ?{}(Fibonacci* this) {
     9      this->fn = 0;
    2510}
    2611
    27 void main( Fibonacci * this ) {
    28         int fn1, fn2;                                   // retained between resumes
    29         this->fn = 0;                                   // case 0
    30         fn1 = this->fn;
    31         suspend();                                              // return to last resume
     12void main(Fibonacci* this) {
     13      int fn1, fn2;             // retained between resumes
     14      this->fn = 0;
     15      fn1 = this->fn;
     16      suspend();                // return to last resume
    3217
    33         this->fn = 1;                                   // case 1
    34         fn2 = fn1;
    35         fn1 = this->fn;
    36         suspend();                                              // return to last resume
     18      this->fn = 1;
     19      fn2 = fn1;
     20      fn1 = this->fn;
     21      suspend();                // return to last resume
    3722
    38         for ( ;; ) {                                    // general case
    39                 this->fn = fn1 + fn2;
    40                 fn2 = fn1;
    41                 fn1 = this->fn;
    42                 suspend();                                      // return to last resume
    43         } // for
     23      for ( ;; ) {
     24            this->fn = fn1 + fn2;
     25            fn2 = fn1;
     26            fn1 = this->fn;
     27            suspend(); // return to last resume
     28      }
    4429}
    4530
    46 int next( Fibonacci * this ) {
    47         resume( this );                                 // transfer to last suspend
    48         return this->fn;
     31int next(Fibonacci* this) {
     32      resume(this); // transfer to last suspend
     33      return this->fn;
    4934}
    5035
    5136int main() {
    52         Fibonacci f1, f2;
    53         for ( int i = 1; i <= 10; i += 1 ) {
    54                 sout | next( &f1 ) | ' ' | next( &f2 ) | endl;
    55         } // for
     37      Fibonacci f1, f2;
     38      for ( int i = 1; i <= 10; i += 1 ) {
     39            sout | next(&f1) | ' ' | next(&f2) | endl;
     40      }
     41
     42      return 0;
    5643}
    57 
    58 // Local Variables: //
    59 // tab-width: 4 //
    60 // compile-command: "cfa fibonacci.c" //
    61 // End: //
Note: See TracChangeset for help on using the changeset viewer.