Ignore:
File:
1 edited

Legend:

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

    rc84b4be re3fea42  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Dec  5 16:25:52 2019
    13 // Update Count     : 52
     12// Last Modified On : Tue Feb  4 13:03:15 2020
     13// Update Count     : 58
    1414//
    1515
     
    187187        self_mon.recursion = 1;
    188188        self_mon_p = &self_mon;
    189         link.next = 0p;
    190         link.prev = 0p;
     189        next = 0p;
    191190
    192191        node.next = 0p;
     
    210209
    211210static void start(processor * this);
    212 void ?{}(processor & this, const char * name, cluster & cltr) with( this ) {
     211void ?{}(processor & this, const char name[], cluster & cltr) with( this ) {
    213212        this.name = name;
    214213        this.cltr = &cltr;
    215         id = -1u;
    216214        terminated{ 0 };
    217215        do_terminate = false;
     
    240238}
    241239
    242 void ?{}(cluster & this, const char * name, Duration preemption_rate) with( this ) {
     240void ?{}(cluster & this, const char name[], Duration preemption_rate) with( this ) {
    243241        this.name = name;
    244242        this.preemption_rate = preemption_rate;
    245243        ready_queue{};
    246         ready_lock{};
    247 
     244        ready_queue_lock{};
     245
     246        procs{ __get };
    248247        idles{ __get };
    249248        threads{ __get };
     
    274273        __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
    275274
    276         // register the processor unless it's the main thread which is handled in the boot sequence
    277         if(this != mainProcessor) {
    278                 this->id = doregister(this->cltr, this);
    279                 ready_queue_grow( this->cltr );
    280         }
    281 
     275        doregister(this->cltr, this);
    282276
    283277        {
     
    311305        }
    312306
     307        unregister(this->cltr, this);
     308
    313309        V( this->terminated );
    314310
    315 
    316         // unregister the processor unless it's the main thread which is handled in the boot sequence
    317         if(this != mainProcessor) {
    318                 ready_queue_shrink( this->cltr );
    319                 unregister(this->cltr, this);
    320         }
    321 
    322311        __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
    323 
    324         stats_tls_tally(this->cltr);
    325312}
    326313
     
    454441}
    455442
    456 static void Abort( int ret, const char * func ) {
     443static void Abort( int ret, const char func[] ) {
    457444        if ( ret ) {                                                                            // pthread routines return errno values
    458445                abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) );
     
    482469        );
    483470
    484         Abort( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
     471        Abort( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" ); 
    485472
    486473        Abort( pthread_create( pthread, &attr, start, arg ), "pthread_create" );
     
    548535        verify( ! kernelTLS.preemption_state.enabled );
    549536
    550         verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next );
    551 
    552 
    553         ready_schedule_lock(thrd->curr_cluster, kernelTLS.this_processor);
    554                 bool was_empty = push( thrd->curr_cluster, thrd );
    555         ready_schedule_unlock(thrd->curr_cluster, kernelTLS.this_processor);
     537        verifyf( thrd->next == 0p, "Expected null got %p", thrd->next );
    556538
    557539        with( *thrd->curr_cluster ) {
    558                 // if(was_empty) {
    559                 //      lock      (proc_list_lock __cfaabi_dbg_ctx2);
    560                 //      if(idles) {
    561                 //              wake_fast(idles.head);
    562                 //      }
    563                 //      unlock    (proc_list_lock);
    564                 // }
    565                 // else if( struct processor * idle = idles.head ) {
    566                 //      wake_fast(idle);
    567                 // }
     540                lock  ( ready_queue_lock __cfaabi_dbg_ctx2 );
     541                bool was_empty = !(ready_queue != 0);
     542                append( ready_queue, thrd );
     543                unlock( ready_queue_lock );
     544
     545                if(was_empty) {
     546                        lock      (proc_list_lock __cfaabi_dbg_ctx2);
     547                        if(idles) {
     548                                wake_fast(idles.head);
     549                        }
     550                        unlock    (proc_list_lock);
     551                }
     552                else if( struct processor * idle = idles.head ) {
     553                        wake_fast(idle);
     554                }
     555
    568556        }
    569557
     
    574562thread_desc * nextThread(cluster * this) with( *this ) {
    575563        verify( ! kernelTLS.preemption_state.enabled );
    576 
    577         ready_schedule_lock(this, kernelTLS.this_processor);
    578                 thread_desc * head = pop( this );
    579         ready_schedule_unlock(this, kernelTLS.this_processor);
    580 
     564        lock( ready_queue_lock __cfaabi_dbg_ctx2 );
     565        thread_desc * head = pop_head( ready_queue );
     566        unlock( ready_queue_lock );
    581567        verify( ! kernelTLS.preemption_state.enabled );
    582568        return head;
     
    740726                pending_preemption = false;
    741727                kernel_thread = pthread_self();
    742                 id = -1u;
    743728
    744729                runner{ &this };
     
    750735        mainProcessor = (processor *)&storage_mainProcessor;
    751736        (*mainProcessor){};
    752 
    753         mainProcessor->id = doregister(mainCluster, mainProcessor);
    754737
    755738        //initialize the global state variables
     
    798781        kernel_stop_preemption();
    799782
    800         unregister(mainCluster, mainProcessor);
    801 
    802783        // Destroy the main processor and its context in reverse order of construction
    803784        // These were manually constructed so we need manually destroy them
    804         void ^?{}(processor & this) with( this ) {
    805                 //don't join the main thread here, that wouldn't make any sense
    806                 __cfaabi_dbg_print_safe("Kernel : destroyed main processor context %p\n", &runner);
    807         }
    808 
    809         ^(*mainProcessor){};
     785        ^(mainProcessor->runner){};
     786        ^(mainProcessor){};
    810787
    811788        // Final step, destroy the main thread since it is no longer needed
    812         // Since we provided a stack to this task it will not destroy anything
    813         ^(*mainThread){};
    814 
    815         ^(*mainCluster){};
     789        // Since we provided a stack to this taxk it will not destroy anything
     790        ^(mainThread){};
    816791
    817792        ^(__cfa_dbg_global_clusters.list){};
     
    825800//=============================================================================================
    826801static void halt(processor * this) with( *this ) {
    827         // // verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
    828 
    829         // with( *cltr ) {
    830         //      lock      (proc_list_lock __cfaabi_dbg_ctx2);
    831         //      push_front(idles, *this);
    832         //      unlock    (proc_list_lock);
    833         // }
    834 
    835         // __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this);
    836 
    837         // wait( idleLock );
    838 
    839         // __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this);
    840 
    841         // with( *cltr ) {
    842         //      lock      (proc_list_lock __cfaabi_dbg_ctx2);
    843         //      remove    (idles, *this);
    844         //      unlock    (proc_list_lock);
    845         // }
     802        // verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
     803
     804        with( *cltr ) {
     805                lock      (proc_list_lock __cfaabi_dbg_ctx2);
     806                remove    (procs, *this);
     807                push_front(idles, *this);
     808                unlock    (proc_list_lock);
     809        }
     810
     811        __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this);
     812
     813        wait( idleLock );
     814
     815        __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this);
     816
     817        with( *cltr ) {
     818                lock      (proc_list_lock __cfaabi_dbg_ctx2);
     819                remove    (idles, *this);
     820                push_front(procs, *this);
     821                unlock    (proc_list_lock);
     822        }
    846823}
    847824
     
    864841                sigemptyset( &mask );
    865842                sigaddset( &mask, SIGALRM );            // block SIGALRM signals
    866                 sigsuspend( &mask );                    // block the processor to prevent further damage during abort
    867                 _exit( EXIT_FAILURE );                  // if processor unblocks before it is killed, terminate it
     843                sigaddset( &mask, SIGUSR1 );            // block SIGALRM signals
     844                sigsuspend( &mask );                            // block the processor to prevent further damage during abort
     845                _exit( EXIT_FAILURE );                          // if processor unblocks before it is killed, terminate it
    868846        }
    869847        else {
     
    982960}
    983961
     962void doregister( cluster * cltr, processor * proc ) {
     963        lock      (cltr->proc_list_lock __cfaabi_dbg_ctx2);
     964        cltr->nprocessors += 1;
     965        push_front(cltr->procs, *proc);
     966        unlock    (cltr->proc_list_lock);
     967}
     968
     969void unregister( cluster * cltr, processor * proc ) {
     970        lock  (cltr->proc_list_lock __cfaabi_dbg_ctx2);
     971        remove(cltr->procs, *proc );
     972        cltr->nprocessors -= 1;
     973        unlock(cltr->proc_list_lock);
     974}
     975
    984976//-----------------------------------------------------------------------------
    985977// Debug
    986978__cfaabi_dbg_debug_do(
    987979        extern "C" {
    988                 void __cfaabi_dbg_record(__spinlock_t & this, const char * prev_name) {
     980                void __cfaabi_dbg_record(__spinlock_t & this, const char prev_name[]) {
    989981                        this.prev_name = prev_name;
    990982                        this.prev_thrd = kernelTLS.this_thread;
Note: See TracChangeset for help on using the changeset viewer.