Ignore:
Timestamp:
Nov 14, 2022, 11:52:44 AM (3 years ago)
Author:
caparson <caparson@…>
Branches:
ADT, ast-experimental, master
Children:
7d9598d8
Parents:
b77f0e1 (diff), 19a8c40 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

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

    rb77f0e1 r63be3387  
    1616#define __cforall_thread__
    1717#define _GNU_SOURCE
     18
     19// #define __CFA_DEBUG_PRINT_RUNTIME_CORE__
    1820
    1921// C Includes
     
    113115KERNEL_STORAGE(thread$,              mainThread);
    114116KERNEL_STORAGE(__stack_t,            mainThreadCtx);
    115 // KERNEL_STORAGE(__scheduler_RWLock_t, __scheduler_lock);
    116 KERNEL_STORAGE(eventfd_t,            mainIdleEventFd);
    117 KERNEL_STORAGE(io_future_t,          mainIdleFuture);
    118117#if !defined(__CFA_NO_STATISTICS__)
    119118KERNEL_STORAGE(__stats_t, mainProcStats);
     
    222221                ( this.runner ){};
    223222                init( this, "Main Processor", *mainCluster, 0p );
    224                 kernel_thread = pthread_self();
     223                kernel_thread = __cfaabi_pthread_self();
    225224
    226225                runner{ &this };
     
    232231        mainProcessor = (processor *)&storage_mainProcessor;
    233232        (*mainProcessor){};
    234 
    235         mainProcessor->idle_wctx.rdbuf = &storage_mainIdleEventFd;
    236         mainProcessor->idle_wctx.ftr   = (io_future_t*)&storage_mainIdleFuture;
    237         /* paranoid */ verify( sizeof(storage_mainIdleEventFd) == sizeof(eventfd_t) );
    238233
    239234        __cfa_io_start( mainProcessor );
     
    283278}
    284279
     280extern "C"{
     281        void pthread_delete_kernel_threads_();
     282}
     283
     284
    285285static void __kernel_shutdown(void) {
    286286        if(!cfa_main_returned) return;
     287
     288        //delete kernel threads for pthread_concurrency
     289        pthread_delete_kernel_threads_();
     290
    287291        /* paranoid */ verify( __preemption_enabled() );
    288292        disable_interrupts();
     
    327331
    328332                /* paranoid */ verify( this.do_terminate == true );
    329                 __cfaabi_dbg_print_safe("Kernel : destroyed main processor context %p\n", &runner);
     333                __cfadbg_print_safe(runtime_core, "Kernel : destroyed main processor context %p\n", &runner);
    330334        }
    331335
     
    373377        register_tls( proc );
    374378
    375         // used for idle sleep when io_uring is present
    376         io_future_t future;
    377         eventfd_t idle_buf;
    378         proc->idle_wctx.ftr = &future;
    379         proc->idle_wctx.rdbuf = &idle_buf;
    380 
    381 
    382379        // SKULLDUGGERY: We want to create a context for the processor coroutine
    383380        // which is needed for the 2-step context switch. However, there is no reason
     
    388385        (proc->runner){ proc, &info };
    389386
    390         __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage);
     387        __cfadbg_print_safe(runtime_core, "Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage);
    391388
    392389        //Set global state
     
    514511        self_mon.recursion = 1;
    515512        self_mon_p = &self_mon;
    516         link.next = 0p;
    517         link.ts   = MAX;
     513        rdy_link.next = 0p;
     514        rdy_link.ts   = MAX;
    518515        preferred = ready_queue_new_preferred();
    519516        last_proc = 0p;
    520517        random_state = __global_random_mask ? __global_random_prime : __global_random_prime ^ rdtscl();
    521518        #if defined( __CFA_WITH_VERIFY__ )
     519                executing = 0p;
    522520                canary = 0x0D15EA5E0D15EA5Ep;
    523521        #endif
    524522
    525         node.next = 0p;
    526         node.prev = 0p;
    527523        doregister(curr_cluster, this);
    528524
     
    647643        #endif
    648644
    649         threads{ __get };
     645        threads{};
    650646
    651647        io.arbiter = create();
    652648        io.params = io_params;
     649
     650        managed.procs = 0p;
     651        managed.cnt = 0;
    653652
    654653        doregister(this);
     
    667666
    668667void ^?{}(cluster & this) libcfa_public {
     668        set_concurrency( this, 0 );
     669
    669670        destroy(this.io.arbiter);
    670671
     
    722723        lock      (cltr->thread_list_lock __cfaabi_dbg_ctx2);
    723724        cltr->nthreads += 1;
    724         push_front(cltr->threads, thrd);
     725        insert_first(cltr->threads, thrd);
    725726        unlock    (cltr->thread_list_lock);
    726727}
     
    728729void unregister( cluster * cltr, thread$ & thrd ) {
    729730        lock  (cltr->thread_list_lock __cfaabi_dbg_ctx2);
    730         remove(cltr->threads, thrd );
    731         cltr->nthreads -= 1;
     731        {
     732                tytagref( dlink(thread$), dlink(thread$) ) ?`inner( thread$ & this ) = void;
     733                with( DLINK_VIA( thread$, struct __thread_user_link ) )
     734                        remove( thrd );
     735                cltr->nthreads -= 1;
     736        }
    732737        unlock(cltr->thread_list_lock);
    733738}
     
    777782        pthread_attr_t attr;
    778783
    779         check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
     784        check( __cfaabi_pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
    780785
    781786        size_t stacksize = max( PTHREAD_STACK_MIN, DEFAULT_STACK_SIZE );
     
    804809        #endif
    805810
    806         check( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
    807         check( pthread_create( pthread, &attr, start, arg ), "pthread_create" );
     811        check( __cfaabi_pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" );
     812        check( __cfaabi_pthread_create( pthread, &attr, start, arg ), "pthread_create" );
    808813        return stack;
    809814}
    810815
    811816void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ) {
    812         int err = pthread_join( pthread, retval );
     817        int err = __cfaabi_pthread_join( pthread, retval );
    813818        if( err != 0 ) abort("KERNEL ERROR: joining pthread %p caused error %s\n", (void*)pthread, strerror(err));
    814819
     
    816821                pthread_attr_t attr;
    817822
    818                 check( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
     823                check( __cfaabi_pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute
    819824
    820825                size_t stacksize;
    821826                // default stack size, normally defined by shell limit
    822                 check( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
     827                check( __cfaabi_pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" );
    823828                assert( stacksize >= PTHREAD_STACK_MIN );
    824829                stacksize += __page_size;
     
    838843}
    839844
     845unsigned set_concurrency( cluster & this, unsigned new ) libcfa_public {
     846        unsigned old = this.managed.cnt;
     847
     848        __cfadbg_print_safe(runtime_core, "Kernel : resizing cluster from %u to %u\n", old, (unsigned)new);
     849
     850        // Delete all the old unneeded procs
     851        if(old > new) for(i; (unsigned)new ~ old) {
     852                __cfadbg_print_safe(runtime_core, "Kernel : destroying %u\n", i);
     853                delete( this.managed.procs[i] );
     854        }
     855
     856        // Allocate new array (uses realloc and memcpies the data)
     857        this.managed.procs = alloc( new, this.managed.procs`realloc );
     858        this.managed.cnt = new;
     859
     860        // Create the desired new procs
     861        if(old < new) for(i; old ~ new) {
     862                __cfadbg_print_safe(runtime_core, "Kernel : constructing %u\n", i);
     863                (*(this.managed.procs[i] = alloc())){ this };
     864        }
     865
     866        // return the old count
     867        return old;
     868}
     869
    840870#if defined(__CFA_WITH_VERIFY__)
    841871static bool verify_fwd_bck_rng(void) {
Note: See TracChangeset for help on using the changeset viewer.