Ignore:
File:
1 edited

Legend:

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

    re3fea42 rac2b598  
    110110//-----------------------------------------------------------------------------
    111111//Start and stop routine for the kernel, declared first to make sure they run first
    112 static void kernel_startup(void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
    113 static void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
     112static void __kernel_startup (void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
     113static void __kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
    114114
    115115//-----------------------------------------------------------------------------
     
    117117KERNEL_STORAGE(cluster,         mainCluster);
    118118KERNEL_STORAGE(processor,       mainProcessor);
    119 KERNEL_STORAGE(thread_desc,     mainThread);
     119KERNEL_STORAGE($thread, mainThread);
    120120KERNEL_STORAGE(__stack_t,       mainThreadCtx);
    121121
    122122cluster     * mainCluster;
    123123processor   * mainProcessor;
    124 thread_desc * mainThread;
     124$thread * mainThread;
    125125
    126126extern "C" {
     
    164164// Main thread construction
    165165
    166 void ?{}( coroutine_desc & this, current_stack_info_t * info) with( this ) {
     166void ?{}( $coroutine & this, current_stack_info_t * info) with( this ) {
    167167        stack.storage = info->storage;
    168168        with(*stack.storage) {
     
    179179}
    180180
    181 void ?{}( thread_desc & this, current_stack_info_t * info) with( this ) {
     181void ?{}( $thread & this, current_stack_info_t * info) with( this ) {
    182182        state = Start;
    183183        self_cor{ info };
     
    208208}
    209209
    210 static void start(processor * this);
     210static void * __invoke_processor(void * arg);
     211
    211212void ?{}(processor & this, const char name[], cluster & cltr) with( this ) {
    212213        this.name = name;
    213214        this.cltr = &cltr;
    214215        terminated{ 0 };
     216        destroyer = 0p;
    215217        do_terminate = false;
    216218        preemption_alarm = 0p;
     
    220222        idleLock{};
    221223
    222         start( &this );
     224        __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", &this);
     225
     226        this.stack = __create_pthread( &this.kernel_thread, __invoke_processor, (void *)&this );
     227
     228        __cfaabi_dbg_print_safe("Kernel : core %p started\n", &this);
    223229}
    224230
     
    258264// Kernel Scheduling logic
    259265//=============================================================================================
    260 static void runThread(processor * this, thread_desc * dst);
    261 static void finishRunning(processor * this);
    262 static void halt(processor * this);
     266static $thread * __next_thread(cluster * this);
     267static void __run_thread(processor * this, $thread * dst);
     268static void __halt(processor * this);
    263269
    264270//Main of the processor contexts
     
    281287                __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
    282288
    283                 thread_desc * readyThread = 0p;
     289                $thread * readyThread = 0p;
    284290                for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ ) {
    285                         readyThread = nextThread( this->cltr );
     291                        readyThread = __next_thread( this->cltr );
    286292
    287293                        if(readyThread) {
    288                                 verify( ! kernelTLS.preemption_state.enabled );
    289 
    290                                 runThread(this, readyThread);
    291 
    292                                 verify( ! kernelTLS.preemption_state.enabled );
    293 
    294                                 //Some actions need to be taken from the kernel
    295                                 finishRunning(this);
     294                                /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     295                                /* paranoid */ verifyf( readyThread->state == Inactive || readyThread->state == Start || readyThread->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", readyThread->state, readyThread->preempted);
     296                                /* paranoid */ verifyf( readyThread->next == 0p, "Expected null got %p", readyThread->next );
     297
     298                                __run_thread(this, readyThread);
     299
     300                                /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    296301
    297302                                spin_count = 0;
    298303                        } else {
    299304                                // spin(this, &spin_count);
    300                                 halt(this);
     305                                __halt(this);
    301306                        }
    302307                }
     
    318323// runThread runs a thread by context switching
    319324// from the processor coroutine to the target thread
    320 static void runThread(processor * this, thread_desc * thrd_dst) {
    321         coroutine_desc * proc_cor = get_coroutine(this->runner);
    322 
    323         // Reset the terminating actions here
    324         this->finish.action_code = No_Action;
     325static void __run_thread(processor * this, $thread * thrd_dst) {
     326        $coroutine * proc_cor = get_coroutine(this->runner);
    325327
    326328        // Update global state
    327329        kernelTLS.this_thread = thrd_dst;
    328330
    329         // set state of processor coroutine to inactive and the thread to active
    330         proc_cor->state = proc_cor->state == Halted ? Halted : Inactive;
    331         thrd_dst->state = Active;
    332 
    333         // set context switch to the thread that the processor is executing
    334         verify( thrd_dst->context.SP );
    335         CtxSwitch( &proc_cor->context, &thrd_dst->context );
    336         // when CtxSwitch returns we are back in the processor coroutine
    337 
    338         // set state of processor coroutine to active and the thread to inactive
    339         thrd_dst->state = thrd_dst->state == Halted ? Halted : Inactive;
     331        // set state of processor coroutine to inactive
     332        verify(proc_cor->state == Active);
     333        proc_cor->state = Inactive;
     334
     335        // Actually run the thread
     336        RUNNING:  while(true) {
     337                if(unlikely(thrd_dst->preempted)) {
     338                        thrd_dst->preempted = __NO_PREEMPTION;
     339                        verify(thrd_dst->state == Active || thrd_dst->state == Rerun);
     340                } else {
     341                        verify(thrd_dst->state == Start || thrd_dst->state == Primed || thrd_dst->state == Inactive);
     342                        thrd_dst->state = Active;
     343                }
     344
     345                /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     346
     347                // set context switch to the thread that the processor is executing
     348                verify( thrd_dst->context.SP );
     349                __cfactx_switch( &proc_cor->context, &thrd_dst->context );
     350                // when __cfactx_switch returns we are back in the processor coroutine
     351
     352                /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     353
     354
     355                // We just finished running a thread, there are a few things that could have happened.
     356                // 1 - Regular case : the thread has blocked and now one has scheduled it yet.
     357                // 2 - Racy case    : the thread has blocked but someone has already tried to schedule it.
     358                // 3 - Polite Racy case : the thread has blocked, someone has already tried to schedule it, but the thread is nice and wants to go through the ready-queue any way
     359                // 4 - Preempted
     360                // In case 1, we may have won a race so we can't write to the state again.
     361                // In case 2, we lost the race so we now own the thread.
     362                // In case 3, we lost the race but can just reschedule the thread.
     363
     364                if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) {
     365                        // The thread was preempted, reschedule it and reset the flag
     366                        __schedule_thread( thrd_dst );
     367                        break RUNNING;
     368                }
     369
     370                // set state of processor coroutine to active and the thread to inactive
     371                static_assert(sizeof(thrd_dst->state) == sizeof(int));
     372                enum coroutine_state old_state = __atomic_exchange_n(&thrd_dst->state, Inactive, __ATOMIC_SEQ_CST);
     373                switch(old_state) {
     374                        case Halted:
     375                                // The thread has halted, it should never be scheduled/run again, leave it back to Halted and move on
     376                                thrd_dst->state = Halted;
     377
     378                                // We may need to wake someone up here since
     379                                unpark( this->destroyer );
     380                                this->destroyer = 0p;
     381                                break RUNNING;
     382                        case Active:
     383                                // This is case 1, the regular case, nothing more is needed
     384                                break RUNNING;
     385                        case Rerun:
     386                                // This is case 2, the racy case, someone tried to run this thread before it finished blocking
     387                                // In this case, just run it again.
     388                                continue RUNNING;
     389                        default:
     390                                // This makes no sense, something is wrong abort
     391                                abort("Finished running a thread that was Inactive/Start/Primed %d\n", old_state);
     392                }
     393        }
     394
     395        // Just before returning to the processor, set the processor coroutine to active
    340396        proc_cor->state = Active;
    341397}
    342398
    343399// KERNEL_ONLY
    344 static void returnToKernel() {
    345         coroutine_desc * proc_cor = get_coroutine(kernelTLS.this_processor->runner);
    346         thread_desc * thrd_src = kernelTLS.this_thread;
    347 
    348         // set state of current coroutine to inactive
    349         thrd_src->state = thrd_src->state == Halted ? Halted : Inactive;
    350         proc_cor->state = Active;
    351         int local_errno = *__volatile_errno();
    352         #if defined( __i386 ) || defined( __x86_64 )
    353                 __x87_store;
    354         #endif
    355 
    356         // set new coroutine that the processor is executing
    357         // and context switch to it
    358         verify( proc_cor->context.SP );
    359         CtxSwitch( &thrd_src->context, &proc_cor->context );
    360 
    361         // set state of new coroutine to active
    362         proc_cor->state = proc_cor->state == Halted ? Halted : Inactive;
    363         thrd_src->state = Active;
    364 
    365         #if defined( __i386 ) || defined( __x86_64 )
    366                 __x87_load;
    367         #endif
    368         *__volatile_errno() = local_errno;
    369 }
    370 
    371 // KERNEL_ONLY
    372 // Once a thread has finished running, some of
    373 // its final actions must be executed from the kernel
    374 static void finishRunning(processor * this) with( this->finish ) {
    375         verify( ! kernelTLS.preemption_state.enabled );
    376         choose( action_code ) {
    377         case No_Action:
    378                 break;
    379         case Release:
    380                 unlock( *lock );
    381         case Schedule:
    382                 ScheduleThread( thrd );
    383         case Release_Schedule:
    384                 unlock( *lock );
    385                 ScheduleThread( thrd );
    386         case Release_Multi:
    387                 for(int i = 0; i < lock_count; i++) {
    388                         unlock( *locks[i] );
    389                 }
    390         case Release_Multi_Schedule:
    391                 for(int i = 0; i < lock_count; i++) {
    392                         unlock( *locks[i] );
    393                 }
    394                 for(int i = 0; i < thrd_count; i++) {
    395                         ScheduleThread( thrds[i] );
    396                 }
    397         case Callback:
    398                 callback();
    399         default:
    400                 abort("KERNEL ERROR: Unexpected action to run after thread");
    401         }
     400void returnToKernel() {
     401        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     402        $coroutine * proc_cor = get_coroutine(kernelTLS.this_processor->runner);
     403        $thread * thrd_src = kernelTLS.this_thread;
     404
     405        // Run the thread on this processor
     406        {
     407                int local_errno = *__volatile_errno();
     408                #if defined( __i386 ) || defined( __x86_64 )
     409                        __x87_store;
     410                #endif
     411                verify( proc_cor->context.SP );
     412                __cfactx_switch( &thrd_src->context, &proc_cor->context );
     413                #if defined( __i386 ) || defined( __x86_64 )
     414                        __x87_load;
     415                #endif
     416                *__volatile_errno() = local_errno;
     417        }
     418
     419        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    402420}
    403421
     
    406424// This is the entry point for processors (kernel threads)
    407425// It effectively constructs a coroutine by stealing the pthread stack
    408 static void * CtxInvokeProcessor(void * arg) {
     426static void * __invoke_processor(void * arg) {
    409427        processor * proc = (processor *) arg;
    410428        kernelTLS.this_processor = proc;
     
    447465} // Abort
    448466
    449 void * create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) {
     467void * __create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) {
    450468        pthread_attr_t attr;
    451469
     
    469487        );
    470488
    471         Abort( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" ); 
     489        Abort( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
    472490
    473491        Abort( pthread_create( pthread, &attr, start, arg ), "pthread_create" );
     
    475493}
    476494
    477 static void start(processor * this) {
    478         __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this);
    479 
    480         this->stack = create_pthread( &this->kernel_thread, CtxInvokeProcessor, (void *)this );
    481 
    482         __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
    483 }
    484 
    485495// KERNEL_ONLY
    486 void kernel_first_resume( processor * this ) {
    487         thread_desc * src = mainThread;
    488         coroutine_desc * dst = get_coroutine(this->runner);
     496static void __kernel_first_resume( processor * this ) {
     497        $thread * src = mainThread;
     498        $coroutine * dst = get_coroutine(this->runner);
    489499
    490500        verify( ! kernelTLS.preemption_state.enabled );
    491501
     502        kernelTLS.this_thread->curr_cor = dst;
    492503        __stack_prepare( &dst->stack, 65000 );
    493         CtxStart(&this->runner, CtxInvokeCoroutine);
     504        __cfactx_start(main, dst, this->runner, __cfactx_invoke_coroutine);
    494505
    495506        verify( ! kernelTLS.preemption_state.enabled );
     
    503514        // context switch to specified coroutine
    504515        verify( dst->context.SP );
    505         CtxSwitch( &src->context, &dst->context );
    506         // when CtxSwitch returns we are back in the src coroutine
     516        __cfactx_switch( &src->context, &dst->context );
     517        // when __cfactx_switch returns we are back in the src coroutine
     518
     519        mainThread->curr_cor = &mainThread->self_cor;
    507520
    508521        // set state of new coroutine to active
     
    513526
    514527// KERNEL_ONLY
    515 void kernel_last_resume( processor * this ) {
    516         coroutine_desc * src = &mainThread->self_cor;
    517         coroutine_desc * dst = get_coroutine(this->runner);
     528static void __kernel_last_resume( processor * this ) {
     529        $coroutine * src = &mainThread->self_cor;
     530        $coroutine * dst = get_coroutine(this->runner);
    518531
    519532        verify( ! kernelTLS.preemption_state.enabled );
     
    522535
    523536        // context switch to the processor
    524         CtxSwitch( &src->context, &dst->context );
     537        __cfactx_switch( &src->context, &dst->context );
    525538}
    526539
    527540//-----------------------------------------------------------------------------
    528541// Scheduler routines
    529 
    530542// KERNEL ONLY
    531 void ScheduleThread( thread_desc * thrd ) {
    532         verify( thrd );
    533         verify( thrd->state != Halted );
    534 
    535         verify( ! kernelTLS.preemption_state.enabled );
    536 
    537         verifyf( thrd->next == 0p, "Expected null got %p", thrd->next );
    538 
    539         with( *thrd->curr_cluster ) {
    540                 lock  ( ready_queue_lock __cfaabi_dbg_ctx2 );
    541                 bool was_empty = !(ready_queue != 0);
    542                 append( ready_queue, thrd );
    543                 unlock( ready_queue_lock );
    544 
    545                 if(was_empty) {
    546                         lock      (proc_list_lock __cfaabi_dbg_ctx2);
    547                         if(idles) {
    548                                 wake_fast(idles.head);
    549                         }
    550                         unlock    (proc_list_lock);
     543void __schedule_thread( $thread * thrd ) with( *thrd->curr_cluster ) {
     544        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     545        /* paranoid */ #if defined( __CFA_WITH_VERIFY__ )
     546        /* paranoid */ if( thrd->state == Inactive || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,
     547                          "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted );
     548        /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active || thrd->state == Rerun,
     549                          "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted );
     550        /* paranoid */ #endif
     551        /* paranoid */ verifyf( thrd->next == 0p, "Expected null got %p", thrd->next );
     552
     553        lock  ( ready_queue_lock __cfaabi_dbg_ctx2 );
     554        bool was_empty = !(ready_queue != 0);
     555        append( ready_queue, thrd );
     556        unlock( ready_queue_lock );
     557
     558        if(was_empty) {
     559                lock      (proc_list_lock __cfaabi_dbg_ctx2);
     560                if(idles) {
     561                        wake_fast(idles.head);
    551562                }
    552                 else if( struct processor * idle = idles.head ) {
    553                         wake_fast(idle);
    554                 }
    555 
    556         }
    557 
    558         verify( ! kernelTLS.preemption_state.enabled );
     563                unlock    (proc_list_lock);
     564        }
     565        else if( struct processor * idle = idles.head ) {
     566                wake_fast(idle);
     567        }
     568
     569        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    559570}
    560571
    561572// KERNEL ONLY
    562 thread_desc * nextThread(cluster * this) with( *this ) {
    563         verify( ! kernelTLS.preemption_state.enabled );
     573static $thread * __next_thread(cluster * this) with( *this ) {
     574        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     575
    564576        lock( ready_queue_lock __cfaabi_dbg_ctx2 );
    565         thread_desc * head = pop_head( ready_queue );
     577        $thread * head = pop_head( ready_queue );
    566578        unlock( ready_queue_lock );
    567         verify( ! kernelTLS.preemption_state.enabled );
     579
     580        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    568581        return head;
    569582}
    570583
    571 void BlockInternal() {
     584void unpark( $thread * thrd ) {
     585        if( !thrd ) return;
     586
    572587        disable_interrupts();
    573         verify( ! kernelTLS.preemption_state.enabled );
     588        static_assert(sizeof(thrd->state) == sizeof(int));
     589        enum coroutine_state old_state = __atomic_exchange_n(&thrd->state, Rerun, __ATOMIC_SEQ_CST);
     590        switch(old_state) {
     591                case Active:
     592                        // Wake won the race, the thread will reschedule/rerun itself
     593                        break;
     594                case Inactive:
     595                        /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION );
     596
     597                        // Wake lost the race,
     598                        thrd->state = Inactive;
     599                        __schedule_thread( thrd );
     600                        break;
     601                case Rerun:
     602                        abort("More than one thread attempted to schedule thread %p\n", thrd);
     603                        break;
     604                case Halted:
     605                case Start:
     606                case Primed:
     607                default:
     608                        // This makes no sense, something is wrong abort
     609                        abort();
     610        }
     611        enable_interrupts( __cfaabi_dbg_ctx );
     612}
     613
     614void park( void ) {
     615        /* paranoid */ verify( kernelTLS.preemption_state.enabled );
     616        disable_interrupts();
     617        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     618        /* paranoid */ verify( kernelTLS.this_thread->preempted == __NO_PREEMPTION );
     619
    574620        returnToKernel();
    575         verify( ! kernelTLS.preemption_state.enabled );
     621
     622        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    576623        enable_interrupts( __cfaabi_dbg_ctx );
    577 }
    578 
    579 void BlockInternal( __spinlock_t * lock ) {
     624        /* paranoid */ verify( kernelTLS.preemption_state.enabled );
     625
     626}
     627
     628// KERNEL ONLY
     629void __leave_thread() {
     630        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     631        returnToKernel();
     632        abort();
     633}
     634
     635// KERNEL ONLY
     636bool force_yield( __Preemption_Reason reason ) {
     637        /* paranoid */ verify( kernelTLS.preemption_state.enabled );
    580638        disable_interrupts();
    581         with( *kernelTLS.this_processor ) {
    582                 finish.action_code = Release;
    583                 finish.lock        = lock;
    584         }
    585 
    586         verify( ! kernelTLS.preemption_state.enabled );
    587         returnToKernel();
    588         verify( ! kernelTLS.preemption_state.enabled );
    589 
    590         enable_interrupts( __cfaabi_dbg_ctx );
    591 }
    592 
    593 void BlockInternal( thread_desc * thrd ) {
    594         disable_interrupts();
    595         with( * kernelTLS.this_processor ) {
    596                 finish.action_code = Schedule;
    597                 finish.thrd        = thrd;
    598         }
    599 
    600         verify( ! kernelTLS.preemption_state.enabled );
    601         returnToKernel();
    602         verify( ! kernelTLS.preemption_state.enabled );
    603 
    604         enable_interrupts( __cfaabi_dbg_ctx );
    605 }
    606 
    607 void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) {
    608         assert(thrd);
    609         disable_interrupts();
    610         with( * kernelTLS.this_processor ) {
    611                 finish.action_code = Release_Schedule;
    612                 finish.lock        = lock;
    613                 finish.thrd        = thrd;
    614         }
    615 
    616         verify( ! kernelTLS.preemption_state.enabled );
    617         returnToKernel();
    618         verify( ! kernelTLS.preemption_state.enabled );
    619 
    620         enable_interrupts( __cfaabi_dbg_ctx );
    621 }
    622 
    623 void BlockInternal(__spinlock_t * locks [], unsigned short count) {
    624         disable_interrupts();
    625         with( * kernelTLS.this_processor ) {
    626                 finish.action_code = Release_Multi;
    627                 finish.locks       = locks;
    628                 finish.lock_count  = count;
    629         }
    630 
    631         verify( ! kernelTLS.preemption_state.enabled );
    632         returnToKernel();
    633         verify( ! kernelTLS.preemption_state.enabled );
    634 
    635         enable_interrupts( __cfaabi_dbg_ctx );
    636 }
    637 
    638 void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) {
    639         disable_interrupts();
    640         with( *kernelTLS.this_processor ) {
    641                 finish.action_code = Release_Multi_Schedule;
    642                 finish.locks       = locks;
    643                 finish.lock_count  = lock_count;
    644                 finish.thrds       = thrds;
    645                 finish.thrd_count  = thrd_count;
    646         }
    647 
    648         verify( ! kernelTLS.preemption_state.enabled );
    649         returnToKernel();
    650         verify( ! kernelTLS.preemption_state.enabled );
    651 
    652         enable_interrupts( __cfaabi_dbg_ctx );
    653 }
    654 
    655 void BlockInternal(__finish_callback_fptr_t callback) {
    656         disable_interrupts();
    657         with( *kernelTLS.this_processor ) {
    658                 finish.action_code = Callback;
    659                 finish.callback    = callback;
    660         }
    661 
    662         verify( ! kernelTLS.preemption_state.enabled );
    663         returnToKernel();
    664         verify( ! kernelTLS.preemption_state.enabled );
    665 
    666         enable_interrupts( __cfaabi_dbg_ctx );
    667 }
    668 
    669 // KERNEL ONLY
    670 void LeaveThread(__spinlock_t * lock, thread_desc * thrd) {
    671         verify( ! kernelTLS.preemption_state.enabled );
    672         with( * kernelTLS.this_processor ) {
    673                 finish.action_code = thrd ? Release_Schedule : Release;
    674                 finish.lock        = lock;
    675                 finish.thrd        = thrd;
    676         }
    677 
    678         returnToKernel();
     639        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     640
     641        $thread * thrd = kernelTLS.this_thread;
     642        /* paranoid */ verify(thrd->state == Active || thrd->state == Rerun);
     643
     644        // SKULLDUGGERY: It is possible that we are preempting this thread just before
     645        // it was going to park itself. If that is the case and it is already using the
     646        // intrusive fields then we can't use them to preempt the thread
     647        // If that is the case, abandon the preemption.
     648        bool preempted = false;
     649        if(thrd->next == 0p) {
     650                preempted = true;
     651                thrd->preempted = reason;
     652                returnToKernel();
     653        }
     654
     655        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     656        enable_interrupts_noPoll();
     657        /* paranoid */ verify( kernelTLS.preemption_state.enabled );
     658
     659        return preempted;
    679660}
    680661
     
    684665//-----------------------------------------------------------------------------
    685666// Kernel boot procedures
    686 static void kernel_startup(void) {
     667static void __kernel_startup(void) {
    687668        verify( ! kernelTLS.preemption_state.enabled );
    688669        __cfaabi_dbg_print_safe("Kernel : Starting\n");
     
    702683        // SKULLDUGGERY: the mainThread steals the process main thread
    703684        // which will then be scheduled by the mainProcessor normally
    704         mainThread = (thread_desc *)&storage_mainThread;
     685        mainThread = ($thread *)&storage_mainThread;
    705686        current_stack_info_t info;
    706687        info.storage = (__stack_t*)&storage_mainThreadCtx;
     
    745726        // Add the main thread to the ready queue
    746727        // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
    747         ScheduleThread(mainThread);
     728        __schedule_thread(mainThread);
    748729
    749730        // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
    750         // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
     731        // context. Hence, the main thread does not begin through __cfactx_invoke_thread, like all other threads. The trick here is that
    751732        // mainThread is on the ready queue when this call is made.
    752         kernel_first_resume( kernelTLS.this_processor );
     733        __kernel_first_resume( kernelTLS.this_processor );
    753734
    754735
     
    762743}
    763744
    764 static void kernel_shutdown(void) {
     745static void __kernel_shutdown(void) {
    765746        __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
    766747
     
    773754        // which is currently here
    774755        __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
    775         kernel_last_resume( kernelTLS.this_processor );
     756        __kernel_last_resume( kernelTLS.this_processor );
    776757        mainThread->self_cor.state = Halted;
    777758
     
    799780// Kernel Quiescing
    800781//=============================================================================================
    801 static void halt(processor * this) with( *this ) {
     782static void __halt(processor * this) with( *this ) {
    802783        // verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
    803784
     
    854835
    855836void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
    856         thread_desc * thrd = kernel_data;
     837        $thread * thrd = kernel_data;
    857838
    858839        if(thrd) {
     
    910891
    911892                // atomically release spin lock and block
    912                 BlockInternal( &lock );
     893                unlock( lock );
     894                park();
    913895        }
    914896        else {
     
    918900
    919901void V(semaphore & this) with( this ) {
    920         thread_desc * thrd = 0p;
     902        $thread * thrd = 0p;
    921903        lock( lock __cfaabi_dbg_ctx2 );
    922904        count += 1;
     
    929911
    930912        // make new owner
    931         WakeThread( thrd );
     913        unpark( thrd );
    932914}
    933915
     
    946928}
    947929
    948 void doregister( cluster * cltr, thread_desc & thrd ) {
     930void doregister( cluster * cltr, $thread & thrd ) {
    949931        lock      (cltr->thread_list_lock __cfaabi_dbg_ctx2);
    950932        cltr->nthreads += 1;
     
    953935}
    954936
    955 void unregister( cluster * cltr, thread_desc & thrd ) {
     937void unregister( cluster * cltr, $thread & thrd ) {
    956938        lock  (cltr->thread_list_lock __cfaabi_dbg_ctx2);
    957939        remove(cltr->threads, thrd );
     
    987969//-----------------------------------------------------------------------------
    988970// Debug
    989 bool threading_enabled(void) {
     971bool threading_enabled(void) __attribute__((const)) {
    990972        return true;
    991973}
Note: See TracChangeset for help on using the changeset viewer.