Ignore:
File:
1 edited

Legend:

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

    r7768b8d r1c40091  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Jun 20 17:21:23 2019
    13 // Update Count     : 25
     12// Last Modified On : Thu Nov 21 16:46:59 2019
     13// Update Count     : 27
    1414//
    1515
     
    133133        NULL,
    134134        NULL,
    135         { 1, false, false },
    136         6u //this should be seeded better but due to a bug calling rdtsc doesn't work
     135        { 1, false, false }
    137136};
    138137
     
    210209        this.name = name;
    211210        this.cltr = &cltr;
    212         id = -1u;
    213211        terminated{ 0 };
    214212        do_terminate = false;
     
    240238        this.preemption_rate = preemption_rate;
    241239        ready_queue{};
    242         ready_lock{};
    243 
     240        ready_queue_lock{};
     241
     242        procs{ __get };
    244243        idles{ __get };
    245244        threads{ __get };
     
    261260//Main of the processor contexts
    262261void 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 
    267262        processor * this = runner.proc;
    268263        verify(this);
     
    270265        __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
    271266
    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);
     267        doregister(this->cltr, this);
    275268
    276269        {
     
    308301        }
    309302
     303        unregister(this->cltr, this);
     304
    310305        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);
    315306
    316307        __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
     
    509500
    510501        with( *thrd->curr_cluster ) {
    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);
     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 );
    517506
    518507                if(was_empty) {
     
    535524thread_desc * nextThread(cluster * this) with( *this ) {
    536525        verify( ! kernelTLS.preemption_state.enabled );
    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 
     526        lock( ready_queue_lock __cfaabi_dbg_ctx2 );
     527        thread_desc * head = pop_head( ready_queue );
     528        unlock( ready_queue_lock );
    546529        verify( ! kernelTLS.preemption_state.enabled );
    547530        return head;
     
    705688                pending_preemption = false;
    706689                kernel_thread = pthread_self();
    707                 id = -1u;
    708690
    709691                runner{ &this };
     
    715697        mainProcessor = (processor *)&storage_mainProcessor;
    716698        (*mainProcessor){};
    717 
    718         mainProcessor->id = doregister(mainCluster, mainProcessor);
    719699
    720700        //initialize the global state variables
     
    763743        kernel_stop_preemption();
    764744
    765         unregister(mainCluster, mainProcessor);
    766 
    767745        // Destroy the main processor and its context in reverse order of construction
    768746        // These were manually constructed so we need manually destroy them
    769747        ^(mainProcessor->runner){};
    770         ^(*mainProcessor){};
     748        ^(mainProcessor){};
    771749
    772750        // Final step, destroy the main thread since it is no longer needed
    773         // Since we provided a stack to this task it will not destroy anything
    774         ^(*mainThread){};
    775 
    776         ^(*mainCluster){};
     751        // Since we provided a stack to this taxk it will not destroy anything
     752        ^(mainThread){};
    777753
    778754        ^(__cfa_dbg_global_clusters.list){};
     
    790766        with( *cltr ) {
    791767                lock      (proc_list_lock __cfaabi_dbg_ctx2);
     768                remove    (procs, *this);
    792769                push_front(idles, *this);
    793770                unlock    (proc_list_lock);
     
    803780                lock      (proc_list_lock __cfaabi_dbg_ctx2);
    804781                remove    (idles, *this);
     782                push_front(procs, *this);
    805783                unlock    (proc_list_lock);
    806784        }
     
    841819        if(thrd) {
    842820                int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
    843                 __cfaabi_dbg_bits_write( abort_text, len );
     821                __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
    844822
    845823                if ( &thrd->self_cor != thrd->curr_cor ) {
    846824                        len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
    847                         __cfaabi_dbg_bits_write( abort_text, len );
     825                        __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
    848826                }
    849827                else {
    850                         __cfaabi_dbg_bits_write( ".\n", 2 );
     828                        __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
    851829                }
    852830        }
    853831        else {
    854832                int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
    855                 __cfaabi_dbg_bits_write( abort_text, len );
     833                __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
    856834        }
    857835}
     
    864842
    865843extern "C" {
    866         void __cfaabi_dbg_bits_acquire() {
     844        void __cfaabi_bits_acquire() {
    867845                lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
    868846        }
    869847
    870         void __cfaabi_dbg_bits_release() {
     848        void __cfaabi_bits_release() {
    871849                unlock( kernel_debug_lock );
    872850        }
     
    943921}
    944922
     923void 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
     930void 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
    945937//-----------------------------------------------------------------------------
    946938// Debug
Note: See TracChangeset for help on using the changeset viewer.