Ignore:
File:
1 edited

Legend:

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

    r254ad1b r431cd4f  
    110110static $thread * __next_thread(cluster * this);
    111111static $thread * __next_thread_slow(cluster * this);
    112 static inline bool __must_unpark( $thread * thrd ) __attribute((nonnull(1)));
    113112static void __run_thread(processor * this, $thread * dst);
    114113static void __wake_one(cluster * cltr);
     
    119118
    120119extern void __cfa_io_start( processor * );
    121 extern bool __cfa_io_drain( processor * );
     120extern void __cfa_io_drain( processor * );
    122121extern void __cfa_io_flush( processor * );
    123122extern void __cfa_io_stop ( processor * );
    124 static inline bool __maybe_io_drain( processor * );
     123static inline void __maybe_io_drain( processor * );
    125124
    126125extern void __disable_interrupts_hard();
    127126extern void __enable_interrupts_hard();
    128 
    129 static inline void __disable_interrupts_checked() {
    130         /* paranoid */ verify( __preemption_enabled() );
    131         disable_interrupts();
    132         /* paranoid */ verify( ! __preemption_enabled() );
    133 }
    134 
    135 static inline void __enable_interrupts_checked( bool poll = true ) {
    136         /* paranoid */ verify( ! __preemption_enabled() );
    137         enable_interrupts( poll );
    138         /* paranoid */ verify( __preemption_enabled() );
    139 }
    140127
    141128//=============================================================================================
     
    349336                if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) {
    350337                        // The thread was preempted, reschedule it and reset the flag
    351                         schedule_thread$( thrd_dst );
     338                        __schedule_thread( thrd_dst );
    352339                        break RUNNING;
    353340                }
     
    439426        /* paranoid */ verify( ! __preemption_enabled() );
    440427        /* paranoid */ verify( kernelTLS().this_proc_id );
    441         /* paranoid */ verify( ready_schedule_islocked());
    442428        /* paranoid */ verify( thrd );
    443429        /* paranoid */ verify( thrd->state != Halted );
     
    458444        struct cluster * cl = thrd->curr_cluster;
    459445
    460         // push the thread to the cluster ready-queue
    461         push( cl, thrd );
    462 
    463         // variable thrd is no longer safe to use
    464         thrd = 0xdeaddeaddeaddeadp;
    465 
    466         // wake the cluster using the save variable.
    467         __wake_one( cl );
     446        ready_schedule_lock();
     447                // push the thread to the cluster ready-queue
     448                push( cl, thrd );
     449
     450                // variable thrd is no longer safe to use
     451
     452                // wake the cluster using the save variable.
     453                __wake_one( cl );
     454        ready_schedule_unlock();
    468455
    469456        #if !defined(__CFA_NO_STATISTICS__)
     
    478465        #endif
    479466
    480         /* paranoid */ verify( ready_schedule_islocked());
    481         /* paranoid */ verify( ! __preemption_enabled() );
    482 }
    483 
    484 void schedule_thread$( $thread * thrd ) {
    485         ready_schedule_lock();
    486                 __schedule_thread( thrd );
    487         ready_schedule_unlock();
     467        /* paranoid */ verify( ! __preemption_enabled() );
    488468}
    489469
     
    516496}
    517497
    518 static inline bool __must_unpark( $thread * thrd ) {
     498void unpark( $thread * thrd ) {
     499        if( !thrd ) return;
     500
    519501        int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST);
    520502        switch(old_ticket) {
    521503                case TICKET_RUNNING:
    522504                        // Wake won the race, the thread will reschedule/rerun itself
    523                         return false;
     505                        break;
    524506                case TICKET_BLOCKED:
    525507                        /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION );
    526508                        /* paranoid */ verify( thrd->state == Blocked );
    527                         return true;
     509
     510                        {
     511                                /* paranoid */ verify( publicTLS_get(this_proc_id) );
     512                                disable_interrupts();
     513
     514                                /* paranoid */ verify( ! __preemption_enabled() );
     515
     516                                // Wake lost the race,
     517                                __schedule_thread( thrd );
     518
     519                                /* paranoid */ verify( ! __preemption_enabled() );
     520
     521                                enable_interrupts_noPoll();
     522                                /* paranoid */ verify( publicTLS_get(this_proc_id) );
     523                        }
     524
     525                        break;
    528526                default:
    529527                        // This makes no sense, something is wrong abort
     
    532530}
    533531
    534 void unpark( $thread * thrd ) {
    535         if( !thrd ) return;
    536 
    537         if(__must_unpark(thrd)) {
    538                 disable_interrupts();
    539                         // Wake lost the race,
    540                         schedule_thread$( thrd );
    541                 enable_interrupts(false);
    542         }
    543 }
    544 
    545532void park( void ) {
    546         __disable_interrupts_checked();
    547                 /* paranoid */ verify( kernelTLS().this_thread->preempted == __NO_PREEMPTION );
    548                 returnToKernel();
    549         __enable_interrupts_checked();
     533        /* paranoid */ verify( __preemption_enabled() );
     534        disable_interrupts();
     535        /* paranoid */ verify( ! __preemption_enabled() );
     536        /* paranoid */ verify( kernelTLS().this_thread->preempted == __NO_PREEMPTION );
     537
     538        returnToKernel();
     539
     540        /* paranoid */ verify( ! __preemption_enabled() );
     541        enable_interrupts( __cfaabi_dbg_ctx );
     542        /* paranoid */ verify( __preemption_enabled() );
    550543
    551544}
     
    587580// KERNEL ONLY
    588581bool force_yield( __Preemption_Reason reason ) {
    589         __disable_interrupts_checked();
    590                 $thread * thrd = kernelTLS().this_thread;
    591                 /* paranoid */ verify(thrd->state == Active);
    592 
    593                 // SKULLDUGGERY: It is possible that we are preempting this thread just before
    594                 // it was going to park itself. If that is the case and it is already using the
    595                 // intrusive fields then we can't use them to preempt the thread
    596                 // If that is the case, abandon the preemption.
    597                 bool preempted = false;
    598                 if(thrd->link.next == 0p) {
    599                         preempted = true;
    600                         thrd->preempted = reason;
    601                         returnToKernel();
    602                 }
    603         __enable_interrupts_checked( false );
     582        /* paranoid */ verify( __preemption_enabled() );
     583        disable_interrupts();
     584        /* paranoid */ verify( ! __preemption_enabled() );
     585
     586        $thread * thrd = kernelTLS().this_thread;
     587        /* paranoid */ verify(thrd->state == Active);
     588
     589        // SKULLDUGGERY: It is possible that we are preempting this thread just before
     590        // it was going to park itself. If that is the case and it is already using the
     591        // intrusive fields then we can't use them to preempt the thread
     592        // If that is the case, abandon the preemption.
     593        bool preempted = false;
     594        if(thrd->link.next == 0p) {
     595                preempted = true;
     596                thrd->preempted = reason;
     597                returnToKernel();
     598        }
     599
     600        /* paranoid */ verify( ! __preemption_enabled() );
     601        enable_interrupts_noPoll();
     602        /* paranoid */ verify( __preemption_enabled() );
     603
    604604        return preempted;
    605605}
     
    646646        __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this);
    647647
    648         __disable_interrupts_checked();
     648        disable_interrupts();
    649649                /* paranoid */ verify( ! __preemption_enabled() );
    650650                eventfd_t val;
    651651                val = 1;
    652652                eventfd_write( this->idle, val );
    653         __enable_interrupts_checked();
     653        enable_interrupts( __cfaabi_dbg_ctx );
    654654}
    655655
     
    743743#endif
    744744
    745 static inline bool __maybe_io_drain( processor * proc ) {
     745static inline void __maybe_io_drain( processor * proc ) {
    746746        #if defined(CFA_HAVE_LINUX_IO_URING_H)
    747747                __cfadbg_print_safe(runtime_core, "Kernel : core %p checking io for ring %d\n", proc, proc->io.ctx->fd);
     
    751751                unsigned head = *ctx->cq.head;
    752752                unsigned tail = *ctx->cq.tail;
    753                 if(head == tail) return false;
    754                 return __cfa_io_drain( proc );
     753                if(head != tail) __cfa_io_drain( proc );
    755754        #endif
    756755}
Note: See TracChangeset for help on using the changeset viewer.