- Timestamp:
- Dec 6, 2016, 6:28:47 PM (8 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/tests/coroutine.c
rfa66f4e r5c81105 3 3 4 4 struct Fibonacci { 5 int fn; // used for communication 6 covptr_t v; 5 7 coroutine c; 6 coVtable v;7 int fn; // used for communication8 8 }; 9 9 10 coroutine* this_coroutine(Fibonacci* this);11 10 void co_main(Fibonacci* this); 12 co Vtable* vtable(Fibonacci* this);11 covptr_t* vtable(Fibonacci* this); 13 12 13 //GENERATED in proposal for virtuals 14 14 void co_main_fib(void* this) { 15 15 co_main( (Fibonacci*) this ); 16 16 } 17 17 18 coroutine* this_coroutine_fib(void* this) { 19 return this_coroutine( (Fibonacci*) this); 20 } 18 //GENERATED in proposal for virtuals 19 static coVtable_t FibonacciVtable = { 20 co_main_fib, 21 VPTR_OFFSET(Fibonacci, v, c), 22 VPTR_OFFSET(Fibonacci, v, fn) 23 }; 21 24 22 25 void ?{}(Fibonacci* this) { 23 26 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 }; 27 29 } 28 30 … … 30 32 #ifdef MORE_DEBUG 31 33 sout | "Starting main of coroutine " | this | endl; 32 sout | "Started from " | this _coroutine(this)->last | endl;34 sout | "Started from " | this->c.last | endl; 33 35 #endif 34 36 int fn1, fn2; // retained between resumes … … 55 57 } 56 58 57 coroutine* this_coroutine(Fibonacci* this) { 58 return &this->c; 59 } 60 61 coVtable* vtable(Fibonacci* this) { 59 covptr_t* vtable(Fibonacci* this) { 62 60 return &this->v; 63 61 } … … 66 64 Fibonacci f1, f2; 67 65 #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; 69 77 #endif 70 78 for ( int i = 1; i <= 10; i += 1 ) {
Note: See TracChangeset
for help on using the changeset viewer.