Ignore:
File:
1 edited

Legend:

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

    r2d8f7b0 r4069faad  
    1515
    1616#define __cforall_thread__
     17// #define __CFA_DEBUG_PRINT_RUNTIME_CORE__
    1718
    1819//C Includes
     
    4041#include "invoke.h"
    4142
     43
    4244//-----------------------------------------------------------------------------
    4345// Some assembly required
     
    230232        idle{};
    231233
    232         __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", &this);
     234        __cfadbg_print_safe(runtime_core, "Kernel : Starting core %p\n", &this);
    233235
    234236        this.stack = __create_pthread( &this.kernel_thread, __invoke_processor, (void *)&this );
    235237
    236         __cfaabi_dbg_print_safe("Kernel : core %p started\n", &this);
     238        __cfadbg_print_safe(runtime_core, "Kernel : core %p created\n", &this);
    237239}
    238240
    239241void ^?{}(processor & this) with( this ){
    240242        if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
    241                 __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
     243                __cfadbg_print_safe(runtime_core, "Kernel : core %p signaling termination\n", &this);
    242244
    243245                __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
     
    258260        ready_queue_lock{};
    259261
     262        #if !defined(__CFA_NO_STATISTICS__)
     263                print_stats = false;
     264        #endif
     265
    260266        procs{ __get };
    261267        idles{ __get };
    262268        threads{ __get };
    263269
    264         __kernel_io_startup( this );
     270        __kernel_io_startup( this, &this == mainCluster );
    265271
    266272        doregister(this);
     
    268274
    269275void ^?{}(cluster & this) {
    270         __kernel_io_shutdown( this );
     276        __kernel_io_shutdown( this, &this == mainCluster );
    271277
    272278        unregister(this);
     
    285291        verify(this);
    286292
    287         __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
     293        __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this);
    288294
    289295        doregister(this->cltr, this);
     
    293299                preemption_scope scope = { this };
    294300
    295                 __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
     301                __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this);
    296302
    297303                $thread * readyThread = 0p;
     
    319325                }
    320326
    321                 __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this);
     327                __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this);
    322328        }
    323329
     
    326332        V( this->terminated );
    327333
    328         __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
     334        __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this);
    329335
    330336        // HACK : the coroutine context switch expects this_thread to be set
     
    471477
    472478        //We now have a proper context from which to schedule threads
    473         __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
     479        __cfadbg_print_safe(runtime_core, "Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
    474480
    475481        // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
     
    482488
    483489        // Main routine of the core returned, the core is now fully terminated
    484         __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner);
     490        __cfadbg_print_safe(runtime_core, "Kernel : core %p main ended (%p)\n", proc, &proc->runner);
    485491
    486492        return 0p;
     
    713719static void __kernel_startup(void) {
    714720        verify( ! kernelTLS.preemption_state.enabled );
    715         __cfaabi_dbg_print_safe("Kernel : Starting\n");
     721        __cfadbg_print_safe(runtime_core, "Kernel : Starting\n");
    716722
    717723        __page_size = sysconf( _SC_PAGESIZE );
     
    724730        (*mainCluster){"Main Cluster"};
    725731
    726         __cfaabi_dbg_print_safe("Kernel : Main cluster ready\n");
     732        __cfadbg_print_safe(runtime_core, "Kernel : Main cluster ready\n");
    727733
    728734        // Start by initializing the main thread
     
    734740        (*mainThread){ &info };
    735741
    736         __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
     742        __cfadbg_print_safe(runtime_core, "Kernel : Main thread ready\n");
    737743
    738744
     
    755761
    756762                runner{ &this };
    757                 __cfaabi_dbg_print_safe("Kernel : constructed main processor context %p\n", &runner);
     763                __cfadbg_print_safe(runtime_core, "Kernel : constructed main processor context %p\n", &runner);
    758764        }
    759765
     
    780786
    781787
    782 
    783788        // THE SYSTEM IS NOW COMPLETELY RUNNING
    784         __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
     789
     790
     791        // Now that the system is up, finish creating systems that need threading
     792        __kernel_io_finish_start( *mainCluster );
     793
     794
     795        __cfadbg_print_safe(runtime_core, "Kernel : Started\n--------------------------------------------------\n\n");
    785796
    786797        verify( ! kernelTLS.preemption_state.enabled );
     
    790801
    791802static void __kernel_shutdown(void) {
    792         __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
     803        //Before we start shutting things down, wait for systems that need threading to shutdown
     804        __kernel_io_prepare_stop( *mainCluster );
    793805
    794806        /* paranoid */ verify( TL_GET( preemption_state.enabled ) );
    795807        disable_interrupts();
    796808        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     809
     810        __cfadbg_print_safe(runtime_core, "\n--------------------------------------------------\nKernel : Shutting down\n");
    797811
    798812        // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
     
    822836        ^(__cfa_dbg_global_clusters.lock){};
    823837
    824         __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
     838        __cfadbg_print_safe(runtime_core, "Kernel : Shutdown complete\n");
    825839}
    826840
     
    847861
    848862        // We are ready to sleep
    849         __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this);
     863        __cfadbg_print_safe(runtime_core, "Kernel : Processor %p ready to sleep\n", this);
    850864        wait( idle );
    851865
    852866        // We have woken up
    853         __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this);
     867        __cfadbg_print_safe(runtime_core, "Kernel : Processor %p woke up and ready to run\n", this);
    854868
    855869        // Get ourself off the idle list
     
    867881static bool __wake_one(cluster * this, __attribute__((unused)) bool force) {
    868882        // if we don't want to force check if we know it's false
    869         if( !this->idles.head && !force ) return false;
     883        // if( !this->idles.head && !force ) return false;
    870884
    871885        // First, lock the cluster idle
     
    880894
    881895        // Wake them up
     896        __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this->idles.head);
    882897        post( this->idles.head->idle );
    883898
     
    889904// Unconditionnaly wake a thread
    890905static bool __wake_proc(processor * this) {
     906        __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this);
    891907        return post( this->idle );
    892908}
     
    10001016        // make new owner
    10011017        unpark( thrd __cfaabi_dbg_ctx2 );
     1018
     1019        return thrd != 0p;
     1020}
     1021
     1022bool V(semaphore & this, unsigned diff) with( this ) {
     1023        $thread * thrd = 0p;
     1024        lock( lock __cfaabi_dbg_ctx2 );
     1025        int release = max(-count, (int)diff);
     1026        count += diff;
     1027        for(release) {
     1028                unpark( pop_head( waiting ) __cfaabi_dbg_ctx2 );
     1029        }
     1030
     1031        unlock( lock );
    10021032
    10031033        return thrd != 0p;
Note: See TracChangeset for help on using the changeset viewer.