Ignore:
File:
1 edited

Legend:

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

    re8e457e r58b6d1b  
    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 };
    3433        self_cor{ name, storage, storageSize };
    35         state = Start;
     34        verify(&self_cor);
    3635        curr_cor = &self_cor;
    3736        self_mon.owner = &this;
     
    7473forall( dtype T | is_thread(T) )
    7574void __thrd_start( T& this ) {
    76         thread_desc * this_thrd = get_thread(this);
    77         thread_desc * curr_thrd = TL_GET( this_thread );
     75        coroutine_desc* thrd_c = get_coroutine(this);
     76        thread_desc   * thrd_h = get_thread   (this);
     77        thrd_c->last = TL_GET( this_coroutine );
     78
     79        // __cfaabi_dbg_print_safe("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
    7880
    7981        disable_interrupts();
     82        create_stack(&thrd_c->stack, thrd_c->stack.size);
     83        kernelTLS.this_coroutine = thrd_c;
    8084        CtxStart(&this, CtxInvokeThread);
    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 );
     85        assert( thrd_c->last->stack.context );
     86        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
    8487
    85         ScheduleThread(this_thrd);
     88        ScheduleThread(thrd_h);
    8689        enable_interrupts( __cfaabi_dbg_ctx );
    8790}
     
    8992extern "C" {
    9093        // KERNEL ONLY
    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 );
     94        void __finish_creation(void) {
     95                coroutine_desc* thrd_c = kernelTLS.this_coroutine;
     96                ThreadCtxSwitch( thrd_c, thrd_c->last );
    9797        }
    9898}
     
    112112}
    113113
     114// KERNEL ONLY
     115void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
     116        // set state of current coroutine to inactive
     117        src->state = src->state == Halted ? Halted : Inactive;
     118        dst->state = Active;
     119
     120        // set new coroutine that the processor is executing
     121        // and context switch to it
     122        kernelTLS.this_coroutine = dst;
     123        assert( src->stack.context );
     124        CtxSwitch( src->stack.context, dst->stack.context );
     125        kernelTLS.this_coroutine = src;
     126
     127        // set state of new coroutine to active
     128        dst->state = dst->state == Halted ? Halted : Inactive;
     129        src->state = Active;
     130}
     131
    114132// Local Variables: //
    115133// mode: c //
Note: See TracChangeset for help on using the changeset viewer.