Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/coroutine.hfa

    rd4e68a6 rac2b598  
    1010// Created On       : Mon Nov 28 12:27:26 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jun 21 17:49:39 2019
    13 // Update Count     : 9
     12// Last Modified On : Tue Feb  4 12:29:26 2020
     13// Update Count     : 11
    1414//
    1515
     
    2525trait is_coroutine(dtype T) {
    2626      void main(T & this);
    27       coroutine_desc * get_coroutine(T & this);
     27      $coroutine * get_coroutine(T & this);
    2828};
    2929
    30 #define DECL_COROUTINE(X) static inline coroutine_desc* get_coroutine(X& this) { return &this.__cor; } void main(X& this)
     30#define DECL_COROUTINE(X) static inline $coroutine* get_coroutine(X& this) { return &this.__cor; } void main(X& this)
    3131
    3232//-----------------------------------------------------------------------------
     
    3535// void ^?{}( coStack_t & this );
    3636
    37 void ?{}( coroutine_desc & this, const char * name, void * storage, size_t storageSize );
    38 void ^?{}( coroutine_desc & this );
     37void  ?{}( $coroutine & this, const char name[], void * storage, size_t storageSize );
     38void ^?{}( $coroutine & this );
    3939
    40 static inline void ?{}( coroutine_desc & this)                                       { this{ "Anonymous Coroutine", NULL, 0 }; }
    41 static inline void ?{}( coroutine_desc & this, size_t stackSize)                     { this{ "Anonymous Coroutine", NULL, stackSize }; }
    42 static inline void ?{}( coroutine_desc & this, void * storage, size_t storageSize )  { this{ "Anonymous Coroutine", storage, storageSize }; }
    43 static inline void ?{}( coroutine_desc & this, const char * name)                    { this{ name, NULL, 0 }; }
    44 static inline void ?{}( coroutine_desc & this, const char * name, size_t stackSize ) { this{ name, NULL, stackSize }; }
     40static inline void ?{}( $coroutine & this)                                       { this{ "Anonymous Coroutine", 0p, 0 }; }
     41static inline void ?{}( $coroutine & this, size_t stackSize)                     { this{ "Anonymous Coroutine", 0p, stackSize }; }
     42static inline void ?{}( $coroutine & this, void * storage, size_t storageSize )  { this{ "Anonymous Coroutine", storage, storageSize }; }
     43static inline void ?{}( $coroutine & this, const char name[])                    { this{ name, 0p, 0 }; }
     44static inline void ?{}( $coroutine & this, const char name[], size_t stackSize ) { this{ name, 0p, stackSize }; }
    4545
    4646//-----------------------------------------------------------------------------
     
    5454void prime(T & cor);
    5555
    56 static inline struct coroutine_desc * active_coroutine() { return TL_GET( this_thread )->curr_cor; }
     56static inline struct $coroutine * active_coroutine() { return TL_GET( this_thread )->curr_cor; }
    5757
    5858//-----------------------------------------------------------------------------
     
    6161// Start coroutine routines
    6262extern "C" {
    63       forall(dtype T | is_coroutine(T))
    64       void CtxInvokeCoroutine(T * this);
     63        void __cfactx_invoke_coroutine(void (*main)(void *), void * this);
    6564
    66       forall(dtype T | is_coroutine(T))
    67       void CtxStart(T * this, void ( *invoke)(T *));
     65        forall(dtype T)
     66        void __cfactx_start(void (*main)(T &), struct $coroutine * cor, T & this, void (*invoke)(void (*main)(void *), void *));
    6867
    69         extern void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine_desc *) __attribute__ ((__noreturn__));
     68        extern void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct $coroutine *) __attribute__ ((__noreturn__));
    7069
    71         extern void CtxSwitch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("CtxSwitch");
     70        extern void __cfactx_switch( struct __stack_context_t * from, struct __stack_context_t * to ) asm ("__cfactx_switch");
    7271}
    7372
    7473// Private wrappers for context switch and stack creation
    7574// Wrapper for co
    76 static inline void CoroutineCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
     75static inline void $ctx_switch( $coroutine * src, $coroutine * dst ) __attribute__((nonnull (1, 2))) {
    7776        // set state of current coroutine to inactive
    7877        src->state = src->state == Halted ? Halted : Inactive;
     
    8382        // context switch to specified coroutine
    8483        verify( dst->context.SP );
    85         CtxSwitch( &src->context, &dst->context );
    86         // when CtxSwitch returns we are back in the src coroutine
     84        __cfactx_switch( &src->context, &dst->context );
     85        // when __cfactx_switch returns we are back in the src coroutine
    8786
    8887        // set state of new coroutine to active
    8988        src->state = Active;
    9089
    91         if( unlikely(src->cancellation != NULL) ) {
    92                 _CtxCoroutine_Unwind(src->cancellation, src);
     90        if( unlikely(src->cancellation != 0p) ) {
     91                __cfactx_coroutine_unwind(src->cancellation, src);
    9392        }
    9493}
     
    103102        // will also migrate which means this value will
    104103        // stay in syn with the TLS
    105         coroutine_desc * src = TL_GET( this_thread )->curr_cor;
     104        $coroutine * src = TL_GET( this_thread )->curr_cor;
    106105
    107106        assertf( src->last != 0,
     
    114113                src->name, src, src->last->name, src->last );
    115114
    116         CoroutineCtxSwitch( src, src->last );
     115        $ctx_switch( src, src->last );
    117116}
    118117
     
    125124        // will also migrate which means this value will
    126125        // stay in syn with the TLS
    127         coroutine_desc * src = TL_GET( this_thread )->curr_cor;
    128         coroutine_desc * dst = get_coroutine(cor);
     126        $coroutine * src = TL_GET( this_thread )->curr_cor;
     127        $coroutine * dst = get_coroutine(cor);
    129128
    130         if( unlikely(dst->context.SP == NULL) ) {
     129        if( unlikely(dst->context.SP == 0p) ) {
     130                TL_GET( this_thread )->curr_cor = dst;
    131131                __stack_prepare(&dst->stack, 65000);
    132                 CtxStart(&cor, CtxInvokeCoroutine);
     132                __cfactx_start(main, dst, cor, __cfactx_invoke_coroutine);
     133                TL_GET( this_thread )->curr_cor = src;
    133134        }
    134135
     
    146147
    147148        // always done for performance testing
    148         CoroutineCtxSwitch( src, dst );
     149        $ctx_switch( src, dst );
    149150
    150151        return cor;
    151152}
    152153
    153 static inline void resume(coroutine_desc * dst) {
     154static inline void resume( $coroutine * dst ) __attribute__((nonnull (1))) {
    154155        // optimization : read TLS once and reuse it
    155156        // Safety note: this is preemption safe since if
     
    157158        // will also migrate which means this value will
    158159        // stay in syn with the TLS
    159         coroutine_desc * src = TL_GET( this_thread )->curr_cor;
     160        $coroutine * src = TL_GET( this_thread )->curr_cor;
    160161
    161162        // not resuming self ?
     
    171172
    172173        // always done for performance testing
    173         CoroutineCtxSwitch( src, dst );
     174        $ctx_switch( src, dst );
    174175}
    175176
Note: See TracChangeset for help on using the changeset viewer.