Ignore:
Timestamp:
Jul 26, 2017, 12:19:41 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:
b947fb2
Parents:
e0a653d (diff), ea91c42 (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:software/cfa/cfa-cc

File:
1 edited

Legend:

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

    re0a653d r33218c6  
    1 //                              -*- Mode: CFA -*-
    21//
    32// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
     
    109// Author           : Thierry Delisle
    1110// Created On       : Tue Jan 17 12:27:26 2017
    12 // Last Modified By : Thierry Delisle
    13 // Last Modified On : --
    14 // Update Count     : 0
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Fri Jul 21 22:33:18 2017
     13// Update Count     : 2
    1514//
    1615
     
    4241//-----------------------------------------------------------------------------
    4342// Kernel storage
    44 #define KERNEL_STORAGE(T,X) static char X##Storage[sizeof(T)]
    45 
    46 KERNEL_STORAGE(processorCtx_t, systemProcessorCtx);
    47 KERNEL_STORAGE(cluster, systemCluster);
    48 KERNEL_STORAGE(system_proc_t, systemProcessor);
    49 KERNEL_STORAGE(thread_desc, mainThread);
     43KERNEL_STORAGE(cluster,           mainCluster);
     44KERNEL_STORAGE(processor,         mainProcessor);
     45KERNEL_STORAGE(processorCtx_t,    mainProcessorCtx);
     46KERNEL_STORAGE(thread_desc,       mainThread);
    5047KERNEL_STORAGE(machine_context_t, mainThreadCtx);
    5148
    52 cluster * systemCluster;
    53 system_proc_t * systemProcessor;
     49cluster *     mainCluster;
     50processor *   mainProcessor;
    5451thread_desc * mainThread;
    5552
     
    5754// Global state
    5855
    59 volatile thread_local processor * this_processor;
    60 volatile thread_local coroutine_desc * this_coroutine;
    61 volatile thread_local thread_desc * this_thread;
     56thread_local coroutine_desc * volatile this_coroutine;
     57thread_local thread_desc *    volatile this_thread;
     58thread_local processor *      volatile this_processor;
     59
     60volatile thread_local bool preemption_in_progress = 0;
    6261volatile thread_local unsigned short disable_preempt_count = 1;
    6362
     
    8483
    8584        this->limit = (void *)(((intptr_t)this->base) - this->size);
    86         this->context = &mainThreadCtxStorage;
     85        this->context = &storage_mainThreadCtx;
    8786        this->top = this->base;
    8887}
     
    124123
    125124void ?{}(processor * this) {
    126         this{ systemCluster };
     125        this{ mainCluster };
    127126}
    128127
     
    130129        this->cltr = cltr;
    131130        (&this->terminated){ 0 };
    132         this->is_terminated = false;
     131        this->do_terminate = false;
    133132        this->preemption_alarm = NULL;
    134         this->preemption = default_preemption();
    135133        this->pending_preemption = false;
    136134
     
    141139        this->cltr = cltr;
    142140        (&this->terminated){ 0 };
    143         this->is_terminated = false;
     141        this->do_terminate = false;
    144142        this->preemption_alarm = NULL;
    145         this->preemption = default_preemption();
    146143        this->pending_preemption = false;
    147144        this->kernel_thread = pthread_self();
    148145
    149146        this->runner = runner;
    150         LIB_DEBUG_PRINT_SAFE("Kernel : constructing system processor context %p\n", runner);
     147        LIB_DEBUG_PRINT_SAFE("Kernel : constructing main processor context %p\n", runner);
    151148        runner{ this };
    152149}
    153150
    154 LIB_DEBUG_DO( bool validate( alarm_list_t * this ); )
    155 
    156 void ?{}(system_proc_t * this, cluster * cltr, processorCtx_t * runner) {
    157         (&this->alarms){};
    158         (&this->alarm_lock){};
    159         this->pending_alarm = false;
    160 
    161         (&this->proc){ cltr, runner };
    162 
    163         verify( validate( &this->alarms ) );
    164 }
    165 
    166151void ^?{}(processor * this) {
    167         if( ! this->is_terminated ) {
     152        if( ! this->do_terminate ) {
    168153                LIB_DEBUG_PRINT_SAFE("Kernel : core %p signaling termination\n", this);
    169                 this->is_terminated = true;
     154                this->do_terminate = true;
    170155                P( &this->terminated );
    171156                pthread_join( this->kernel_thread, NULL );
     
    175160void ?{}(cluster * this) {
    176161        ( &this->ready_queue ){};
    177         ( &this->lock ){};
     162        ( &this->ready_queue_lock ){};
     163
     164        this->preemption = default_preemption();
    178165}
    179166
     
    198185
    199186                thread_desc * readyThread = NULL;
    200                 for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ )
     187                for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ )
    201188                {
    202189                        readyThread = nextThread( this->cltr );
     
    342329        verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
    343330
    344         lock( &systemProcessor->proc.cltr->lock DEBUG_CTX2 );
    345         append( &systemProcessor->proc.cltr->ready_queue, thrd );
    346         unlock( &systemProcessor->proc.cltr->lock );
     331        lock(   &this_processor->cltr->ready_queue_lock DEBUG_CTX2 );
     332        append( &this_processor->cltr->ready_queue, thrd );
     333        unlock( &this_processor->cltr->ready_queue_lock );
    347334
    348335        verify( disable_preempt_count > 0 );
     
    351338thread_desc * nextThread(cluster * this) {
    352339        verify( disable_preempt_count > 0 );
    353         lock( &this->lock DEBUG_CTX2 );
     340        lock( &this->ready_queue_lock DEBUG_CTX2 );
    354341        thread_desc * head = pop_head( &this->ready_queue );
    355         unlock( &this->lock );
     342        unlock( &this->ready_queue_lock );
    356343        verify( disable_preempt_count > 0 );
    357344        return head;
     
    451438        // Start by initializing the main thread
    452439        // SKULLDUGGERY: the mainThread steals the process main thread
    453         // which will then be scheduled by the systemProcessor normally
    454         mainThread = (thread_desc *)&mainThreadStorage;
     440        // which will then be scheduled by the mainProcessor normally
     441        mainThread = (thread_desc *)&storage_mainThread;
    455442        current_stack_info_t info;
    456443        mainThread{ &info };
     
    458445        LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n");
    459446
    460         // Initialize the system cluster
    461         systemCluster = (cluster *)&systemClusterStorage;
    462         systemCluster{};
    463 
    464         LIB_DEBUG_PRINT_SAFE("Kernel : System cluster ready\n");
    465 
    466         // Initialize the system processor and the system processor ctx
     447        // Initialize the main cluster
     448        mainCluster = (cluster *)&storage_mainCluster;
     449        mainCluster{};
     450
     451        LIB_DEBUG_PRINT_SAFE("Kernel : main cluster ready\n");
     452
     453        // Initialize the main processor and the main processor ctx
    467454        // (the coroutine that contains the processing control flow)
    468         systemProcessor = (system_proc_t *)&systemProcessorStorage;
    469         systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtxStorage };
    470 
    471         // Add the main thread to the ready queue
    472         // once resume is called on systemProcessor->runner the mainThread needs to be scheduled like any normal thread
    473         ScheduleThread(mainThread);
     455        mainProcessor = (processor *)&storage_mainProcessor;
     456        mainProcessor{ mainCluster, (processorCtx_t *)&storage_mainProcessorCtx };
    474457
    475458        //initialize the global state variables
    476         this_processor = &systemProcessor->proc;
     459        this_processor = mainProcessor;
    477460        this_thread = mainThread;
    478461        this_coroutine = &mainThread->cor;
    479         disable_preempt_count = 1;
    480462
    481463        // Enable preemption
    482464        kernel_start_preemption();
    483465
    484         // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
     466        // Add the main thread to the ready queue
     467        // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
     468        ScheduleThread(mainThread);
     469
     470        // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
    485471        // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
    486472        // mainThread is on the ready queue when this call is made.
    487         resume( systemProcessor->proc.runner );
     473        resume( mainProcessor->runner );
    488474
    489475
     
    500486        disable_interrupts();
    501487
    502         // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
     488        // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
    503489        // When its coroutine terminates, it return control to the mainThread
    504490        // which is currently here
    505         systemProcessor->proc.is_terminated = true;
     491        mainProcessor->do_terminate = true;
    506492        suspend();
    507493
     
    511497        kernel_stop_preemption();
    512498
    513         // Destroy the system processor and its context in reverse order of construction
     499        // Destroy the main processor and its context in reverse order of construction
    514500        // These were manually constructed so we need manually destroy them
    515         ^(systemProcessor->proc.runner){};
    516         ^(systemProcessor){};
     501        ^(mainProcessor->runner){};
     502        ^(mainProcessor){};
    517503
    518504        // Final step, destroy the main thread since it is no longer needed
     
    698684        return top;
    699685}
     686
    700687// Local Variables: //
    701688// mode: c //
Note: See TracChangeset for help on using the changeset viewer.