Ignore:
File:
1 edited

Legend:

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

    r1c40091 r7768b8d  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Nov 21 16:46:59 2019
    13 // Update Count     : 27
     12// Last Modified On : Thu Jun 20 17:21:23 2019
     13// Update Count     : 25
    1414//
    1515
     
    133133        NULL,
    134134        NULL,
    135         { 1, false, false }
     135        { 1, false, false },
     136        6u //this should be seeded better but due to a bug calling rdtsc doesn't work
    136137};
    137138
     
    209210        this.name = name;
    210211        this.cltr = &cltr;
     212        id = -1u;
    211213        terminated{ 0 };
    212214        do_terminate = false;
     
    238240        this.preemption_rate = preemption_rate;
    239241        ready_queue{};
    240         ready_queue_lock{};
    241 
    242         procs{ __get };
     242        ready_lock{};
     243
    243244        idles{ __get };
    244245        threads{ __get };
     
    260261//Main of the processor contexts
    261262void main(processorCtx_t & runner) {
     263        // Because of a bug, we couldn't initialized the seed on construction
     264        // Do it here
     265        kernelTLS.rand_seed ^= rdtscl();
     266
    262267        processor * this = runner.proc;
    263268        verify(this);
     
    265270        __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
    266271
    267         doregister(this->cltr, this);
     272        // register the processor unless it's the main thread which is handled in the boot sequence
     273        if(this != mainProcessor)
     274                this->id = doregister(this->cltr, this);
    268275
    269276        {
     
    301308        }
    302309
    303         unregister(this->cltr, this);
    304 
    305310        V( this->terminated );
     311
     312        // unregister the processor unless it's the main thread which is handled in the boot sequence
     313        if(this != mainProcessor)
     314                unregister(this->cltr, this);
    306315
    307316        __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
     
    500509
    501510        with( *thrd->curr_cluster ) {
    502                 lock  ( ready_queue_lock __cfaabi_dbg_ctx2 );
    503                 bool was_empty = !(ready_queue != 0);
    504                 append( ready_queue, thrd );
    505                 unlock( ready_queue_lock );
     511                ready_schedule_lock(*thrd->curr_cluster, kernelTLS.this_processor);
     512                __atomic_acquire(&ready_queue.lock);
     513                thrd->ts = rdtscl();
     514                bool was_empty = push( ready_queue, thrd );
     515                __atomic_unlock(&ready_queue.lock);
     516                ready_schedule_unlock(*thrd->curr_cluster, kernelTLS.this_processor);
    506517
    507518                if(was_empty) {
     
    524535thread_desc * nextThread(cluster * this) with( *this ) {
    525536        verify( ! kernelTLS.preemption_state.enabled );
    526         lock( ready_queue_lock __cfaabi_dbg_ctx2 );
    527         thread_desc * head = pop_head( ready_queue );
    528         unlock( ready_queue_lock );
     537
     538        ready_schedule_lock(*this, kernelTLS.this_processor);
     539                __atomic_acquire(&ready_queue.lock);
     540                        thread_desc * head;
     541                        __attribute__((unused)) bool _;
     542                        [head, _] = pop( ready_queue );
     543                __atomic_unlock(&ready_queue.lock);
     544        ready_schedule_unlock(*this, kernelTLS.this_processor);
     545
    529546        verify( ! kernelTLS.preemption_state.enabled );
    530547        return head;
     
    688705                pending_preemption = false;
    689706                kernel_thread = pthread_self();
     707                id = -1u;
    690708
    691709                runner{ &this };
     
    697715        mainProcessor = (processor *)&storage_mainProcessor;
    698716        (*mainProcessor){};
     717
     718        mainProcessor->id = doregister(mainCluster, mainProcessor);
    699719
    700720        //initialize the global state variables
     
    743763        kernel_stop_preemption();
    744764
     765        unregister(mainCluster, mainProcessor);
     766
    745767        // Destroy the main processor and its context in reverse order of construction
    746768        // These were manually constructed so we need manually destroy them
    747769        ^(mainProcessor->runner){};
    748         ^(mainProcessor){};
     770        ^(*mainProcessor){};
    749771
    750772        // Final step, destroy the main thread since it is no longer needed
    751         // Since we provided a stack to this taxk it will not destroy anything
    752         ^(mainThread){};
     773        // Since we provided a stack to this task it will not destroy anything
     774        ^(*mainThread){};
     775
     776        ^(*mainCluster){};
    753777
    754778        ^(__cfa_dbg_global_clusters.list){};
     
    766790        with( *cltr ) {
    767791                lock      (proc_list_lock __cfaabi_dbg_ctx2);
    768                 remove    (procs, *this);
    769792                push_front(idles, *this);
    770793                unlock    (proc_list_lock);
     
    780803                lock      (proc_list_lock __cfaabi_dbg_ctx2);
    781804                remove    (idles, *this);
    782                 push_front(procs, *this);
    783805                unlock    (proc_list_lock);
    784806        }
     
    819841        if(thrd) {
    820842                int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
    821                 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
     843                __cfaabi_dbg_bits_write( abort_text, len );
    822844
    823845                if ( &thrd->self_cor != thrd->curr_cor ) {
    824846                        len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
    825                         __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
     847                        __cfaabi_dbg_bits_write( abort_text, len );
    826848                }
    827849                else {
    828                         __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
     850                        __cfaabi_dbg_bits_write( ".\n", 2 );
    829851                }
    830852        }
    831853        else {
    832854                int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
    833                 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
     855                __cfaabi_dbg_bits_write( abort_text, len );
    834856        }
    835857}
     
    842864
    843865extern "C" {
    844         void __cfaabi_bits_acquire() {
     866        void __cfaabi_dbg_bits_acquire() {
    845867                lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
    846868        }
    847869
    848         void __cfaabi_bits_release() {
     870        void __cfaabi_dbg_bits_release() {
    849871                unlock( kernel_debug_lock );
    850872        }
     
    921943}
    922944
    923 void doregister( cluster * cltr, processor * proc ) {
    924         lock      (cltr->proc_list_lock __cfaabi_dbg_ctx2);
    925         cltr->nprocessors += 1;
    926         push_front(cltr->procs, *proc);
    927         unlock    (cltr->proc_list_lock);
    928 }
    929 
    930 void unregister( cluster * cltr, processor * proc ) {
    931         lock  (cltr->proc_list_lock __cfaabi_dbg_ctx2);
    932         remove(cltr->procs, *proc );
    933         cltr->nprocessors -= 1;
    934         unlock(cltr->proc_list_lock);
    935 }
    936 
    937945//-----------------------------------------------------------------------------
    938946// Debug
Note: See TracChangeset for help on using the changeset viewer.