Ignore:
Timestamp:
Dec 2, 2016, 5:10:22 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
d7bcbf5
Parents:
4cb935e
Message:

Ugly but working coroutines

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/examples/coroutine.c

    r4cb935e r78b3f52  
    44struct Fibonacci {
    55      coroutine c;
     6      coVtable v;
    67      int fn; // used for communication
    78};
    89
     10coroutine* this_coroutine(Fibonacci* this);
     11void co_main(Fibonacci* this);
     12coVtable* vtable(Fibonacci* this);
     13
     14void co_main_fib(void* this) {
     15      co_main( (Fibonacci*) this );
     16}
     17
     18coroutine* this_coroutine_fib(void* this) {
     19      return this_coroutine( (Fibonacci*) this);
     20}
     21
    922void ?{}(Fibonacci* this) {
    1023      this->fn = 0;
    11 }
    12 
    13 coroutine* this_coroutine(Fibonacci* this) {
    14       return &this->c;
     24      this->v.main = co_main_fib;
     25      this->v.this_coroutine = this_coroutine_fib;
     26      start(this);
    1527}
    1628
    1729void co_main(Fibonacci* this) {
     30      sout | "Starting main of coroutine " | this | endl;
     31      sout | "Started from " | this_coroutine(this)->last | endl;
    1832      int fn1, fn2;             // retained between resumes
    1933      this->fn = 0;
     
    3953}
    4054
     55coroutine* this_coroutine(Fibonacci* this) {
     56      return &this->c;
     57}
     58
     59coVtable* vtable(Fibonacci* this) {
     60      return &this->v;
     61}
     62
    4163int main() {
    42       Fibonacci f1, f2;
     64      Fibonacci f1;
     65      sout | "User coroutine : " | &f1 | endl;
    4366      for ( int i = 1; i <= 10; i += 1 ) {
    44             sout | next(&f1) | ' ' | next(&f2) | endl;
     67            sout | next(&f1) | endl;
    4568      }
    4669
Note: See TracChangeset for help on using the changeset viewer.