Ignore:
File:
1 edited

Legend:

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

    r09f357ec r2026bb6  
    2929// Called from the kernel when starting a coroutine or task so must switch back to user mode.
    3030
    31 extern void __leave_coroutine ( struct coroutine_desc * );
    32 extern struct coroutine_desc * __finish_coroutine(void);
    33 extern void __leave_thread_monitor();
     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 );
    3435extern void disable_interrupts() OPTIONAL_THREAD;
    3536extern void enable_interrupts( __cfaabi_dbg_ctx_param );
     
    3738void CtxInvokeCoroutine(
    3839        void (*main)(void *),
     40        struct coroutine_desc *(*get_coroutine)(void *),
    3941        void *this
    4042) {
    41         // Finish setting up the coroutine by setting its state
    42         struct coroutine_desc * cor = __finish_coroutine();
     43        struct coroutine_desc* cor = get_coroutine( this );
    4344
    44         // Call the main of the coroutine
     45        if(cor->state == Primed) {
     46                __suspend_internal();
     47        }
     48
     49        cor->state = Active;
     50
    4551        main( this );
    4652
     
    7783
    7884void CtxInvokeThread(
     85        void (*dtor)(void *),
    7986        void (*main)(void *),
     87        struct thread_desc *(*get_thread)(void *),
    8088        void *this
    8189) {
     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
    8297        // Officially start the thread by enabling preemption
    8398        enable_interrupts( __cfaabi_dbg_ctx );
     
    93108        // The order of these 4 operations is very important
    94109        //Final suspend, should never return
    95         __leave_thread_monitor();
     110        __leave_thread_monitor( thrd );
    96111        __cabi_abort( "Resumed dead thread" );
    97112}
    98113
     114
    99115void CtxStart(
    100116        void (*main)(void *),
    101         struct coroutine_desc * cor,
     117        struct coroutine_desc *(*get_coroutine)(void *),
    102118        void *this,
    103119        void (*invoke)(void *)
    104120) {
     121        struct coroutine_desc * cor = get_coroutine( this );
    105122        struct __stack_t * stack = cor->stack.storage;
    106123
     
    121138
    122139        fs->dummyReturn = NULL;
    123         fs->argument[0] = main;     // argument to invoke
    124         fs->argument[1] = this;     // argument to invoke
     140        fs->argument[0] = this;     // argument to invoke
    125141        fs->rturn = invoke;
    126142
     
    140156        fs->dummyReturn = NULL;
    141157        fs->rturn = CtxInvokeStub;
    142         fs->fixedRegisters[0] = main;
    143         fs->fixedRegisters[1] = this;
    144         fs->fixedRegisters[2] = invoke;
     158        fs->fixedRegisters[0] = this;
     159        fs->fixedRegisters[1] = invoke;
    145160
    146161#elif defined( __ARM_ARCH )
    147 #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
    148163        struct FakeStack {
    149164                float fpRegs[16];                       // floating point registers
Note: See TracChangeset for help on using the changeset viewer.