Ignore:
Timestamp:
Dec 6, 2016, 6:28:47 PM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, 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, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
2cd0434
Parents:
fa66f4e
Message:

cleaned-up coroutines code to no longer need a manual start

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/tests/coroutine.c

    rfa66f4e r5c81105  
    33
    44struct Fibonacci {
     5      int fn; // used for communication
     6      covptr_t v;
    57      coroutine c;
    6       coVtable v;
    7       int fn; // used for communication
    88};
    99
    10 coroutine* this_coroutine(Fibonacci* this);
    1110void co_main(Fibonacci* this);
    12 coVtable* vtable(Fibonacci* this);
     11covptr_t* vtable(Fibonacci* this);
    1312
     13//GENERATED in proposal for virtuals
    1414void co_main_fib(void* this) {
    1515      co_main( (Fibonacci*) this );
    1616}
    1717
    18 coroutine* this_coroutine_fib(void* this) {
    19       return this_coroutine( (Fibonacci*) this);
    20 }
     18//GENERATED in proposal for virtuals
     19static coVtable_t FibonacciVtable = {
     20      co_main_fib,
     21      VPTR_OFFSET(Fibonacci, v, c),
     22      VPTR_OFFSET(Fibonacci, v, fn)     
     23};
    2124
    2225void ?{}(Fibonacci* this) {
    2326      this->fn = 0;
    24       this->v.main = co_main_fib;
    25       this->v.this_coroutine = this_coroutine_fib;
    26       start(this);
     27      this->v = &FibonacciVtable;  //GENERATED in proposal for virtuals
     28      (&this->c) { &this->v };
    2729}
    2830
     
    3032#ifdef MORE_DEBUG
    3133      sout | "Starting main of coroutine " | this | endl;
    32       sout | "Started from " | this_coroutine(this)->last | endl;
     34      sout | "Started from " | this->c.last | endl;
    3335#endif
    3436      int fn1, fn2;             // retained between resumes
     
    5557}
    5658
    57 coroutine* this_coroutine(Fibonacci* this) {
    58       return &this->c;
    59 }
    60 
    61 coVtable* vtable(Fibonacci* this) {
     59covptr_t* vtable(Fibonacci* this) {
    6260      return &this->v;
    6361}
     
    6664      Fibonacci f1, f2;
    6765#ifdef MORE_DEBUG     
    68       sout | "User coroutines : " | &f1 | ' ' | &f1 | endl;
     66      Fibonacci *pf1 = &f1, *pf2 = &f2;
     67      coroutine *cf1 = &f1.c, *cf2 = &f2.c;
     68      covptr_t  *vf1 = vtable(pf1), *vf2 = vtable(pf2);
     69      coroutine *cv1 = get_coroutine(vf1), *cv2 = get_coroutine(vf2);
     70      Fibonacci *ov1 = (Fibonacci *)get_object(vf1), *ov2 = (Fibonacci *)get_object(vf2);
     71
     72      sout | "User coroutines : " | pf1 | ' ' | pf2 | endl;
     73      sout | "Coroutine data  : " | cf1 | ' ' | cf2 | endl;
     74      sout | "Vptr address    : " | vf1 | ' ' | vf2 | endl;
     75      sout | "Vptr obj data   : " | ov1 | ' ' | ov2 | endl;
     76      sout | "Vptr cor data   : " | cv1 | ' ' | cv2 | endl;
    6977#endif
    7078      for ( int i = 1; i <= 10; i += 1 ) {
Note: See TracChangeset for help on using the changeset viewer.