Ignore:
File:
1 edited

Legend:

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

    r504a7dc ra7b486b  
    120120static void __run_thread(processor * this, $thread * dst);
    121121static $thread * __halt(processor * this);
    122 static bool __wake_one(cluster * cltr);
     122static bool __wake_one(cluster * cltr, bool was_empty);
    123123static bool __wake_proc(processor *);
    124124
     
    197197        self_mon.recursion = 1;
    198198        self_mon_p = &self_mon;
    199         link.next = 0p;
    200         link.prev = 0p;
     199        next = 0p;
    201200
    202201        node.next = 0p;
     
    224223        this.name = name;
    225224        this.cltr = &cltr;
    226         id = -1u;
    227225        terminated{ 0 };
    228226        destroyer = 0p;
     
    262260        this.preemption_rate = preemption_rate;
    263261        ready_queue{};
    264         ready_lock{};
     262        ready_queue_lock{};
    265263
    266264        #if !defined(__CFA_NO_STATISTICS__)
     
    297295        __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this);
    298296
    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 
    305297        doregister(this->cltr, this);
    306298
     
    326318                                /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    327319                                /* paranoid */ verifyf( readyThread->state == Ready || readyThread->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", readyThread->state, readyThread->preempted);
    328                                 /* paranoid */ verifyf( readyThread->link.next == 0p, "Expected null got %p", readyThread->link.next );
     320                                /* paranoid */ verifyf( readyThread->next == 0p, "Expected null got %p", readyThread->next );
    329321
    330322                                // We found a thread run it
     
    342334        V( this->terminated );
    343335
    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 
    356336        __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this);
    357337
    358         stats_tls_tally(this->cltr);
     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;
    359342}
    360343
     
    608591// Scheduler routines
    609592// KERNEL ONLY
    610 void __schedule_thread( $thread * thrd ) {
    611         /* paranoid */ verify( thrd );
    612         /* paranoid */ verify( thrd->state != Halted );
     593void __schedule_thread( $thread * thrd ) with( *thrd->curr_cluster ) {
    613594        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    614595        /* paranoid */ #if defined( __CFA_WITH_VERIFY__ )
    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 );
     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 );
    619600        /* paranoid */ #endif
    620         /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next );
     601        /* paranoid */ verifyf( thrd->next == 0p, "Expected null got %p", thrd->next );
    621602
    622603        if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready;
    623604
    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);
     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);
    629611
    630612        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     
    635617        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    636618
    637         ready_schedule_lock(this, kernelTLS.this_processor);
    638                 $thread * head = pop( this );
    639         ready_schedule_unlock(this, kernelTLS.this_processor);
     619        lock( ready_queue_lock __cfaabi_dbg_ctx2 );
     620        $thread * head = pop_head( ready_queue );
     621        unlock( ready_queue_lock );
    640622
    641623        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     
    721703        // If that is the case, abandon the preemption.
    722704        bool preempted = false;
    723         if(thrd->link.next == 0p) {
     705        if(thrd->next == 0p) {
    724706                preempted = true;
    725707                thrd->preempted = reason;
     
    781763                pending_preemption = false;
    782764                kernel_thread = pthread_self();
    783                 id = -1u;
    784765
    785766                runner{ &this };
     
    791772        mainProcessor = (processor *)&storage_mainProcessor;
    792773        (*mainProcessor){};
    793 
    794         mainProcessor->id = doregister2(mainCluster, mainProcessor);
    795774
    796775        //initialize the global state variables
     
    847826        kernel_stop_preemption();
    848827
    849         unregister2(mainCluster, mainProcessor);
    850 
    851828        // Destroy the main processor and its context in reverse order of construction
    852829        // These were manually constructed so we need manually destroy them
    853830        void ^?{}(processor & this) with( this ){
    854831                /* paranoid */ verify( this.do_terminate == true );
    855                 __cfaabi_dbg_print_safe("Kernel : destroyed main processor context %p\n", &runner);
    856832        }
    857833
     
    859835
    860836        // Final step, destroy the main thread since it is no longer needed
    861 
    862837        // Since we provided a stack to this taxk it will not destroy anything
    863838        /* paranoid */ verify(mainThread->self_cor.stack.storage == (__stack_t*)(((uintptr_t)&storage_mainThreadCtx)| 0x1));
     
    912887
    913888// Wake a thread from the front if there are any
    914 static bool __wake_one(cluster * this) {
     889static 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
    915893        // First, lock the cluster idle
    916894        lock( this->idle_lock __cfaabi_dbg_ctx2 );
Note: See TracChangeset for help on using the changeset viewer.