Ignore:
File:
1 edited

Legend:

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

    ree897e4b r8fcbb4c  
    1717#include "threads"
    1818
    19 #include "kernel_private.h"
     19#include "kernel"
    2020#include "libhdr.h"
    2121
     
    4444        (&this->c){};
    4545        this->c.name = "Anonymous Coroutine";
    46         (&this->terminated){};
     46        (&this->lock){};
    4747        this->next = NULL;
    4848}
     
    7272//-----------------------------------------------------------------------------
    7373// Starting and stopping threads
     74extern "C" {
     75      forall(dtype T | is_thread(T))
     76      void CtxInvokeThread(T * this);
     77}
     78
     79extern void thread_schedule( thread * );
     80
    7481forall( dtype T | is_thread(T) )
    7582void start( T* this ) {
     
    8592        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
    8693
    87         ScheduleThread(thrd_h);
     94        fenv_t envp;
     95        fegetenv( &envp );
     96        LIB_DEBUG_PRINTF("Thread : mxcsr %x\n", envp.__mxcsr);
     97        LIB_DEBUG_PRINTF("Thread started : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
     98
     99        thread_schedule(thrd_h);
    88100}
    89101
    90102forall( dtype T | is_thread(T) )
    91103void stop( T* this ) {
    92         wait( & get_thread(this)->terminated );
     104        thread*  thrd = get_thread(this);
     105        if( thrd->c.notHalted ) {
     106                lock( &thrd->lock );
     107        }
    93108}
    94109
    95110void yield( void ) {
    96         ScheduleInternal( get_this_processor()->current_thread );
     111        get_this_processor()->thread_action = Reschedule;
     112        suspend();
    97113}
    98114
    99115void ThreadCtxSwitch(coroutine* src, coroutine* dst) {
     116        dst->last = src;
     117
    100118        // set state of current coroutine to inactive
    101119        src->state = Inactive;
    102         dst->state = Active;
    103120
    104         //update the last resumer
    105         dst->last = src;
     121        // set new coroutine that task is executing
     122        get_this_processor()->current_coroutine = dst; 
    106123
    107         // set new coroutine that the processor is executing
    108         // and context switch to it
    109         get_this_processor()->current_coroutine = dst; 
     124        // context switch to specified coroutine
    110125        CtxSwitch( src->stack.context, dst->stack.context );
    111         get_this_processor()->current_coroutine = src; 
     126        // when CtxSwitch returns we are back in the src coroutine
    112127
    113128        // set state of new coroutine to active
    114         dst->state = Inactive;
    115129        src->state = Active;
    116130}
     
    120134extern "C" {
    121135        void __thread_signal_termination( thread * this ) {
    122                 this->c.state = Halted;
    123                 LIB_DEBUG_PRINTF("Thread end : %p\n", this);
    124                 signal( &this->terminated );   
     136                this->c.state = Halt;
     137                this->c.notHalted = false;
     138                unlock( &this->lock );
    125139        }
    126140}
Note: See TracChangeset for help on using the changeset viewer.