Ignore:
File:
1 edited

Legend:

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

    ra7b486b r504a7dc  
    120120static void __run_thread(processor * this, $thread * dst);
    121121static $thread * __halt(processor * this);
    122 static bool __wake_one(cluster * cltr, bool was_empty);
     122static bool __wake_one(cluster * cltr);
    123123static bool __wake_proc(processor *);
    124124
     
    197197        self_mon.recursion = 1;
    198198        self_mon_p = &self_mon;
    199         next = 0p;
     199        link.next = 0p;
     200        link.prev = 0p;
    200201
    201202        node.next = 0p;
     
    223224        this.name = name;
    224225        this.cltr = &cltr;
     226        id = -1u;
    225227        terminated{ 0 };
    226228        destroyer = 0p;
     
    260262        this.preemption_rate = preemption_rate;
    261263        ready_queue{};
    262         ready_queue_lock{};
     264        ready_lock{};
    263265
    264266        #if !defined(__CFA_NO_STATISTICS__)
     
    295297        __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this);
    296298
     299        // register the processor unless it's the main thread which is handled in the boot sequence
     300        if(this != mainProcessor) {
     301                this->id = doregister2(this->cltr, this);
     302                ready_queue_grow( this->cltr );
     303        }
     304
    297305        doregister(this->cltr, this);
    298306
     
    318326                                /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    319327                                /* paranoid */ verifyf( readyThread->state == Ready || readyThread->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", readyThread->state, readyThread->preempted);
    320                                 /* paranoid */ verifyf( readyThread->next == 0p, "Expected null got %p", readyThread->next );
     328                                /* paranoid */ verifyf( readyThread->link.next == 0p, "Expected null got %p", readyThread->link.next );
    321329
    322330                                // We found a thread run it
     
    334342        V( this->terminated );
    335343
     344        // unregister the processor unless it's the main thread which is handled in the boot sequence
     345        if(this != mainProcessor) {
     346                ready_queue_shrink( this->cltr );
     347                unregister2(this->cltr, this);
     348        }
     349        else {
     350                // HACK : the coroutine context switch expects this_thread to be set
     351                // and it make sense for it to be set in all other cases except here
     352                // fake it
     353                kernelTLS.this_thread = mainThread;
     354        }
     355
    336356        __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this);
    337357
    338         // HACK : the coroutine context switch expects this_thread to be set
    339         // and it make sense for it to be set in all other cases except here
    340         // fake it
    341         if( this == mainProcessor ) kernelTLS.this_thread = mainThread;
     358        stats_tls_tally(this->cltr);
    342359}
    343360
     
    591608// Scheduler routines
    592609// KERNEL ONLY
    593 void __schedule_thread( $thread * thrd ) with( *thrd->curr_cluster ) {
     610void __schedule_thread( $thread * thrd ) {
     611        /* paranoid */ verify( thrd );
     612        /* paranoid */ verify( thrd->state != Halted );
    594613        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    595614        /* paranoid */ #if defined( __CFA_WITH_VERIFY__ )
    596         /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,
    597                           "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted );
    598         /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active || thrd->state == Rerun,
    599                           "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted );
     615        /* paranoid */  if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,
     616                                        "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted );
     617        /* paranoid */  if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active || thrd->state == Rerun,
     618                                        "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted );
    600619        /* paranoid */ #endif
    601         /* paranoid */ verifyf( thrd->next == 0p, "Expected null got %p", thrd->next );
     620        /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next );
    602621
    603622        if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready;
    604623
    605         lock  ( ready_queue_lock __cfaabi_dbg_ctx2 );
    606         bool was_empty = !(ready_queue != 0);
    607         append( ready_queue, thrd );
    608         unlock( ready_queue_lock );
    609 
    610         __wake_one(thrd->curr_cluster, was_empty);
     624        ready_schedule_lock(thrd->curr_cluster, kernelTLS.this_processor);
     625                push( thrd->curr_cluster, thrd );
     626
     627                __wake_one(thrd->curr_cluster);
     628        ready_schedule_unlock(thrd->curr_cluster, kernelTLS.this_processor);
    611629
    612630        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     
    617635        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    618636
    619         lock( ready_queue_lock __cfaabi_dbg_ctx2 );
    620         $thread * head = pop_head( ready_queue );
    621         unlock( ready_queue_lock );
     637        ready_schedule_lock(this, kernelTLS.this_processor);
     638                $thread * head = pop( this );
     639        ready_schedule_unlock(this, kernelTLS.this_processor);
    622640
    623641        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     
    703721        // If that is the case, abandon the preemption.
    704722        bool preempted = false;
    705         if(thrd->next == 0p) {
     723        if(thrd->link.next == 0p) {
    706724                preempted = true;
    707725                thrd->preempted = reason;
     
    763781                pending_preemption = false;
    764782                kernel_thread = pthread_self();
     783                id = -1u;
    765784
    766785                runner{ &this };
     
    772791        mainProcessor = (processor *)&storage_mainProcessor;
    773792        (*mainProcessor){};
     793
     794        mainProcessor->id = doregister2(mainCluster, mainProcessor);
    774795
    775796        //initialize the global state variables
     
    826847        kernel_stop_preemption();
    827848
     849        unregister2(mainCluster, mainProcessor);
     850
    828851        // Destroy the main processor and its context in reverse order of construction
    829852        // These were manually constructed so we need manually destroy them
    830853        void ^?{}(processor & this) with( this ){
    831854                /* paranoid */ verify( this.do_terminate == true );
     855                __cfaabi_dbg_print_safe("Kernel : destroyed main processor context %p\n", &runner);
    832856        }
    833857
     
    835859
    836860        // Final step, destroy the main thread since it is no longer needed
     861
    837862        // Since we provided a stack to this taxk it will not destroy anything
    838863        /* paranoid */ verify(mainThread->self_cor.stack.storage == (__stack_t*)(((uintptr_t)&storage_mainThreadCtx)| 0x1));
     
    887912
    888913// Wake a thread from the front if there are any
    889 static bool __wake_one(cluster * this, __attribute__((unused)) bool force) {
    890         // if we don't want to force check if we know it's false
    891         // if( !this->idles.head && !force ) return false;
    892 
     914static bool __wake_one(cluster * this) {
    893915        // First, lock the cluster idle
    894916        lock( this->idle_lock __cfaabi_dbg_ctx2 );
Note: See TracChangeset for help on using the changeset viewer.