Ignore:
File:
1 edited

Legend:

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

    r6b33e89 rb93bf85  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Apr 25 07:02:42 2025
    13 // Update Count     : 82
     12// Last Modified On : Mon Jan  9 08:42:05 2023
     13// Update Count     : 77
    1414//
    1515
     
    4545#pragma GCC diagnostic pop
    4646
    47 #if ! defined(__CFA_NO_STATISTICS__)
     47#if !defined(__CFA_NO_STATISTICS__)
    4848        #define __STATS_DEF( ...) __VA_ARGS__
    4949#else
     
    158158
    159159        __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this);
    160         #if ! defined(__CFA_NO_STATISTICS__)
    161                 if ( this->print_halts ) {
     160        #if !defined(__CFA_NO_STATISTICS__)
     161                if( this->print_halts ) {
    162162                        __cfaabi_bits_print_safe( STDOUT_FILENO, "Processor : %d - %s (%p)\n", this->unique_id, this->name, (void*)this);
    163163                }
     
    169169
    170170                // if we need to run some special setup, now is the time to do it.
    171                 if (this->init.thrd) {
     171                if(this->init.thrd) {
    172172                        this->init.thrd->curr_cluster = this->cltr;
    173173                        __run_thread(this, this->init.thrd);
     
    185185                        readyThread = __next_thread( this->cltr );
    186186
    187                         if ( ! readyThread ) {
     187                        if( !readyThread ) {
    188188                                // there is no point in holding submissions if we are idle
    189189                                __IO_STATS__(true, io.flush.idle++; )
     
    196196                        }
    197197
    198                         if ( ! readyThread ) for(5) {
     198                        if( !readyThread ) for(5) {
    199199                                readyThread = __next_thread_slow( this->cltr );
    200200
    201                                 if ( readyThread ) break;
     201                                if( readyThread ) break;
    202202
    203203                                // It's unlikely we still I/O to submit, but the arbiter could
     
    210210
    211211                        HALT:
    212                         if ( ! readyThread ) {
     212                        if( !readyThread ) {
    213213                                // Don't block if we are done
    214                                 if ( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
     214                                if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
    215215
    216216                                // Push self to idle stack
    217                                 if ( ! mark_idle(this->cltr->procs, * this)) continue MAIN_LOOP;
     217                                if(!mark_idle(this->cltr->procs, * this)) continue MAIN_LOOP;
    218218
    219219                                // Confirm the ready-queue is empty
    220220                                readyThread = __next_thread_search( this->cltr );
    221                                 if ( readyThread ) {
     221                                if( readyThread ) {
    222222                                        // A thread was found, cancel the halt
    223223                                        mark_awake(this->cltr->procs, * this);
     
    247247
    248248                        // Are we done?
    249                         if ( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
    250 
    251                         if (__atomic_load_n(&this->io.pending, __ATOMIC_RELAXED) && ! __atomic_load_n(&this->io.dirty, __ATOMIC_RELAXED)) {
     249                        if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
     250
     251                        if(__atomic_load_n(&this->io.pending, __ATOMIC_RELAXED) && !__atomic_load_n(&this->io.dirty, __ATOMIC_RELAXED)) {
    252252                                __IO_STATS__(true, io.flush.dirty++; )
    253253                                __cfa_io_flush( this );
     
    263263        post( this->terminated );
    264264
    265         if (this == mainProcessor) {
     265        if(this == mainProcessor) {
    266266                // HACK : the coroutine context switch expects this_thread to be set
    267267                // and it make sense for it to be set in all other cases except here
     
    294294
    295295        // Actually run the thread
    296         RUNNING:
    297         while( true ) {
     296        RUNNING:  while(true) {
    298297                thrd_dst->preempted = __NO_PREEMPTION;
    299298
     
    340339                // In case 2, we lost the race so we now own the thread.
    341340
    342                 if (unlikely(thrd_dst->preempted != __NO_PREEMPTION)) {
     341                if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) {
    343342                        // Reset the this_thread now that we know
    344343                        // the state isn't active anymore
     
    350349                }
    351350
    352                 if (unlikely(thrd_dst->state == Halting)) {
     351                if(unlikely(thrd_dst->state == Halting)) {
    353352                        // Reset the this_thread now that we know
    354353                        // the state isn't active anymore
     
    419418        }
    420419
    421         #if ! defined(__CFA_NO_STATISTICS__)
     420        #if !defined(__CFA_NO_STATISTICS__)
    422421                /* paranoid */ verify( thrd_src->last_proc != 0p );
    423                 if (thrd_src->last_proc != kernelTLS().this_processor) {
     422                if(thrd_src->last_proc != kernelTLS().this_processor) {
    424423                        __tls_stats()->ready.threads.migration++;
    425424                }
     
    441440        /* paranoid */ verify( thrd->curr_cluster );
    442441        /* paranoid */ #if defined( __CFA_WITH_VERIFY__ )
    443         /* paranoid */  if ( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,
     442        /* paranoid */  if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,
    444443                                        "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted );
    445         /* paranoid */  if ( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active,
     444        /* paranoid */  if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active,
    446445                                        "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted );
    447446        /* paranoid */ #endif
     
    464463        __wake_one( cl );
    465464
    466         #if ! defined(__CFA_NO_STATISTICS__)
    467                 if ( kernelTLS().this_stats ) {
     465        #if !defined(__CFA_NO_STATISTICS__)
     466                if( kernelTLS().this_stats ) {
    468467                        __tls_stats()->ready.threads.threads++;
    469                         if (outside) {
     468                        if(outside) {
    470469                                __tls_stats()->ready.threads.extunpark++;
    471470                        }
     
    543542        /* paranoid */ verify( ready_schedule_islocked());
    544543
    545         if ( ! thrd ) return;
    546 
    547         if (__must_unpark(thrd)) {
     544        if( !thrd ) return;
     545
     546        if(__must_unpark(thrd)) {
    548547                // Wake lost the race,
    549548                __schedule_thread( thrd, hint );
     
    555554
    556555void unpark( thread$ * thrd, unpark_hint hint ) libcfa_public {
    557         if ( ! thrd ) return;
    558 
    559         if (__must_unpark(thrd)) {
     556        if( !thrd ) return;
     557
     558        if(__must_unpark(thrd)) {
    560559                disable_interrupts();
    561560                        // Wake lost the race,
     
    593592                /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) < ((uintptr_t)__get_stack(thrd->curr_cor)->base ), "ERROR : thread$ %p has been corrupted.\n StackPointer too small.\n", thrd );
    594593
    595                 if ( TICKET_RUNNING != thrd->ticket ) { abort( "Thread terminated with pending unpark" ); }
    596                 if ( thrd != this->owner ) { abort( "Thread internal monitor has incorrect owner" ); }
    597                 if ( this->recursion != 1) { abort( "Thread internal monitor has unbalanced recursion" ); }
     594                if( TICKET_RUNNING != thrd->ticket ) { abort( "Thread terminated with pending unpark" ); }
     595                if( thrd != this->owner ) { abort( "Thread internal monitor has incorrect owner" ); }
     596                if( this->recursion != 1) { abort( "Thread internal monitor has unbalanced recursion" ); }
    598597
    599598                thrd->state = Halting;
     
    619618                // If that is the case, abandon the preemption.
    620619                bool preempted = false;
    621                 if (thrd->rdy_link.next == 0p) {
     620                if(thrd->rdy_link.next == 0p) {
    622621                        preempted = true;
    623622                        thrd->preempted = reason;
     
    642641
    643642        // If no one is sleeping: we are done
    644         if ( fdp == 0p ) return;
     643        if( fdp == 0p ) return;
    645644
    646645        int fd = 1;
    647         if ( __atomic_load_n(&fdp->sem, __ATOMIC_SEQ_CST) != 1 ) {
     646        if( __atomic_load_n(&fdp->sem, __ATOMIC_SEQ_CST) != 1 ) {
    648647                fd = __atomic_exchange_n(&fdp->sem, 1, __ATOMIC_RELAXED);
    649648        }
     
    653652        case 0:
    654653                // If the processor isn't ready to sleep then the exchange will already wake it up
    655                 #if ! defined(__CFA_NO_STATISTICS__)
    656                         if ( kernelTLS().this_stats ) { __tls_stats()->ready.sleep.early++;
     654                #if !defined(__CFA_NO_STATISTICS__)
     655                        if( kernelTLS().this_stats ) { __tls_stats()->ready.sleep.early++;
    657656                        } else { __atomic_fetch_add(&this->stats->ready.sleep.early, 1, __ATOMIC_RELAXED); }
    658657                #endif
     
    660659        case 1:
    661660                // If someone else already said they will wake them: we are done
    662                 #if ! defined(__CFA_NO_STATISTICS__)
    663                         if ( kernelTLS().this_stats ) { __tls_stats()->ready.sleep.seen++;
     661                #if !defined(__CFA_NO_STATISTICS__)
     662                        if( kernelTLS().this_stats ) { __tls_stats()->ready.sleep.seen++;
    664663                        } else { __atomic_fetch_add(&this->stats->ready.sleep.seen, 1, __ATOMIC_RELAXED); }
    665664                #endif
     
    671670                /* paranoid */ verifyf( ret == 0, "Expected return to be 0, was %d\n", ret );
    672671
    673                 #if ! defined(__CFA_NO_STATISTICS__)
    674                         if ( kernelTLS().this_stats ) { __tls_stats()->ready.sleep.wakes++;
     672                #if !defined(__CFA_NO_STATISTICS__)
     673                        if( kernelTLS().this_stats ) { __tls_stats()->ready.sleep.wakes++;
    675674                        } else { __atomic_fetch_add(&this->stats->ready.sleep.wakes, 1, __ATOMIC_RELAXED); }
    676675                #endif
     
    711710
    712711                // Someone already told us to wake-up! No time for a nap.
    713                 if (expected == 1) { return; }
     712                if(expected == 1) { return; }
    714713
    715714                // Try to mark that we are going to sleep
    716                 if (__atomic_compare_exchange_n(&this->idle_wctx.sem, &expected, this->idle_wctx.evfd, false,  __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ) {
     715                if(__atomic_compare_exchange_n(&this->idle_wctx.sem, &expected, this->idle_wctx.evfd, false,  __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) ) {
    717716                        // Every one agreed, taking a nap
    718717                        break;
     
    721720
    722721
    723         #if ! defined(__CFA_NO_STATISTICS__)
    724                 if (this->print_halts) {
     722        #if !defined(__CFA_NO_STATISTICS__)
     723                if(this->print_halts) {
    725724                        __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->unique_id, rdtscl());
    726725                }
     
    732731                eventfd_t val;
    733732                ssize_t ret = read( this->idle_wctx.evfd, &val, sizeof(val) );
    734                 if (ret < 0) {
     733                if(ret < 0) {
    735734                        switch((int)errno) {
    736735                        case EAGAIN:
     
    747746        }
    748747
    749         #if ! defined(__CFA_NO_STATISTICS__)
    750                 if (this->print_halts) {
     748        #if !defined(__CFA_NO_STATISTICS__)
     749                if(this->print_halts) {
    751750                        __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->unique_id, rdtscl());
    752751                }
     
    760759
    761760        /* paranoid */ verify( ! __preemption_enabled() );
    762         if ( ! try_lock( this )) return false;
     761        if(!try_lock( this )) return false;
    763762                this.idle++;
    764763                /* paranoid */ verify( this.idle <= this.total );
     
    785784                        // update the pointer to the head wait context
    786785                        struct __fd_waitctx * wctx = 0;
    787                         if ( ! isEmpty( this.idles )) wctx = &first( this. idles ).idle_wctx;
     786                        if(!this.idles`isEmpty) wctx = &this.idles`first.idle_wctx;
    788787                        __atomic_store_n(&this.fdw, wctx, __ATOMIC_SEQ_CST);
    789788                }
     
    799798        thread$ * thrd = __cfaabi_tls.this_thread;
    800799
    801         if (thrd) {
     800        if(thrd) {
    802801                int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
    803802                __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
     
    848847//-----------------------------------------------------------------------------
    849848// Statistics
    850 #if ! defined(__CFA_NO_STATISTICS__)
     849#if !defined(__CFA_NO_STATISTICS__)
    851850        void print_halts( processor & this ) libcfa_public {
    852851                this.print_halts = true;
     
    856855                /* paranoid */ verify( cltr->stats );
    857856
    858                 processor * it = &first( list );
     857                processor * it = &list`first;
    859858                for(unsigned i = 0; i < count; i++) {
    860859                        /* paranoid */ verifyf( it, "Unexpected null iterator, at index %u of %u\n", i, count);
     
    862861                        // __print_stats( it->local_data->this_stats, cltr->print_stats, "Processor", it->name, (void*)it );
    863862                        __tally_stats( cltr->stats, it->local_data->this_stats );
    864                         it = &next( *it );
     863                        it = &(*it)`next;
    865864                }
    866865        }
Note: See TracChangeset for help on using the changeset viewer.