Changeset e8e457e


Ignore:
Timestamp:
Apr 10, 2019, 11:37:21 AM (5 years ago)
Author:
tdelisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
5c1a531
Parents:
69a61d2
Message:

Thread context is now distinct from coroutine context

Location:
libcfa/src/concurrency
Files:
5 edited

Legend:

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

    r69a61d2 re8e457e  
    138138
    139139        // context switch to specified coroutine
    140         verify( src->context.SP );
    141140        verify( dst->context.SP );
    142141        CtxSwitch( &src->context, &dst->context );
  • libcfa/src/concurrency/invoke.c

    r69a61d2 re8e457e  
    2929extern void __suspend_internal(void);
    3030extern void __leave_coroutine( struct coroutine_desc * );
    31 extern void __finish_creation( struct coroutine_desc * );
     31extern void __finish_creation( struct thread_desc * );
    3232extern void __leave_thread_monitor( struct thread_desc * this );
    3333extern void disable_interrupts();
     
    9393        // First suspend, once the thread arrives here,
    9494        // the function pointer to main can be invalidated without risk
    95         __finish_creation(&thrd->self_cor);
    96 
    97         // Restore the last to NULL, we clobbered because of the thunk problem
    98         thrd->self_cor.last = NULL;
     95        __finish_creation( thrd );
    9996
    10097        // Officially start the thread by enabling preemption
  • libcfa/src/concurrency/invoke.h

    r69a61d2 re8e457e  
    6464        struct __stack_context_t {
    6565                void * SP;
    66 
    6766                void * FP;
    6867                // copy of global UNIX variable errno
     
    105104                struct __stack_info_t stack;
    106105
    107                 // textual name for coroutine/task, initialized by uC++ generated code
     106                // textual name for coroutine/task
    108107                const char * name;
    109108
     
    164163        struct thread_desc {
    165164                // Core threading fields
     165                // context that is switch during a CtxSwitch
     166                struct __stack_context_t context;
     167
     168                // current execution status for coroutine
     169                enum coroutine_state state;
     170
    166171                // coroutine body used to store context
    167172                struct coroutine_desc  self_cor;
  • libcfa/src/concurrency/kernel.cfa

    r69a61d2 re8e457e  
    106106
    107107void ?{}( thread_desc & this, current_stack_info_t * info) with( this ) {
     108        state = Start;
    108109        self_cor{ info };
    109110        curr_cor = &self_cor;
     
    239240// runThread runs a thread by context switching
    240241// from the processor coroutine to the target thread
    241 static void runThread(processor * this, thread_desc * dst) {
    242         assert(dst->curr_cor);
     242static void runThread(processor * this, thread_desc * thrd_dst) {
    243243        coroutine_desc * proc_cor = get_coroutine(this->runner);
    244         coroutine_desc * thrd_cor = dst->curr_cor;
    245244
    246245        // Reset the terminating actions here
     
    248247
    249248        // Update global state
    250         kernelTLS.this_thread = dst;
    251 
    252         // Context Switch to the thread
    253         ThreadCtxSwitch(proc_cor, thrd_cor);
    254         // when ThreadCtxSwitch returns we are back in the processor coroutine
     249        kernelTLS.this_thread = thrd_dst;
     250
     251        // set state of processor coroutine to inactive and the thread to active
     252        proc_cor->state = proc_cor->state == Halted ? Halted : Inactive;
     253        thrd_dst->state = Active;
     254
     255        // set context switch to the thread that the processor is executing
     256        verify( thrd_dst->context.SP );
     257        CtxSwitch( &proc_cor->context, &thrd_dst->context );
     258        // when CtxSwitch returns we are back in the processor coroutine
     259
     260        // set state of processor coroutine to active and the thread to inactive
     261        thrd_dst->state = thrd_dst->state == Halted ? Halted : Inactive;
     262        proc_cor->state = Active;
    255263}
    256264
     
    258266static void returnToKernel() {
    259267        coroutine_desc * proc_cor = get_coroutine(kernelTLS.this_processor->runner);
    260         coroutine_desc * thrd_cor = kernelTLS.this_thread->curr_cor;
    261         ThreadCtxSwitch(thrd_cor, proc_cor);
     268        thread_desc * thrd_src = kernelTLS.this_thread;
     269
     270        // set state of current coroutine to inactive
     271        thrd_src->state = thrd_src->state == Halted ? Halted : Inactive;
     272        proc_cor->state = Active;
     273
     274        // set new coroutine that the processor is executing
     275        // and context switch to it
     276        verify( proc_cor->context.SP );
     277        CtxSwitch( &thrd_src->context, &proc_cor->context );
     278
     279        // set state of new coroutine to active
     280        proc_cor->state = proc_cor->state == Halted ? Halted : Inactive;
     281        thrd_src->state = Active;
    262282}
    263283
     
    343363
    344364// KERNEL_ONLY
    345 void kernel_first_resume(processor * this) {
    346         coroutine_desc * src = mainThread->curr_cor;
     365void kernel_first_resume( processor * this ) {
     366        thread_desc * src = mainThread;
    347367        coroutine_desc * dst = get_coroutine(this->runner);
    348368
     
    354374        verify( ! kernelTLS.preemption_state.enabled );
    355375
    356         dst->last = src;
    357         dst->starter = dst->starter ? dst->starter : src;
     376        dst->last = &src->self_cor;
     377        dst->starter = dst->starter ? dst->starter : &src->self_cor;
    358378
    359379        // set state of current coroutine to inactive
     
    378398}
    379399
     400// KERNEL_ONLY
     401void kernel_last_resume( processor * this ) {
     402        coroutine_desc * src = &mainThread->self_cor;
     403        coroutine_desc * dst = get_coroutine(this->runner);
     404
     405        verify( ! kernelTLS.preemption_state.enabled );
     406        verify( dst->starter == src );
     407        verify( dst->context.SP );
     408
     409        // context switch to the processor
     410        CtxSwitch( &src->context, &dst->context );
     411}
     412
    380413//-----------------------------------------------------------------------------
    381414// Scheduler routines
     
    384417void ScheduleThread( thread_desc * thrd ) {
    385418        verify( thrd );
    386         verify( thrd->self_cor.state != Halted );
     419        verify( thrd->state != Halted );
    387420
    388421        verify( ! kernelTLS.preemption_state.enabled );
     
    626659        // which is currently here
    627660        __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
    628         returnToKernel();
     661        kernel_last_resume( kernelTLS.this_processor );
    629662        mainThread->self_cor.state = Halted;
    630663
  • libcfa/src/concurrency/thread.cfa

    r69a61d2 re8e457e  
    3131// Thread ctors and dtors
    3232void ?{}(thread_desc & this, const char * const name, cluster & cl, void * storage, size_t storageSize ) with( this ) {
     33        context{ NULL, NULL };
    3334        self_cor{ name, storage, storageSize };
    34         verify(&self_cor);
     35        state = Start;
    3536        curr_cor = &self_cor;
    3637        self_mon.owner = &this;
     
    7374forall( dtype T | is_thread(T) )
    7475void __thrd_start( T& this ) {
    75         coroutine_desc* thrd_c = get_coroutine(this);
    76         thread_desc   * thrd_h = get_thread   (this);
    77         thrd_c->last = TL_GET( this_thread )->curr_cor;
    78 
    79         // __cfaabi_dbg_print_safe("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
     76        thread_desc * this_thrd = get_thread(this);
     77        thread_desc * curr_thrd = TL_GET( this_thread );
    8078
    8179        disable_interrupts();
    82         assert( thrd_c->stack.storage );
    8380        CtxStart(&this, CtxInvokeThread);
    84         verify( thrd_c->last->context.SP );
    85         verify( thrd_c->      context.SP );
    86         CtxSwitch( &thrd_c->last->context, &thrd_c->context );
     81        this_thrd->context.[SP, FP] = this_thrd->self_cor.context.[SP, FP];
     82        verify( this_thrd->context.SP );
     83        CtxSwitch( &curr_thrd->context, &this_thrd->context );
    8784
    88         ScheduleThread(thrd_h);
     85        ScheduleThread(this_thrd);
    8986        enable_interrupts( __cfaabi_dbg_ctx );
    9087}
     
    9289extern "C" {
    9390        // KERNEL ONLY
    94         void __finish_creation(coroutine_desc * thrd_c) {
    95                 ThreadCtxSwitch( thrd_c, thrd_c->last );
     91        void __finish_creation(thread_desc * this) {
     92                // set new coroutine that the processor is executing
     93                // and context switch to it
     94                verify( kernelTLS.this_thread != this );
     95                verify( kernelTLS.this_thread->context.SP );
     96                CtxSwitch( &this->context, &kernelTLS.this_thread->context );
    9697        }
    9798}
     
    111112}
    112113
    113 // KERNEL ONLY
    114 void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
    115         // set state of current coroutine to inactive
    116         src->state = src->state == Halted ? Halted : Inactive;
    117         dst->state = Active;
    118 
    119         // set new coroutine that the processor is executing
    120         // and context switch to it
    121         verify( dst->context.SP );
    122         CtxSwitch( &src->context, &dst->context );
    123 
    124         // set state of new coroutine to active
    125         dst->state = dst->state == Halted ? Halted : Inactive;
    126         src->state = Active;
    127 }
    128 
    129114// Local Variables: //
    130115// mode: c //
Note: See TracChangeset for help on using the changeset viewer.