Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/invoke.c

    rac2b598 r2026bb6  
    2929// Called from the kernel when starting a coroutine or task so must switch back to user mode.
    3030
    31 extern struct $coroutine * __cfactx_cor_finish(void);
    32 extern void __cfactx_cor_leave ( struct $coroutine * );
    33 extern void __cfactx_thrd_leave();
    34 
     31extern void __suspend_internal(void);
     32extern void __leave_coroutine( struct coroutine_desc * );
     33extern void __finish_creation( struct thread_desc * );
     34extern void __leave_thread_monitor( struct thread_desc * this );
    3535extern void disable_interrupts() OPTIONAL_THREAD;
    3636extern void enable_interrupts( __cfaabi_dbg_ctx_param );
    3737
    38 void __cfactx_invoke_coroutine(
     38void CtxInvokeCoroutine(
    3939        void (*main)(void *),
     40        struct coroutine_desc *(*get_coroutine)(void *),
    4041        void *this
    4142) {
    42         // Finish setting up the coroutine by setting its state
    43         struct $coroutine * cor = __cfactx_cor_finish();
     43        struct coroutine_desc* cor = get_coroutine( this );
    4444
    45         // Call the main of the coroutine
     45        if(cor->state == Primed) {
     46                __suspend_internal();
     47        }
     48
     49        cor->state = Active;
     50
    4651        main( this );
    4752
    4853        //Final suspend, should never return
    49         __cfactx_cor_leave( cor );
     54        __leave_coroutine( cor );
    5055        __cabi_abort( "Resumed dead coroutine" );
    5156}
    5257
    53 static _Unwind_Reason_Code __cfactx_coroutine_unwindstop(
     58static _Unwind_Reason_Code _CtxCoroutine_UnwindStop(
    5459        __attribute((__unused__)) int version,
    5560        _Unwind_Action actions,
     
    6267                // We finished unwinding the coroutine,
    6368                // leave it
    64                 __cfactx_cor_leave( param );
     69                __leave_coroutine( param );
    6570                __cabi_abort( "Resumed dead coroutine" );
    6671        }
     
    7075}
    7176
    72 void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct $coroutine * cor) __attribute__ ((__noreturn__));
    73 void __cfactx_coroutine_unwind(struct _Unwind_Exception * storage, struct $coroutine * cor) {
    74         _Unwind_Reason_Code ret = _Unwind_ForcedUnwind( storage, __cfactx_coroutine_unwindstop, cor );
     77void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine_desc * cor) __attribute__ ((__noreturn__));
     78void _CtxCoroutine_Unwind(struct _Unwind_Exception * storage, struct coroutine_desc * cor) {
     79        _Unwind_Reason_Code ret = _Unwind_ForcedUnwind( storage, _CtxCoroutine_UnwindStop, cor );
    7580        printf("UNWIND ERROR %d after force unwind\n", ret);
    7681        abort();
    7782}
    7883
    79 void __cfactx_invoke_thread(
     84void CtxInvokeThread(
     85        void (*dtor)(void *),
    8086        void (*main)(void *),
     87        struct thread_desc *(*get_thread)(void *),
    8188        void *this
    8289) {
     90        // Fetch the thread handle from the user defined thread structure
     91        struct thread_desc* thrd = get_thread( this );
     92
     93        // First suspend, once the thread arrives here,
     94        // the function pointer to main can be invalidated without risk
     95        __finish_creation( thrd );
     96
    8397        // Officially start the thread by enabling preemption
    8498        enable_interrupts( __cfaabi_dbg_ctx );
     
    94108        // The order of these 4 operations is very important
    95109        //Final suspend, should never return
    96         __cfactx_thrd_leave();
     110        __leave_thread_monitor( thrd );
    97111        __cabi_abort( "Resumed dead thread" );
    98112}
    99113
    100 void __cfactx_start(
     114
     115void CtxStart(
    101116        void (*main)(void *),
    102         struct $coroutine * cor,
     117        struct coroutine_desc *(*get_coroutine)(void *),
    103118        void *this,
    104119        void (*invoke)(void *)
    105120) {
     121        struct coroutine_desc * cor = get_coroutine( this );
    106122        struct __stack_t * stack = cor->stack.storage;
    107123
     
    122138
    123139        fs->dummyReturn = NULL;
    124         fs->argument[0] = main;     // argument to invoke
    125         fs->argument[1] = this;     // argument to invoke
     140        fs->argument[0] = this;     // argument to invoke
    126141        fs->rturn = invoke;
    127142
     
    140155
    141156        fs->dummyReturn = NULL;
    142         fs->rturn = __cfactx_invoke_stub;
    143         fs->fixedRegisters[0] = main;
    144         fs->fixedRegisters[1] = this;
    145         fs->fixedRegisters[2] = invoke;
     157        fs->rturn = CtxInvokeStub;
     158        fs->fixedRegisters[0] = this;
     159        fs->fixedRegisters[1] = invoke;
    146160
    147161#elif defined( __ARM_ARCH )
    148 #error ARM needs to be upgrade to use to parameters like X86/X64 (A.K.A. : I broke this and do not know how to fix it)
     162
    149163        struct FakeStack {
    150164                float fpRegs[16];                       // floating point registers
     
    158172        struct FakeStack *fs = (struct FakeStack *)cor->context.SP;
    159173
    160         fs->intRegs[8] = __cfactx_invoke_stub;
     174        fs->intRegs[8] = CtxInvokeStub;
    161175        fs->arg[0] = this;
    162176        fs->arg[1] = invoke;
Note: See TracChangeset for help on using the changeset viewer.