Changeset 75f3522 for src/libcfa


Ignore:
Timestamp:
Feb 13, 2017, 2:39:26 PM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
db6f06a
Parents:
fb7dca0
Message:

Moved several declarations to a new private header and made some clean-up

Location:
src/libcfa/concurrency
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/concurrency/kernel

    rfb7dca0 r75f3522  
    99//
    1010// Author           : Thierry Delisle
    11 // Created On       : Tue Jan 17 12:27:26 2016
     11// Created On       : Tue Jan 17 12:27:26 2017
    1212// Last Modified By : Thierry Delisle
    1313// Last Modified On : --
     
    5858void ^?{}(processor * this);
    5959
    60 
    6160//-----------------------------------------------------------------------------
    6261// Locks
  • src/libcfa/concurrency/kernel.c

    rfb7dca0 r75f3522  
    99//
    1010// Author           : Thierry Delisle
    11 // Created On       : Tue Jan 17 12:27:26 2016
     11// Created On       : Tue Jan 17 12:27:26 2017
    1212// Last Modified By : Thierry Delisle
    1313// Last Modified On : --
     
    2020
    2121//Header
    22 #include "kernel"
     22#include "kernel_private.h"
    2323
    2424//C Includes
     
    3131//CFA Includes
    3232#include "libhdr.h"
    33 #include "threads"
    3433
    3534//Private includes
     
    5150//-----------------------------------------------------------------------------
    5251// Kernel storage
    53 struct processorCtx_t {
    54         processor * proc;
    55         coroutine c;
    56 };
    57 
    58 DECL_COROUTINE(processorCtx_t);
    59 
    6052#define KERNEL_STORAGE(T,X) static char X##_storage[sizeof(T)]
    6153
     
    194186}
    195187
    196 //-----------------------------------------------------------------------------
    197 // Processor running routines
    198 void main(processorCtx_t *);
    199 thread * nextThread(cluster * this);
    200 void scheduleInternal(processor * this, thread * dst);
    201 void spin(processor * this, unsigned int * spin_count);
    202 void thread_schedule( thread * thrd );
    203 
     188//=============================================================================================
     189// Kernel Scheduling logic
     190//=============================================================================================
    204191//Main of the processor contexts
    205192void main(processorCtx_t * runner) {
     
    212199
    213200        thread * readyThread = NULL;
    214         for( unsigned int spin_count = 0; ! this->terminated; spin_count++ ) {
    215                
     201        for( unsigned int spin_count = 0; ! this->terminated; spin_count++ )
     202        {
    216203                readyThread = nextThread( this->cltr );
    217204
    218                 if(readyThread) {
    219                         scheduleInternal(this, readyThread);
     205                if(readyThread)
     206                {
     207                        runThread(this, readyThread);
     208
     209                        //Some actions need to be taken from the kernel
     210                        finishRunning(this, readyThread);
     211
    220212                        spin_count = 0;
    221                 } else {
     213                }
     214                else
     215                {
    222216                        spin(this, &spin_count);
    223217                }               
     
    229223}
    230224
    231 //Declarations for scheduleInternal
    232 extern void ThreadCtxSwitch(coroutine * src, coroutine * dst);
    233 
    234 // scheduleInternal runs a thread by context switching
     225// runThread runs a thread by context switching
    235226// from the processor coroutine to the target thread
    236 void scheduleInternal(processor * this, thread * dst) {
     227void runThread(processor * this, thread * dst) {
     228        coroutine * proc_cor = get_coroutine(this->runner);
     229        coroutine * thrd_cor = get_coroutine(dst);
     230       
     231        //Reset the terminating actions here
    237232        this->thread_action = NoAction;
    238233
    239         // coroutine * proc_ctx = get_coroutine(this->ctx);
    240         // coroutine * thrd_ctx = get_coroutine(dst);
    241 
    242         // //Update global state
    243         // this->current_thread = dst;
    244 
    245         // // Context Switch to the thread
    246         // ThreadCtxSwitch(proc_ctx, thrd_ctx);
    247         // // when ThreadCtxSwitch returns we are back in the processor coroutine
    248 
    249         coroutine * proc_ctx = get_coroutine(this->runner);
    250         coroutine * thrd_ctx = get_coroutine(dst);
    251       thrd_ctx->last = proc_ctx;
    252  
    253       // context switch to specified coroutine
    254       // Which is now the current_coroutine
    255       // LIB_DEBUG_PRINTF("Kernel : switching to ctx %p (from %p, current %p)\n", thrd_ctx, proc_ctx, this->current_coroutine);
    256       this->current_thread = dst;
    257       this->current_coroutine = thrd_ctx;
    258       CtxSwitch( proc_ctx->stack.context, thrd_ctx->stack.context );
    259       this->current_coroutine = proc_ctx;
    260       // LIB_DEBUG_PRINTF("Kernel : returned from ctx %p (to %p, current %p)\n", thrd_ctx, proc_ctx, this->current_coroutine);
    261  
    262       // when CtxSwitch returns we are back in the processor coroutine
     234        //Update global state
     235        this->current_thread = dst;
     236
     237        // Context Switch to the thread
     238        ThreadCtxSwitch(proc_cor, thrd_cor);
     239        // when ThreadCtxSwitch returns we are back in the processor coroutine
     240}
     241
     242// Once a thread has finished running, some of
     243// its final actions must be executed from the kernel
     244void finishRunning(processor * this, thread * thrd) {
    263245        if(this->thread_action == Reschedule) {
    264                 thread_schedule( dst );
     246                ScheduleThread( thrd );
    265247        }
    266248}
     
    325307//-----------------------------------------------------------------------------
    326308// Scheduler routines
    327 void thread_schedule( thread * thrd ) {
     309void ScheduleThread( thread * thrd ) {
    328310        assertf( thrd->next == NULL, "Expected null got %p", thrd->next );
    329311       
     
    331313        append( &systemProcessor->cltr->ready_queue, thrd );
    332314        spin_unlock( &lock );
     315}
     316
     317void ScheduleInternal() {
     318        get_this_processor()->thread_action = Reschedule;
     319        suspend();
    333320}
    334321
     
    363350        // Add the main thread to the ready queue
    364351        // once resume is called on systemProcessor->ctx the mainThread needs to be scheduled like any normal thread
    365         thread_schedule(mainThread);
     352        ScheduleThread(mainThread);
    366353
    367354        //initialize the global state variables
     
    426413        thread * it;
    427414        while( it = pop_head( &this->blocked) ) {
    428                 thread_schedule( it );
     415                ScheduleThread( it );
    429416        }
    430417}
  • src/libcfa/concurrency/threads.c

    rfb7dca0 r75f3522  
    1717#include "threads"
    1818
    19 #include "kernel"
     19#include "kernel_private.h"
    2020#include "libhdr.h"
    2121
     
    7272//-----------------------------------------------------------------------------
    7373// Starting and stopping threads
    74 extern "C" {
    75       forall(dtype T | is_thread(T))
    76       void CtxInvokeThread(T * this);
    77 }
    78 
    79 extern void thread_schedule( thread * );
    80 
    8174forall( dtype T | is_thread(T) )
    8275void start( T* this ) {
     
    9285        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
    9386
    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);
     87        ScheduleThread(thrd_h);
    10088}
    10189
     
    10997
    11098void yield( void ) {
    111         get_this_processor()->thread_action = Reschedule;
    112         suspend();
     99        ScheduleInternal();
    113100}
    114101
    115102void ThreadCtxSwitch(coroutine* src, coroutine* dst) {
     103        // set state of current coroutine to inactive
     104        src->state = Inactive;
     105        dst->state = Active;
     106
     107        //update the last resumer
    116108        dst->last = src;
    117109
    118         // set state of current coroutine to inactive
    119         src->state = Inactive;
    120 
    121         // set new coroutine that task is executing
     110        // set new coroutine that the processor is executing
     111        // and context switch to it
    122112        get_this_processor()->current_coroutine = dst; 
    123 
    124         // context switch to specified coroutine
    125113        CtxSwitch( src->stack.context, dst->stack.context );
    126         // when CtxSwitch returns we are back in the src coroutine
     114        get_this_processor()->current_coroutine = src; 
    127115
    128116        // set state of new coroutine to active
     117        dst->state = Inactive;
    129118        src->state = Active;
    130119}
Note: See TracChangeset for help on using the changeset viewer.