Changes in / [124c1b7:7d94bfe3]


Ignore:
Location:
libcfa/src/concurrency
Files:
3 edited

Legend:

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

    r124c1b7 r7d94bfe3  
    228228static void * __invoke_processor(void * arg);
    229229
    230 static init(processor & this, const char name[], cluster & _cltr) with( this ) {
     230void ?{}(processor & this, const char name[], cluster & _cltr) with( this ) {
    231231        this.name = name;
    232232        this.cltr = &_cltr;
    233233        id = -1u;
     234        terminated{ 0 };
    234235        destroyer = 0p;
    235236        do_terminate = false;
    236237        preemption_alarm = 0p;
    237238        pending_preemption = false;
     239        runner.proc = &this;
    238240
    239241        #if !defined(__CFA_NO_STATISTICS__)
     
    242244        #endif
    243245
     246        idle{};
     247
     248        __cfadbg_print_safe(runtime_core, "Kernel : Starting core %p\n", &this);
     249
     250        this.stack = __create_pthread( &this.kernel_thread, __invoke_processor, (void *)&this );
    244251        __atomic_fetch_add( &cltr->nprocessors, 1u, __ATOMIC_SEQ_CST );
    245252
    246         id = doregister((__processor_id_t*)&this);
    247 
    248         // Lock the RWlock so no-one pushes/pops while we are changing the queue
    249         uint_fast32_t last_size = ready_mutate_lock();
    250 
    251                 // Adjust the ready queue size
    252                 ready_queue_grow( cltr );
    253 
    254         // Unlock the RWlock
    255         ready_mutate_unlock( last_size );
    256 
    257253        __cfadbg_print_safe(runtime_core, "Kernel : core %p created\n", &this);
    258 }
    259 
    260 // Not a ctor, it just preps the destruction but should not destroy members
    261 void deinit(processor & this) {
    262         // Lock the RWlock so no-one pushes/pops while we are changing the queue
    263         uint_fast32_t last_size = ready_mutate_lock();
    264 
    265                 // Adjust the ready queue size
    266                 ready_queue_shrink( this.cltr );
    267 
    268                 // Make sure we aren't on the idle queue
    269                 unsafe_remove( this.cltr->idles, &this );
    270 
    271         // Unlock the RWlock
    272         ready_mutate_unlock( last_size );
    273 
    274         // Finally we don't need the read_lock any more
    275         unregister((__processor_id_t*)&this);
    276 }
    277 
    278 void ?{}(processor & this, const char name[], cluster & _cltr) {
    279         ( this.idle ){};
    280         ( this.terminated ){ 0 };
    281         ( this.runner ){};
    282         init( this, name, _cltr );
    283 
    284         __cfadbg_print_safe(runtime_core, "Kernel : Starting core %p\n", &this);
    285 
    286         this.stack = __create_pthread( &this.kernel_thread, __invoke_processor, (void *)&this );
    287 
    288254}
    289255
     
    303269
    304270        free( this.stack );
    305 
    306         deinit( this );
    307271
    308272        __atomic_fetch_sub( &cltr->nprocessors, 1u, __ATOMIC_SEQ_CST );
     
    354318
    355319        __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this);
    356         #if !defined(__CFA_NO_STATISTICS__)
    357                 if( this->print_halts ) {
    358                         __cfaabi_bits_print_safe( STDOUT_FILENO, "Processor : %d - %s (%p)\n", this->id, this->name, (void*)this);
    359                 }
    360         #endif
     320
     321        // register the processor unless it's the main thread which is handled in the boot sequence
     322        if(this != mainProcessor) {
     323                this->id = doregister((__processor_id_t*)this);
     324                #if !defined(__CFA_NO_STATISTICS__)
     325                        if( this->print_halts ) {
     326                                __cfaabi_bits_print_safe( STDOUT_FILENO, "Processor : %d - %s (%p)\n", this->id, this->name, (void*)this);
     327                        }
     328                #endif
     329
     330                // Lock the RWlock so no-one pushes/pops while we are changing the queue
     331                uint_fast32_t last_size = ready_mutate_lock();
     332
     333                        // Adjust the ready queue size
     334                        ready_queue_grow( this->cltr );
     335
     336                // Unlock the RWlock
     337                ready_mutate_unlock( last_size );
     338        }
    361339
    362340        {
     
    397375        V( this->terminated );
    398376
    399         if(this == mainProcessor) {
     377        // unregister the processor unless it's the main thread which is handled in the boot sequence
     378        if(this != mainProcessor) {
     379                // Lock the RWlock so no-one pushes/pops while we are changing the queue
     380                uint_fast32_t last_size = ready_mutate_lock();
     381
     382                        // Adjust the ready queue size
     383                        ready_queue_shrink( this->cltr );
     384
     385                        // Make sure we aren't on the idle queue
     386                        #if !defined(__CFA_NO_STATISTICS__)
     387                                bool removed =
     388                        #endif
     389                        unsafe_remove( this->cltr->idles, this );
     390
     391                        #if !defined(__CFA_NO_STATISTICS__)
     392                                if(removed) __tls_stats()->ready.sleep.exits++;
     393                        #endif
     394
     395                // Unlock the RWlock
     396                ready_mutate_unlock( last_size );
     397
     398                // Finally we don't need the read_lock any more
     399                unregister((__processor_id_t*)this);
     400        }
     401        else {
    400402                // HACK : the coroutine context switch expects this_thread to be set
    401403                // and it make sense for it to be set in all other cases except here
     
    857859
    858860        void ?{}(processor & this) with( this ) {
    859                 ( this.idle ){};
    860                 ( this.terminated ){ 0 };
    861                 ( this.runner ){};
    862                 init( this, "Main Processor", *mainCluster );
     861                name = "Main Processor";
     862                cltr = mainCluster;
     863                terminated{ 0 };
     864                do_terminate = false;
     865                preemption_alarm = 0p;
     866                pending_preemption = false;
    863867                kernel_thread = pthread_self();
     868                id = -1u;
     869
     870                #if !defined(__CFA_NO_STATISTICS__)
     871                        print_stats = false;
     872                        print_halts = false;
     873                #endif
    864874
    865875                runner{ &this };
    866876                __cfadbg_print_safe(runtime_core, "Kernel : constructed main processor context %p\n", &runner);
     877
     878                __atomic_fetch_add( &cltr->nprocessors, 1u, __ATOMIC_SEQ_CST );
    867879        }
    868880
     
    871883        mainProcessor = (processor *)&storage_mainProcessor;
    872884        (*mainProcessor){};
     885
     886        mainProcessor->id = doregister( (__processor_id_t*)mainProcessor);
    873887
    874888        //initialize the global state variables
     
    930944        kernel_stop_preemption();
    931945
     946        unregister((__processor_id_t*)mainProcessor);
     947
    932948        // Destroy the main processor and its context in reverse order of construction
    933949        // These were manually constructed so we need manually destroy them
    934950        void ^?{}(processor & this) with( this ){
    935                 deinit( this );
    936 
    937951                /* paranoid */ verify( this.do_terminate == true );
    938952                __atomic_fetch_sub( &cltr->nprocessors, 1u, __ATOMIC_SEQ_CST );
  • libcfa/src/concurrency/ready_queue.cfa

    r124c1b7 r7d94bfe3  
    186186//=======================================================================
    187187void ?{}(__ready_queue_t & this) with (this) {
    188         lanes.data  = 0p;
    189         lanes.count = 0;
     188
     189        lanes.data = alloc(4);
     190        for( i; 4 ) {
     191                (lanes.data[i]){};
     192        }
     193        lanes.count = 4;
     194        snzi{ log2( lanes.count / 8 ) };
    190195}
    191196
    192197void ^?{}(__ready_queue_t & this) with (this) {
    193         verify( 0  == lanes.count );
     198        verify( 4  == lanes.count );
    194199        verify( !query( snzi ) );
     200
     201        ^(snzi){};
     202
     203        for( i; 4 ) {
     204                ^(lanes.data[i]){};
     205        }
    195206        free(lanes.data);
    196207}
     
    484495}
    485496
    486 #warning remove when alloc is fixed
    487 forall( dtype T | sized(T) )
    488 static inline T * correct_alloc( T ptr[], size_t dim ) {
    489         if( dim == 0 ) {
    490                 free(ptr);
    491                 return 0p;
    492         }
    493         T * temp = alloc( dim );
    494         if(ptr) {
    495                 memcpy( temp, ptr, dim * sizeof(T));
    496                 free(ptr);
    497         }
    498         return temp;
    499 }
    500 
    501497// Grow the ready queue
    502498void ready_queue_grow  (struct cluster * cltr) {
     
    517513
    518514                // Allocate new array (uses realloc and memcpies the data)
    519                 lanes.data = correct_alloc(lanes.data, ncount);
     515                lanes.data = alloc(lanes.data, ncount);
    520516
    521517                // Fix the moved data
     
    562558                size_t ocount = lanes.count;
    563559                // Check that we have some space left
    564                 if(ocount < 4) abort("Program attempted to destroy more Ready Queues than were created");
     560                if(ocount < 8) abort("Program attempted to destroy more Ready Queues than were created");
    565561
    566562                // reduce the actual count so push doesn't use the old queues
     
    604600
    605601                // Allocate new array (uses realloc and memcpies the data)
    606                 lanes.data = correct_alloc(lanes.data, lanes.count);
     602                lanes.data = alloc(lanes.data, lanes.count);
    607603
    608604                // Fix the moved data
  • libcfa/src/concurrency/snzi.hfa

    r124c1b7 r7d94bfe3  
    120120//--------------------------------------------------
    121121// SNZI object
    122 void  ?{}( __snzi_t & this ) {
    123         this.mask = 0;
    124         this.root = 0;
    125         this.nodes = 0p;
    126 }
    127 
    128122void  ?{}( __snzi_t & this, unsigned depth ) with( this ) {
    129123        mask = (1 << depth) - 1;
Note: See TracChangeset for help on using the changeset viewer.