Ignore:
File:
1 edited

Legend:

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

    r4aa2fb2 rcd17862  
    1515//
    1616
    17 #include "startup.h"
    18 
    19 //Start and stop routine for the kernel, declared first to make sure they run first
    20 void kernel_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
    21 void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
    22 
    23 //Header
    24 #include "kernel_private.h"
     17#include "libhdr.h"
    2518
    2619//C Includes
     
    3528
    3629//CFA Includes
    37 #include "libhdr.h"
     30#include "kernel_private.h"
    3831#include "preemption.h"
     32#include "startup.h"
    3933
    4034//Private includes
    4135#define __CFA_INVOKE_PRIVATE__
    4236#include "invoke.h"
     37
     38//Start and stop routine for the kernel, declared first to make sure they run first
     39void kernel_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
     40void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
    4341
    4442//-----------------------------------------------------------------------------
     
    5957// Global state
    6058
    61 thread_local processor * this_processor;
    62 
    63 coroutine_desc * this_coroutine(void) {
    64         return this_processor->current_coroutine;
    65 }
    66 
    67 thread_desc * this_thread(void) {
    68         return this_processor->current_thread;
    69 }
     59volatile thread_local processor * this_processor;
     60volatile thread_local coroutine_desc * this_coroutine;
     61volatile thread_local thread_desc * this_thread;
     62volatile thread_local unsigned short disable_preempt_count = 1;
    7063
    7164//-----------------------------------------------------------------------------
    7265// Main thread construction
    7366struct current_stack_info_t {
    74         machine_context_t ctx; 
     67        machine_context_t ctx;
    7568        unsigned int size;              // size of stack
    7669        void *base;                             // base of stack
     
    10699
    107100void ?{}( coroutine_desc * this, current_stack_info_t * info) {
    108         (&this->stack){ info }; 
     101        (&this->stack){ info };
    109102        this->name = "Main Thread";
    110103        this->errno_ = 0;
     
    136129void ?{}(processor * this, cluster * cltr) {
    137130        this->cltr = cltr;
    138         this->current_coroutine = NULL;
    139         this->current_thread = NULL;
    140131        (&this->terminated){};
    141132        this->is_terminated = false;
    142133        this->preemption_alarm = NULL;
    143134        this->preemption = default_preemption();
    144         this->disable_preempt_count = 1;                //Start with interrupts disabled
    145135        this->pending_preemption = false;
    146136
     
    150140void ?{}(processor * this, cluster * cltr, processorCtx_t * runner) {
    151141        this->cltr = cltr;
    152         this->current_coroutine = NULL;
    153         this->current_thread = NULL;
    154142        (&this->terminated){};
    155143        this->is_terminated = false;
    156         this->disable_preempt_count = 0;
     144        this->preemption_alarm = NULL;
     145        this->preemption = default_preemption();
    157146        this->pending_preemption = false;
     147        this->kernel_thread = pthread_self();
    158148
    159149        this->runner = runner;
    160         LIB_DEBUG_PRINT_SAFE("Kernel : constructing processor context %p\n", runner);
     150        LIB_DEBUG_PRINT_SAFE("Kernel : constructing system processor context %p\n", runner);
    161151        runner{ this };
    162152}
     153
     154LIB_DEBUG_DO( bool validate( alarm_list_t * this ); )
    163155
    164156void ?{}(system_proc_t * this, cluster * cltr, processorCtx_t * runner) {
     
    168160
    169161        (&this->proc){ cltr, runner };
     162
     163        verify( validate( &this->alarms ) );
    170164}
    171165
     
    184178
    185179void ^?{}(cluster * this) {
    186        
     180
    187181}
    188182
     
    203197
    204198                thread_desc * readyThread = NULL;
    205                 for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ ) 
     199                for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ )
    206200                {
    207201                        readyThread = nextThread( this->cltr );
     
    209203                        if(readyThread)
    210204                        {
     205                                verify( disable_preempt_count > 0 );
     206
    211207                                runThread(this, readyThread);
     208
     209                                verify( disable_preempt_count > 0 );
    212210
    213211                                //Some actions need to be taken from the kernel
     
    229227}
    230228
    231 // runThread runs a thread by context switching 
    232 // from the processor coroutine to the target thread 
     229// runThread runs a thread by context switching
     230// from the processor coroutine to the target thread
    233231void runThread(processor * this, thread_desc * dst) {
    234232        coroutine_desc * proc_cor = get_coroutine(this->runner);
    235233        coroutine_desc * thrd_cor = get_coroutine(dst);
    236        
     234
    237235        //Reset the terminating actions here
    238236        this->finish.action_code = No_Action;
    239237
    240238        //Update global state
    241         this->current_thread = dst;
     239        this_thread = dst;
    242240
    243241        // Context Switch to the thread
     
    246244}
    247245
    248 // Once a thread has finished running, some of 
     246// Once a thread has finished running, some of
    249247// its final actions must be executed from the kernel
    250248void finishRunning(processor * this) {
     
    256254        }
    257255        else if( this->finish.action_code == Release_Schedule ) {
    258                 unlock( this->finish.lock );           
     256                unlock( this->finish.lock );
    259257                ScheduleThread( this->finish.thrd );
    260258        }
     
    289287        processor * proc = (processor *) arg;
    290288        this_processor = proc;
     289        this_coroutine = NULL;
     290        this_thread = NULL;
     291        disable_preempt_count = 1;
    291292        // SKULLDUGGERY: We want to create a context for the processor coroutine
    292293        // which is needed for the 2-step context switch. However, there is no reason
    293         // to waste the perfectly valid stack create by pthread. 
     294        // to waste the perfectly valid stack create by pthread.
    294295        current_stack_info_t info;
    295296        machine_context_t ctx;
     
    300301
    301302        //Set global state
    302         proc->current_coroutine = &proc->runner->__cor;
    303         proc->current_thread = NULL;
     303        this_coroutine = &proc->runner->__cor;
     304        this_thread = NULL;
    304305
    305306        //We now have a proper context from which to schedule threads
    306307        LIB_DEBUG_PRINT_SAFE("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
    307308
    308         // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't 
    309         // resume it to start it like it normally would, it will just context switch 
    310         // back to here. Instead directly call the main since we already are on the 
     309        // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
     310        // resume it to start it like it normally would, it will just context switch
     311        // back to here. Instead directly call the main since we already are on the
    311312        // appropriate stack.
    312313        proc_cor_storage.__cor.state = Active;
     
    315316
    316317        // Main routine of the core returned, the core is now fully terminated
    317         LIB_DEBUG_PRINT_SAFE("Kernel : core %p main ended (%p)\n", proc, proc->runner); 
     318        LIB_DEBUG_PRINT_SAFE("Kernel : core %p main ended (%p)\n", proc, proc->runner);
    318319
    319320        return NULL;
     
    322323void start(processor * this) {
    323324        LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this);
    324        
     325
    325326        pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
    326327
    327         LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);       
     328        LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);
    328329}
    329330
     
    331332// Scheduler routines
    332333void ScheduleThread( thread_desc * thrd ) {
    333         if( !thrd ) return;
     334        // if( !thrd ) return;
     335        assert( thrd );
     336        assert( thrd->cor.state != Halted );
     337
     338        verify( disable_preempt_count > 0 );
    334339
    335340        verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
    336        
    337         lock( &systemProcessor->proc.cltr->lock );
     341
     342        lock( &systemProcessor->proc.cltr->lock DEBUG_CTX2 );
    338343        append( &systemProcessor->proc.cltr->ready_queue, thrd );
    339344        unlock( &systemProcessor->proc.cltr->lock );
     345
     346        verify( disable_preempt_count > 0 );
    340347}
    341348
    342349thread_desc * nextThread(cluster * this) {
    343         lock( &this->lock );
     350        verify( disable_preempt_count > 0 );
     351        lock( &this->lock DEBUG_CTX2 );
    344352        thread_desc * head = pop_head( &this->ready_queue );
    345353        unlock( &this->lock );
     354        verify( disable_preempt_count > 0 );
    346355        return head;
    347356}
    348357
    349 void ScheduleInternal() {
     358void BlockInternal() {
     359        disable_interrupts();
     360        verify( disable_preempt_count > 0 );
    350361        suspend();
    351 }
    352 
    353 void ScheduleInternal( spinlock * lock ) {
     362        verify( disable_preempt_count > 0 );
     363        enable_interrupts( DEBUG_CTX );
     364}
     365
     366void BlockInternal( spinlock * lock ) {
     367        disable_interrupts();
    354368        this_processor->finish.action_code = Release;
    355369        this_processor->finish.lock = lock;
     370
     371        verify( disable_preempt_count > 0 );
    356372        suspend();
    357 }
    358 
    359 void ScheduleInternal( thread_desc * thrd ) {
     373        verify( disable_preempt_count > 0 );
     374
     375        enable_interrupts( DEBUG_CTX );
     376}
     377
     378void BlockInternal( thread_desc * thrd ) {
     379        disable_interrupts();
     380        assert( thrd->cor.state != Halted );
    360381        this_processor->finish.action_code = Schedule;
    361382        this_processor->finish.thrd = thrd;
     383
     384        verify( disable_preempt_count > 0 );
    362385        suspend();
    363 }
    364 
    365 void ScheduleInternal( spinlock * lock, thread_desc * thrd ) {
     386        verify( disable_preempt_count > 0 );
     387
     388        enable_interrupts( DEBUG_CTX );
     389}
     390
     391void BlockInternal( spinlock * lock, thread_desc * thrd ) {
     392        disable_interrupts();
    366393        this_processor->finish.action_code = Release_Schedule;
    367394        this_processor->finish.lock = lock;
    368395        this_processor->finish.thrd = thrd;
     396
     397        verify( disable_preempt_count > 0 );
    369398        suspend();
    370 }
    371 
    372 void ScheduleInternal(spinlock ** locks, unsigned short count) {
     399        verify( disable_preempt_count > 0 );
     400
     401        enable_interrupts( DEBUG_CTX );
     402}
     403
     404void BlockInternal(spinlock ** locks, unsigned short count) {
     405        disable_interrupts();
    373406        this_processor->finish.action_code = Release_Multi;
    374407        this_processor->finish.locks = locks;
    375408        this_processor->finish.lock_count = count;
     409
     410        verify( disable_preempt_count > 0 );
    376411        suspend();
    377 }
    378 
    379 void ScheduleInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) {
     412        verify( disable_preempt_count > 0 );
     413
     414        enable_interrupts( DEBUG_CTX );
     415}
     416
     417void BlockInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) {
     418        disable_interrupts();
    380419        this_processor->finish.action_code = Release_Multi_Schedule;
    381420        this_processor->finish.locks = locks;
     
    383422        this_processor->finish.thrds = thrds;
    384423        this_processor->finish.thrd_count = thrd_count;
     424
     425        verify( disable_preempt_count > 0 );
    385426        suspend();
     427        verify( disable_preempt_count > 0 );
     428
     429        enable_interrupts( DEBUG_CTX );
    386430}
    387431
     
    392436// Kernel boot procedures
    393437void kernel_startup(void) {
    394         LIB_DEBUG_PRINT_SAFE("Kernel : Starting\n");   
     438        LIB_DEBUG_PRINT_SAFE("Kernel : Starting\n");
    395439
    396440        // Start by initializing the main thread
    397         // SKULLDUGGERY: the mainThread steals the process main thread 
     441        // SKULLDUGGERY: the mainThread steals the process main thread
    398442        // which will then be scheduled by the systemProcessor normally
    399443        mainThread = (thread_desc *)&mainThread_storage;
     
    403447        LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n");
    404448
    405         // Enable preemption
    406         kernel_start_preemption();
    407 
    408449        // Initialize the system cluster
    409450        systemCluster = (cluster *)&systemCluster_storage;
     
    417458        systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtx_storage };
    418459
    419         // Add the main thread to the ready queue 
     460        // Add the main thread to the ready queue
    420461        // once resume is called on systemProcessor->runner the mainThread needs to be scheduled like any normal thread
    421462        ScheduleThread(mainThread);
     
    423464        //initialize the global state variables
    424465        this_processor = &systemProcessor->proc;
    425         this_processor->current_thread = mainThread;
    426         this_processor->current_coroutine = &mainThread->cor;
     466        this_thread = mainThread;
     467        this_coroutine = &mainThread->cor;
     468        disable_preempt_count = 1;
     469
     470        // Enable preemption
     471        kernel_start_preemption();
    427472
    428473        // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
    429474        // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
    430         // mainThread is on the ready queue when this call is made. 
     475        // mainThread is on the ready queue when this call is made.
    431476        resume( systemProcessor->proc.runner );
    432477
     
    435480        // THE SYSTEM IS NOW COMPLETELY RUNNING
    436481        LIB_DEBUG_PRINT_SAFE("Kernel : Started\n--------------------------------------------------\n\n");
     482
     483        enable_interrupts( DEBUG_CTX );
    437484}
    438485
    439486void kernel_shutdown(void) {
    440487        LIB_DEBUG_PRINT_SAFE("\n--------------------------------------------------\nKernel : Shutting down\n");
     488
     489        disable_interrupts();
    441490
    442491        // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
     
    448497        // THE SYSTEM IS NOW COMPLETELY STOPPED
    449498
     499        // Disable preemption
     500        kernel_stop_preemption();
     501
    450502        // Destroy the system processor and its context in reverse order of construction
    451503        // These were manually constructed so we need manually destroy them
     
    457509        ^(mainThread){};
    458510
    459         LIB_DEBUG_PRINT_SAFE("Kernel : Shutdown complete\n");   
     511        LIB_DEBUG_PRINT_SAFE("Kernel : Shutdown complete\n");
    460512}
    461513
     
    467519        // abort cannot be recursively entered by the same or different processors because all signal handlers return when
    468520        // the globalAbort flag is true.
    469         lock( &kernel_abort_lock );
     521        lock( &kernel_abort_lock DEBUG_CTX2 );
    470522
    471523        // first task to abort ?
     
    473525                kernel_abort_called = true;
    474526                unlock( &kernel_abort_lock );
    475         } 
     527        }
    476528        else {
    477529                unlock( &kernel_abort_lock );
    478                
     530
    479531                sigset_t mask;
    480532                sigemptyset( &mask );
     
    482534                sigaddset( &mask, SIGUSR1 );                    // block SIGUSR1 signals
    483535                sigsuspend( &mask );                            // block the processor to prevent further damage during abort
    484                 _exit( EXIT_FAILURE );                          // if processor unblocks before it is killed, terminate it             
    485         }
    486 
    487         return this_thread();
     536                _exit( EXIT_FAILURE );                          // if processor unblocks before it is killed, terminate it
     537        }
     538
     539        return this_thread;
    488540}
    489541
     
    494546        __lib_debug_write( STDERR_FILENO, abort_text, len );
    495547
    496         if ( thrd != this_coroutine() ) {
    497                 len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine()->name, this_coroutine() );
     548        if ( thrd != this_coroutine ) {
     549                len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine->name, this_coroutine );
    498550                __lib_debug_write( STDERR_FILENO, abort_text, len );
    499         } 
     551        }
    500552        else {
    501553                __lib_debug_write( STDERR_FILENO, ".\n", 2 );
     
    505557extern "C" {
    506558        void __lib_debug_acquire() {
    507                 lock(&kernel_debug_lock);
     559                lock( &kernel_debug_lock DEBUG_CTX2 );
    508560        }
    509561
    510562        void __lib_debug_release() {
    511                 unlock(&kernel_debug_lock);
     563                unlock( &kernel_debug_lock );
    512564        }
    513565}
     
    525577}
    526578
    527 bool try_lock( spinlock * this ) {
     579bool try_lock( spinlock * this DEBUG_CTX_PARAM2 ) {
    528580        return this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0;
    529581}
    530582
    531 void lock( spinlock * this ) {
     583void lock( spinlock * this DEBUG_CTX_PARAM2 ) {
    532584        for ( unsigned int i = 1;; i += 1 ) {
    533                 if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) break;
    534         }
    535 }
     585                if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) { break; }
     586        }
     587        LIB_DEBUG_DO(
     588                this->prev_name = caller;
     589                this->prev_thrd = this_thread;
     590        )
     591}
     592
     593void lock_yield( spinlock * this DEBUG_CTX_PARAM2 ) {
     594        for ( unsigned int i = 1;; i += 1 ) {
     595                if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) { break; }
     596                yield();
     597        }
     598        LIB_DEBUG_DO(
     599                this->prev_name = caller;
     600                this->prev_thrd = this_thread;
     601        )
     602}
     603
    536604
    537605void unlock( spinlock * this ) {
     
    547615
    548616void wait( signal_once * this ) {
    549         lock( &this->lock );
     617        lock( &this->lock DEBUG_CTX2 );
    550618        if( !this->cond ) {
    551                 append( &this->blocked, this_thread() );
    552                 ScheduleInternal( &this->lock );
    553                 lock( &this->lock );
    554         }
    555         unlock( &this->lock );
     619                append( &this->blocked, (thread_desc*)this_thread );
     620                BlockInternal( &this->lock );
     621        }
     622        else {
     623                unlock( &this->lock );
     624        }
    556625}
    557626
    558627void signal( signal_once * this ) {
    559         lock( &this->lock );
     628        lock( &this->lock DEBUG_CTX2 );
    560629        {
    561630                this->cond = true;
    562631
     632                disable_interrupts();
    563633                thread_desc * it;
    564634                while( it = pop_head( &this->blocked) ) {
    565635                        ScheduleThread( it );
    566636                }
     637                enable_interrupts( DEBUG_CTX );
    567638        }
    568639        unlock( &this->lock );
     
    590661                }
    591662                head->next = NULL;
    592         }       
     663        }
    593664        return head;
    594665}
     
    609680                this->top = top->next;
    610681                top->next = NULL;
    611         }       
     682        }
    612683        return top;
    613684}
Note: See TracChangeset for help on using the changeset viewer.