Ignore:
Timestamp:
Jun 6, 2017, 2:50:57 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
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:
c6d2e93
Parents:
8ca3a72 (diff), c5ac6d5 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

File:
1 edited

Legend:

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

    r8ca3a72 r10e90cb  
    3636//CFA Includes
    3737#include "libhdr.h"
     38#include "preemption.h"
    3839
    3940//Private includes
     
    4748KERNEL_STORAGE(processorCtx_t, systemProcessorCtx);
    4849KERNEL_STORAGE(cluster, systemCluster);
    49 KERNEL_STORAGE(processor, systemProcessor);
     50KERNEL_STORAGE(system_proc_t, systemProcessor);
    5051KERNEL_STORAGE(thread_desc, mainThread);
    5152KERNEL_STORAGE(machine_context_t, mainThread_context);
    5253
    5354cluster * systemCluster;
    54 processor * systemProcessor;
     55system_proc_t * systemProcessor;
    5556thread_desc * mainThread;
    5657
     
    118119// Processor coroutine
    119120void ?{}(processorCtx_t * this, processor * proc) {
    120         (&this->__cor){};
     121        (&this->__cor){ "Processor" };
    121122        this->proc = proc;
    122123        proc->runner = this;
     
    139140        (&this->terminated){};
    140141        this->is_terminated = false;
     142        this->preemption_alarm = NULL;
     143        this->preemption = default_preemption();
     144        this->disable_preempt_count = 1;                //Start with interrupts disabled
     145        this->pending_preemption = false;
    141146
    142147        start( this );
     
    149154        (&this->terminated){};
    150155        this->is_terminated = false;
     156        this->disable_preempt_count = 0;
     157        this->pending_preemption = false;
    151158
    152159        this->runner = runner;
    153160        LIB_DEBUG_PRINT_SAFE("Kernel : constructing processor context %p\n", runner);
    154161        runner{ this };
     162}
     163
     164void ?{}(system_proc_t * this, cluster * cltr, processorCtx_t * runner) {
     165        (&this->alarms){};
     166        (&this->alarm_lock){};
     167        this->pending_alarm = false;
     168
     169        (&this->proc){ cltr, runner };
    155170}
    156171
     
    178193void main(processorCtx_t * runner) {
    179194        processor * this = runner->proc;
     195
    180196        LIB_DEBUG_PRINT_SAFE("Kernel : core %p starting\n", this);
    181197
    182         thread_desc * readyThread = NULL;
    183         for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ )
    184198        {
    185                 readyThread = nextThread( this->cltr );
    186 
    187                 if(readyThread)
     199                // Setup preemption data
     200                preemption_scope scope = { this };
     201
     202                LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);
     203
     204                thread_desc * readyThread = NULL;
     205                for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ )
    188206                {
    189                         runThread(this, readyThread);
    190 
    191                         //Some actions need to be taken from the kernel
    192                         finishRunning(this);
    193 
    194                         spin_count = 0;
    195                 }
    196                 else
    197                 {
    198                         spin(this, &spin_count);
    199                 }               
    200         }
    201 
    202         LIB_DEBUG_PRINT_SAFE("Kernel : core %p unlocking thread\n", this);
     207                        readyThread = nextThread( this->cltr );
     208
     209                        if(readyThread)
     210                        {
     211                                runThread(this, readyThread);
     212
     213                                //Some actions need to be taken from the kernel
     214                                finishRunning(this);
     215
     216                                spin_count = 0;
     217                        }
     218                        else
     219                        {
     220                                spin(this, &spin_count);
     221                        }
     222                }
     223
     224                LIB_DEBUG_PRINT_SAFE("Kernel : core %p stopping\n", this);
     225        }
     226
    203227        signal( &this->terminated );
    204228        LIB_DEBUG_PRINT_SAFE("Kernel : core %p terminated\n", this);
     
    299323        LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this);
    300324       
    301         // pthread_attr_t attributes;
    302         // pthread_attr_init( &attributes );
    303 
    304325        pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
    305 
    306         // pthread_attr_destroy( &attributes );
    307326
    308327        LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);       
     
    316335        assertf( thrd->next == NULL, "Expected null got %p", thrd->next );
    317336       
    318         lock( &systemProcessor->cltr->lock );
    319         append( &systemProcessor->cltr->ready_queue, thrd );
    320         unlock( &systemProcessor->cltr->lock );
     337        lock( &systemProcessor->proc.cltr->lock );
     338        append( &systemProcessor->proc.cltr->ready_queue, thrd );
     339        unlock( &systemProcessor->proc.cltr->lock );
    321340}
    322341
     
    367386}
    368387
     388//=============================================================================================
     389// Kernel Setup logic
     390//=============================================================================================
    369391//-----------------------------------------------------------------------------
    370392// Kernel boot procedures
     
    379401        mainThread{ &info };
    380402
     403        LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n");
     404
     405        // Enable preemption
     406        kernel_start_preemption();
     407
    381408        // Initialize the system cluster
    382409        systemCluster = (cluster *)&systemCluster_storage;
    383410        systemCluster{};
    384411
     412        LIB_DEBUG_PRINT_SAFE("Kernel : System cluster ready\n");
     413
    385414        // Initialize the system processor and the system processor ctx
    386415        // (the coroutine that contains the processing control flow)
    387         systemProcessor = (processor *)&systemProcessor_storage;
     416        systemProcessor = (system_proc_t *)&systemProcessor_storage;
    388417        systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtx_storage };
    389418
    390419        // Add the main thread to the ready queue
    391         // once resume is called on systemProcessor->ctx the mainThread needs to be scheduled like any normal thread
     420        // once resume is called on systemProcessor->runner the mainThread needs to be scheduled like any normal thread
    392421        ScheduleThread(mainThread);
    393422
    394423        //initialize the global state variables
    395         this_processor = systemProcessor;
     424        this_processor = &systemProcessor->proc;
    396425        this_processor->current_thread = mainThread;
    397426        this_processor->current_coroutine = &mainThread->cor;
     
    400429        // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
    401430        // mainThread is on the ready queue when this call is made.
    402         resume(systemProcessor->runner);
     431        resume( systemProcessor->proc.runner );
    403432
    404433
     
    414443        // When its coroutine terminates, it return control to the mainThread
    415444        // which is currently here
    416         systemProcessor->is_terminated = true;
     445        systemProcessor->proc.is_terminated = true;
    417446        suspend();
    418447
     
    421450        // Destroy the system processor and its context in reverse order of construction
    422451        // These were manually constructed so we need manually destroy them
    423         ^(systemProcessor->runner){};
     452        ^(systemProcessor->proc.runner){};
    424453        ^(systemProcessor){};
    425454
     
    484513}
    485514
     515//=============================================================================================
     516// Kernel Utilities
     517//=============================================================================================
    486518//-----------------------------------------------------------------------------
    487519// Locks
     
    491523void ^?{}( spinlock * this ) {
    492524
     525}
     526
     527bool try_lock( spinlock * this ) {
     528        return this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0;
    493529}
    494530
Note: See TracChangeset for help on using the changeset viewer.