Ignore:
File:
1 edited

Legend:

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

    r09d4b22 r2026bb6  
    1010// Created On       : Tue Jan 17 12:27:26 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Dec  5 16:25:52 2019
    13 // Update Count     : 52
     12// Last Modified On : Thu Jun 20 17:21:23 2019
     13// Update Count     : 25
    1414//
    1515
     
    2626#include <signal.h>
    2727#include <unistd.h>
    28 #include <limits.h>                                                                             // PTHREAD_STACK_MIN
    29 #include <sys/mman.h>                                                                   // mprotect
    3028}
    3129
     
    4240//-----------------------------------------------------------------------------
    4341// Some assembly required
    44 #if defined( __i386 )
     42#if   defined( __i386 )
    4543        #define CtxGet( ctx )        \
    4644                __asm__ volatile (     \
     
    125123
    126124extern "C" {
    127         struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
     125struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
    128126}
    129127
     
    133131// Global state
    134132thread_local struct KernelThreadData kernelTLS __attribute__ ((tls_model ( "initial-exec" ))) = {
    135         NULL,                                                                                           // cannot use 0p
    136133        NULL,
    137         { 1, false, false },
    138         6u //this should be seeded better but due to a bug calling rdtsc doesn't work
     134        NULL,
     135        { 1, false, false }
    139136};
    140137
     
    142139// Struct to steal stack
    143140struct current_stack_info_t {
    144         __stack_t * storage;                                                            // pointer to stack object
    145         void * base;                                                                            // base of stack
    146         void * limit;                                                                           // stack grows towards stack limit
    147         void * context;                                                                         // address of cfa_context_t
     141        __stack_t * storage;            // pointer to stack object
     142        void *base;                             // base of stack
     143        void *limit;                    // stack grows towards stack limit
     144        void *context;                  // address of cfa_context_t
    148145};
    149146
     
    174171        name = "Main Thread";
    175172        state = Start;
    176         starter = 0p;
    177         last = 0p;
    178         cancellation = 0p;
     173        starter = NULL;
     174        last = NULL;
     175        cancellation = NULL;
    179176}
    180177
     
    187184        self_mon.recursion = 1;
    188185        self_mon_p = &self_mon;
    189         next = 0p;
    190 
    191         node.next = 0p;
    192         node.prev = 0p;
     186        next = NULL;
     187
     188        node.next = NULL;
     189        node.prev = NULL;
    193190        doregister(curr_cluster, this);
    194191
     
    214211        terminated{ 0 };
    215212        do_terminate = false;
    216         preemption_alarm = 0p;
     213        preemption_alarm = NULL;
    217214        pending_preemption = false;
    218215        runner.proc = &this;
     
    234231        }
    235232
    236         pthread_join( kernel_thread, 0p );
    237         free( this.stack );
     233        pthread_join( kernel_thread, NULL );
    238234}
    239235
     
    264260//Main of the processor contexts
    265261void main(processorCtx_t & runner) {
    266         // Because of a bug, we couldn't initialized the seed on construction
    267         // Do it here
    268         kernelTLS.rand_seed ^= rdtscl();
    269 
    270262        processor * this = runner.proc;
    271263        verify(this);
     
    281273                __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
    282274
    283                 thread_desc * readyThread = 0p;
    284                 for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ ) {
     275                thread_desc * readyThread = NULL;
     276                for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ )
     277                {
    285278                        readyThread = nextThread( this->cltr );
    286279
    287                         if(readyThread) {
     280                        if(readyThread)
     281                        {
    288282                                verify( ! kernelTLS.preemption_state.enabled );
    289283
     
    296290
    297291                                spin_count = 0;
    298                         } else {
     292                        }
     293                        else
     294                        {
    299295                                // spin(this, &spin_count);
    300296                                halt(this);
     
    409405        processor * proc = (processor *) arg;
    410406        kernelTLS.this_processor = proc;
    411         kernelTLS.this_thread    = 0p;
     407        kernelTLS.this_thread    = NULL;
    412408        kernelTLS.preemption_state.[enabled, disable_count] = [false, 1];
    413409        // SKULLDUGGERY: We want to create a context for the processor coroutine
     
    422418
    423419        //Set global state
    424         kernelTLS.this_thread = 0p;
     420        kernelTLS.this_thread    = NULL;
    425421
    426422        //We now have a proper context from which to schedule threads
     
    438434        __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner);
    439435
    440         return 0p;
    441 }
    442 
    443 static void Abort( int ret, const char * func ) {
    444         if ( ret ) {                                                                            // pthread routines return errno values
    445                 abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) );
    446         } // if
    447 } // Abort
    448 
    449 void * create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) {
    450         pthread_attr_t attr;
    451 
    452         Abort( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
    453 
    454         size_t stacksize;
    455         // default stack size, normally defined by shell limit
    456         Abort( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
    457         assert( stacksize >= PTHREAD_STACK_MIN );
    458 
    459         void * stack;
    460         __cfaabi_dbg_debug_do(
    461                 stack = memalign( __page_size, stacksize + __page_size );
    462                 // pthread has no mechanism to create the guard page in user supplied stack.
    463                 if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) {
    464                         abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) );
    465                 } // if
    466         );
    467         __cfaabi_dbg_no_debug_do(
    468                 stack = malloc( stacksize );
    469         );
    470 
    471         Abort( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
    472 
    473         Abort( pthread_create( pthread, &attr, start, arg ), "pthread_create" );
    474         return stack;
     436        return NULL;
    475437}
    476438
     
    478440        __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this);
    479441
    480         this->stack = create_pthread( &this->kernel_thread, CtxInvokeProcessor, (void *)this );
     442        pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
    481443
    482444        __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
     
    535497        verify( ! kernelTLS.preemption_state.enabled );
    536498
    537         verifyf( thrd->next == 0p, "Expected null got %p", thrd->next );
     499        verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
    538500
    539501        with( *thrd->curr_cluster ) {
     
    714676        void ?{}(processorCtx_t & this, processor * proc) {
    715677                (this.__cor){ "Processor" };
    716                 this.__cor.starter = 0p;
     678                this.__cor.starter = NULL;
    717679                this.proc = proc;
    718680        }
     
    723685                terminated{ 0 };
    724686                do_terminate = false;
    725                 preemption_alarm = 0p;
     687                preemption_alarm = NULL;
    726688                pending_preemption = false;
    727689                kernel_thread = pthread_self();
     
    857819        if(thrd) {
    858820                int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
    859                 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
     821                __cfaabi_dbg_bits_write( abort_text, len );
    860822
    861823                if ( &thrd->self_cor != thrd->curr_cor ) {
    862824                        len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
    863                         __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
     825                        __cfaabi_dbg_bits_write( abort_text, len );
    864826                }
    865827                else {
    866                         __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
     828                        __cfaabi_dbg_bits_write( ".\n", 2 );
    867829                }
    868830        }
    869831        else {
    870832                int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
    871                 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
     833                __cfaabi_dbg_bits_write( abort_text, len );
    872834        }
    873835}
     
    880842
    881843extern "C" {
    882         void __cfaabi_bits_acquire() {
     844        void __cfaabi_dbg_bits_acquire() {
    883845                lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
    884846        }
    885847
    886         void __cfaabi_bits_release() {
     848        void __cfaabi_dbg_bits_release() {
    887849                unlock( kernel_debug_lock );
    888850        }
     
    917879
    918880void V(semaphore & this) with( this ) {
    919         thread_desc * thrd = 0p;
     881        thread_desc * thrd = NULL;
    920882        lock( lock __cfaabi_dbg_ctx2 );
    921883        count += 1;
Note: See TracChangeset for help on using the changeset viewer.