Ignore:
File:
1 edited

Legend:

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

    r4e6fb8e r4aa2fb2  
    5959// Global state
    6060
    61 volatile thread_local processor * this_processor;
    62 volatile thread_local unsigned short disable_preempt_count;
     61thread_local processor * this_processor;
    6362
    6463coroutine_desc * this_coroutine(void) {
     
    143142        this->preemption_alarm = NULL;
    144143        this->preemption = default_preemption();
     144        this->disable_preempt_count = 1;                //Start with interrupts disabled
    145145        this->pending_preemption = false;
    146146
     
    154154        (&this->terminated){};
    155155        this->is_terminated = false;
    156         this->preemption_alarm = NULL;
    157         this->preemption = default_preemption();
     156        this->disable_preempt_count = 0;
    158157        this->pending_preemption = false;
    159         this->kernel_thread = pthread_self();
    160158
    161159        this->runner = runner;
    162         LIB_DEBUG_PRINT_SAFE("Kernel : constructing system processor context %p\n", runner);
     160        LIB_DEBUG_PRINT_SAFE("Kernel : constructing processor context %p\n", runner);
    163161        runner{ this };
    164162}
    165 
    166 LIB_DEBUG_DO( bool validate( alarm_list_t * this ); )
    167163
    168164void ?{}(system_proc_t * this, cluster * cltr, processorCtx_t * runner) {
     
    172168
    173169        (&this->proc){ cltr, runner };
    174 
    175         LIB_DEBUG_DO( assert( validate( &this->alarms ) ) );
    176170}
    177171
     
    215209                        if(readyThread)
    216210                        {
    217                                 assert( disable_preempt_count > 0 );
    218 
    219211                                runThread(this, readyThread);
    220 
    221                                 assert( disable_preempt_count > 0 );
    222212
    223213                                //Some actions need to be taken from the kernel
     
    299289        processor * proc = (processor *) arg;
    300290        this_processor = proc;
    301         disable_preempt_count = 1;
    302291        // SKULLDUGGERY: We want to create a context for the processor coroutine
    303292        // which is needed for the 2-step context switch. However, there is no reason
     
    322311        // appropriate stack.
    323312        proc_cor_storage.__cor.state = Active;
    324       main( &proc_cor_storage );
    325       proc_cor_storage.__cor.state = Halted;
     313        main( &proc_cor_storage );
     314        proc_cor_storage.__cor.state = Halted;
    326315
    327316        // Main routine of the core returned, the core is now fully terminated
     
    333322void start(processor * this) {
    334323        LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this);
    335 
    336         // SIGALRM must only be caught by the system processor
    337         sigset_t old_mask;
    338         bool is_system_proc = this_processor == &systemProcessor->proc;
    339         if ( is_system_proc ) {
    340                 // Child kernel-thread inherits the signal mask from the parent kernel-thread. So one special case for the
    341                 // system processor creating the user processor => toggle the blocking SIGALRM on system processor, create user
    342                 // processor, and toggle back (below) previous signal mask of the system processor.
    343 
    344                 sigset_t new_mask;
    345                 sigemptyset( &new_mask );
    346                 sigemptyset( &old_mask );
    347                 sigaddset( &new_mask, SIGALRM );
    348 
    349                 if ( sigprocmask( SIG_BLOCK, &new_mask, &old_mask ) == -1 ) {
    350                         abortf( "internal error, sigprocmask" );
    351                 }
    352 
    353                 assert( ! sigismember( &old_mask, SIGALRM ) );
    354         }
    355 
     324       
    356325        pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
    357 
    358         // Toggle back previous signal mask of system processor.
    359         if ( is_system_proc ) {
    360                 if ( sigprocmask( SIG_SETMASK, &old_mask, NULL ) == -1 ) {
    361                         abortf( "internal error, sigprocmask" );
    362                 } // if
    363         } // if
    364326
    365327        LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);       
     
    371333        if( !thrd ) return;
    372334
    373         assertf( thrd->next == NULL, "Expected null got %p", thrd->next );
     335        verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
    374336       
    375337        lock( &systemProcessor->proc.cltr->lock );
     
    385347}
    386348
    387 void BlockInternal() {
    388         disable_interrupts();
    389         assert( disable_preempt_count > 0 );
     349void ScheduleInternal() {
    390350        suspend();
    391         assert( disable_preempt_count > 0 );
    392         enable_interrupts( __PRETTY_FUNCTION__ );
    393 }
    394 
    395 void BlockInternal( spinlock * lock ) {
    396         disable_interrupts();
     351}
     352
     353void ScheduleInternal( spinlock * lock ) {
    397354        this_processor->finish.action_code = Release;
    398355        this_processor->finish.lock = lock;
    399         assert( disable_preempt_count > 0 );
    400356        suspend();
    401         assert( disable_preempt_count > 0 );
    402         enable_interrupts( __PRETTY_FUNCTION__ );
    403 }
    404 
    405 void BlockInternal( thread_desc * thrd ) {
    406         disable_interrupts();
     357}
     358
     359void ScheduleInternal( thread_desc * thrd ) {
    407360        this_processor->finish.action_code = Schedule;
    408361        this_processor->finish.thrd = thrd;
    409         assert( disable_preempt_count > 0 );
    410362        suspend();
    411         assert( disable_preempt_count > 0 );
    412         enable_interrupts( __PRETTY_FUNCTION__ );
    413 }
    414 
    415 void BlockInternal( spinlock * lock, thread_desc * thrd ) {
    416         disable_interrupts();
     363}
     364
     365void ScheduleInternal( spinlock * lock, thread_desc * thrd ) {
    417366        this_processor->finish.action_code = Release_Schedule;
    418367        this_processor->finish.lock = lock;
    419368        this_processor->finish.thrd = thrd;
    420         assert( disable_preempt_count > 0 );
    421369        suspend();
    422         assert( disable_preempt_count > 0 );
    423         enable_interrupts( __PRETTY_FUNCTION__ );
    424 }
    425 
    426 void BlockInternal(spinlock ** locks, unsigned short count) {
    427         disable_interrupts();
     370}
     371
     372void ScheduleInternal(spinlock ** locks, unsigned short count) {
    428373        this_processor->finish.action_code = Release_Multi;
    429374        this_processor->finish.locks = locks;
    430375        this_processor->finish.lock_count = count;
    431         assert( disable_preempt_count > 0 );
    432376        suspend();
    433         assert( disable_preempt_count > 0 );
    434         enable_interrupts( __PRETTY_FUNCTION__ );
    435 }
    436 
    437 void BlockInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) {
    438         disable_interrupts();
     377}
     378
     379void ScheduleInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) {
    439380        this_processor->finish.action_code = Release_Multi_Schedule;
    440381        this_processor->finish.locks = locks;
     
    442383        this_processor->finish.thrds = thrds;
    443384        this_processor->finish.thrd_count = thrd_count;
    444         assert( disable_preempt_count > 0 );
    445385        suspend();
    446         assert( disable_preempt_count > 0 );
    447         enable_interrupts( __PRETTY_FUNCTION__ );
    448386}
    449387
     
    465403        LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n");
    466404
     405        // Enable preemption
     406        kernel_start_preemption();
     407
    467408        // Initialize the system cluster
    468409        systemCluster = (cluster *)&systemCluster_storage;
     
    484425        this_processor->current_thread = mainThread;
    485426        this_processor->current_coroutine = &mainThread->cor;
    486         disable_preempt_count = 1;
    487 
    488         // Enable preemption
    489         kernel_start_preemption();
    490427
    491428        // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
     
    498435        // THE SYSTEM IS NOW COMPLETELY RUNNING
    499436        LIB_DEBUG_PRINT_SAFE("Kernel : Started\n--------------------------------------------------\n\n");
    500 
    501         enable_interrupts( __PRETTY_FUNCTION__ );
    502437}
    503438
    504439void kernel_shutdown(void) {
    505440        LIB_DEBUG_PRINT_SAFE("\n--------------------------------------------------\nKernel : Shutting down\n");
    506 
    507         disable_interrupts();
    508441
    509442        // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
     
    514447
    515448        // THE SYSTEM IS NOW COMPLETELY STOPPED
    516 
    517         // Disable preemption
    518         kernel_stop_preemption();
    519449
    520450        // Destroy the system processor and its context in reverse order of construction
     
    620550        if( !this->cond ) {
    621551                append( &this->blocked, this_thread() );
    622                 BlockInternal( &this->lock );
    623         }
    624         else {
    625                 unlock( &this->lock );
    626         }
     552                ScheduleInternal( &this->lock );
     553                lock( &this->lock );
     554        }
     555        unlock( &this->lock );
    627556}
    628557
     
    632561                this->cond = true;
    633562
    634                 disable_interrupts();
    635563                thread_desc * it;
    636564                while( it = pop_head( &this->blocked) ) {
    637565                        ScheduleThread( it );
    638566                }
    639                 enable_interrupts( __PRETTY_FUNCTION__ );
    640567        }
    641568        unlock( &this->lock );
     
    650577
    651578void append( __thread_queue_t * this, thread_desc * t ) {
    652         assert(this->tail != NULL);
     579        verify(this->tail != NULL);
    653580        *this->tail = t;
    654581        this->tail = &t->next;
     
    672599
    673600void push( __condition_stack_t * this, __condition_criterion_t * t ) {
    674         assert( !t->next );
     601        verify( !t->next );
    675602        t->next = this->top;
    676603        this->top = t;
Note: See TracChangeset for help on using the changeset viewer.