// // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // coroutine -- // // Author : Thierry Delisle // Created On : Mon Nov 28 12:27:26 2016 // Last Modified By : Peter A. Buhr // Last Modified On : Fri Mar 30 18:23:45 2018 // Update Count : 8 // #pragma once #include #include "invoke.h" //----------------------------------------------------------------------------- // Coroutine trait // Anything that implements this trait can be resumed. // Anything that is resumed is a coroutine. trait is_coroutine(dtype T) { void main(T & this); coroutine_desc * get_coroutine(T & this); }; #define DECL_COROUTINE(X) static inline coroutine_desc* get_coroutine(X& this) { return &this.__cor; } void main(X& this) //----------------------------------------------------------------------------- // Ctors and dtors // void ?{}( coStack_t & this ); // void ^?{}( coStack_t & this ); void ?{}( coroutine_desc & this, const char * name, void * storage, size_t storageSize ); void ^?{}( coroutine_desc & this ); static inline void ?{}( coroutine_desc & this) { this{ "Anonymous Coroutine", NULL, 0 }; } static inline void ?{}( coroutine_desc & this, size_t stackSize) { this{ "Anonymous Coroutine", NULL, stackSize }; } static inline void ?{}( coroutine_desc & this, void * storage, size_t storageSize ) { this{ "Anonymous Coroutine", storage, storageSize }; } static inline void ?{}( coroutine_desc & this, const char * name) { this{ name, NULL, 0 }; } static inline void ?{}( coroutine_desc & this, const char * name, size_t stackSize ) { this{ name, NULL, stackSize }; } //----------------------------------------------------------------------------- // Public coroutine API static inline void suspend(void); forall(dtype T | is_coroutine(T)) static inline void resume(T & cor); forall(dtype T | is_coroutine(T)) void prime(T & cor); //----------------------------------------------------------------------------- // PRIVATE exposed because of inline // Start coroutine routines extern "C" { forall(dtype T | is_coroutine(T)) void CtxInvokeCoroutine(T * this); forall(dtype T | is_coroutine(T)) void CtxStart(T * this, void ( *invoke)(T *)); extern void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine_desc *) __attribute__ ((__noreturn__)); extern void CtxSwitch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("CtxSwitch"); extern void CtxStore ( struct __stack_context_t * from, __attribute__((noreturn)) void (*__callback)(void) ) asm ("CtxStore"); extern void CtxRet ( struct __stack_context_t * to ) asm ("CtxRet") __attribute__ ((__noreturn__)); } // Private wrappers for context switch and stack creation // Wrapper for co static inline void CoroutineCtxSwitch(coroutine_desc* src, coroutine_desc* dst) { // set state of current coroutine to inactive src->state = src->state == Halted ? Halted : Inactive; // set new coroutine that task is executing TL_GET( this_thread )->curr_cor = dst; // context switch to specified coroutine verify( dst->context.SP ); CtxSwitch( &src->context, &dst->context ); // when CtxSwitch returns we are back in the src coroutine // set state of new coroutine to active src->state = Active; if( unlikely(src->cancellation != NULL) ) { _CtxCoroutine_Unwind(src->cancellation, src); } } extern void __stack_prepare ( __stack_info_t * this, size_t size /* ignored if storage already allocated */); // Suspend implementation inlined for performance static inline void suspend(void) { // optimization : read TLS once and reuse it // Safety note: this is preemption safe since if // preemption occurs after this line, the pointer // will also migrate which means this value will // stay in syn with the TLS coroutine_desc * src = TL_GET( this_thread )->curr_cor; assertf( src->last != 0, "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n" "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.", src->name, src ); assertf( src->last->state != Halted, "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n" "Possible cause is terminated coroutine's main routine has already returned.", src->name, src, src->last->name, src->last ); CoroutineCtxSwitch( src, src->last ); } // Resume implementation inlined for performance forall(dtype T | is_coroutine(T)) static inline void resume(T & cor) { // optimization : read TLS once and reuse it // Safety note: this is preemption safe since if // preemption occurs after this line, the pointer // will also migrate which means this value will // stay in syn with the TLS coroutine_desc * src = TL_GET( this_thread )->curr_cor; coroutine_desc * dst = get_coroutine(cor); if( unlikely(dst->context.SP == NULL) ) { __stack_prepare(&dst->stack, 65000); CtxStart(&cor, CtxInvokeCoroutine); } // not resuming self ? if ( src != dst ) { assertf( dst->state != Halted , "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n" "Possible cause is terminated coroutine's main routine has already returned.", src->name, src, dst->name, dst ); // set last resumer dst->last = src; dst->starter = dst->starter ? dst->starter : src; } // always done for performance testing CoroutineCtxSwitch( src, dst ); } static inline void resume(coroutine_desc * dst) { // optimization : read TLS once and reuse it // Safety note: this is preemption safe since if // preemption occurs after this line, the pointer // will also migrate which means this value will // stay in syn with the TLS coroutine_desc * src = TL_GET( this_thread )->curr_cor; // not resuming self ? if ( src != dst ) { assertf( dst->state != Halted , "Attempt by coroutine %.256s (%p) to resume terminated coroutine %.256s (%p).\n" "Possible cause is terminated coroutine's main routine has already returned.", src->name, src, dst->name, dst ); // set last resumer dst->last = src; } // always done for performance testing CoroutineCtxSwitch( src, dst ); } static inline void suspend_then(fptr_t call) { // optimization : read TLS once and reuse it // Safety note: this is preemption safe since if // preemption occurs after this line, the pointer // will also migrate which means this value will // stay in syn with the TLS coroutine_desc * src = TL_GET( this_thread )->curr_cor; assertf( src->last != 0, "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n" "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.", src->name, src ); assertf( src->last->state != Halted, "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n" "Possible cause is terminated coroutine's main routine has already returned.", src->name, src, src->last->name, src->last ); src->state = PreInactive; // context switch to specified coroutine assert( src->context.SP ); __attribute__((noreturn)) void __suspend_callback(void) { call(); // set state of current coroutine to inactive src->state = src->state == Halted ? Halted : Inactive; TL_GET( this_thread )->curr_cor = src->last; // context switch to specified coroutine assert( src->last->context.SP ); CtxRet( &src->last->context ); abort(); } CtxStore( &src->context, __suspend_callback ); // when CtxStore returns we are back in the src coroutine // set state of new coroutine to active src->state = Active; if( unlikely(src->cancellation != NULL) ) { _CtxCoroutine_Unwind(src->cancellation, src); } return; } // static inline void suspend_return(void) { // // optimization : read TLS once and reuse it // // Safety note: this is preemption safe since if // // preemption occurs after this line, the pointer // // will also migrate which means this value will // // stay in syn with the TLS // coroutine_desc * src = TL_GET( this_thread )->curr_cor; // assertf( src->last != 0, // "Attempt to suspend coroutine \"%.256s\" (%p) that has never been resumed.\n" // "Possible cause is a suspend executed in a member called by a coroutine user rather than by the coroutine main.", // src->name, src ); // assertf( src->last->state != Halted, // "Attempt by coroutine \"%.256s\" (%p) to suspend back to terminated coroutine \"%.256s\" (%p).\n" // "Possible cause is terminated coroutine's main routine has already returned.", // src->name, src, src->last->name, src->last ); // // Safety note : Preemption must be disabled here since kernelTLS.this_coroutine must always be up to date // verify( TL_GET( preemption_state.enabled ) || TL_GET( this_processor )->do_terminate ); // disable_interrupts(); // // set state of current coroutine to inactive // src->state = src->state == Halted ? Halted : Inactive; // // set new coroutine that task is executing // kernelTLS.this_coroutine = dst; // // context switch to specified coroutine // assert( src->stack.context ); // CtxRet( src->stack.context ); // abort(); // } // Local Variables: // // mode: c // // tab-width: 4 // // End: //