Ignore:
File:
1 edited

Legend:

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

    rdddb3dd0 rda3963a  
    2222extern "C" {
    2323      #include <limits.h>       // PTHREAD_STACK_MIN
    24         #include <sys/eventfd.h>  // eventfd
    2524      #include <sys/mman.h>     // mprotect
    2625      #include <sys/resource.h> // getrlimit
     
    8180static void ?{}(processorCtx_t & this) {}
    8281static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info);
     82static void ?{}(__bin_sem_t & this);
     83static void ^?{}(__bin_sem_t & this);
    8384
    8485#if defined(__CFA_WITH_VERIFY__)
     
    9091extern void __kernel_alarm_startup(void);
    9192extern void __kernel_alarm_shutdown(void);
     93extern void __kernel_io_startup (void);
     94extern void __kernel_io_shutdown(void);
    9295
    9396//-----------------------------------------------------------------------------
     
    101104KERNEL_STORAGE($thread,              mainThread);
    102105KERNEL_STORAGE(__stack_t,            mainThreadCtx);
     106KERNEL_STORAGE(io_context,           mainPollerThread);
    103107KERNEL_STORAGE(__scheduler_RWLock_t, __scheduler_lock);
    104108#if !defined(__CFA_NO_STATISTICS__)
     
    196200
    197201        void ?{}(processor & this) with( this ) {
     202                ( this.idle ){};
    198203                ( this.terminated ){};
    199204                ( this.runner ){};
     
    223228        __kernel_alarm_startup();
    224229
     230        // Start IO
     231        __kernel_io_startup();
     232
    225233        // Add the main thread to the ready queue
    226234        // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
     
    235243        // THE SYSTEM IS NOW COMPLETELY RUNNING
    236244
     245
     246        // SKULLDUGGERY: The constructor for the mainCluster will call alloc with a dimension of 0
     247        // malloc *can* return a non-null value, we should free it if that is the case
     248        free( mainCluster->io.ctxs );
     249
     250        // Now that the system is up, finish creating systems that need threading
     251        mainCluster->io.ctxs = (io_context *)&storage_mainPollerThread;
     252        mainCluster->io.cnt  = 1;
     253        (*mainCluster->io.ctxs){ *mainCluster };
     254
    237255        __cfadbg_print_safe(runtime_core, "Kernel : Started\n--------------------------------------------------\n\n");
    238256
     
    244262
    245263static void __kernel_shutdown(void) {
     264        //Before we start shutting things down, wait for systems that need threading to shutdown
     265        ^(*mainCluster->io.ctxs){};
     266        mainCluster->io.cnt  = 0;
     267        mainCluster->io.ctxs = 0p;
     268
    246269        /* paranoid */ verify( __preemption_enabled() );
    247270        disable_interrupts();
     
    261284        // Disable preemption
    262285        __kernel_alarm_shutdown();
     286
     287        // Stop IO
     288        __kernel_io_shutdown();
    263289
    264290        // Destroy the main processor and its context in reverse order of construction
     
    460486        pending_preemption = false;
    461487
    462         this.io.ctx = 0p;
    463         this.io.pending = false;
    464         this.io.dirty   = false;
    465 
    466         this.idle = eventfd(0, 0);
    467         if (idle < 0) {
    468                 abort("KERNEL ERROR: PROCESSOR EVENTFD - %s\n", strerror(errno));
    469         }
    470 
    471488        #if !defined(__CFA_NO_STATISTICS__)
    472489                print_stats = 0;
     
    509526        // Finally we don't need the read_lock any more
    510527        unregister((__processor_id_t*)&this);
    511 
    512         close(this.idle);
    513528}
    514529
    515530void ?{}(processor & this, const char name[], cluster & _cltr) {
     531        ( this.idle ){};
    516532        ( this.terminated ){};
    517533        ( this.runner ){};
     
    568584        threads{ __get };
    569585
    570         io.arbiter = create();
    571         io.params = io_params;
    572 
    573586        doregister(this);
    574587
     
    583596        ready_mutate_unlock( last_size );
    584597        enable_interrupts_noPoll(); // Don't poll, could be in main cluster
     598
     599
     600        this.io.cnt  = num_io;
     601        this.io.ctxs = aalloc(num_io);
     602        for(i; this.io.cnt) {
     603                (this.io.ctxs[i]){ this, io_params };
     604        }
    585605}
    586606
    587607void ^?{}(cluster & this) {
    588         destroy(this.io.arbiter);
     608        for(i; this.io.cnt) {
     609                ^(this.io.ctxs[i]){ true };
     610        }
     611        free(this.io.ctxs);
    589612
    590613        // Lock the RWlock so no-one pushes/pops while we are changing the queue
     
    715738}
    716739
     740extern "C" {
     741        char * strerror(int);
     742}
     743#define CHECKED(x) { int err = x; if( err != 0 ) abort("KERNEL ERROR: Operation \"" #x "\" return error %d - %s\n", err, strerror(err)); }
     744
     745static void ?{}(__bin_sem_t & this) with( this ) {
     746        // Create the mutex with error checking
     747        pthread_mutexattr_t mattr;
     748        pthread_mutexattr_init( &mattr );
     749        pthread_mutexattr_settype( &mattr, PTHREAD_MUTEX_ERRORCHECK_NP);
     750        pthread_mutex_init(&lock, &mattr);
     751
     752        pthread_cond_init (&cond, (const pthread_condattr_t *)0p);  // workaround trac#208: cast should not be required
     753        val = 0;
     754}
     755
     756static void ^?{}(__bin_sem_t & this) with( this ) {
     757        CHECKED( pthread_mutex_destroy(&lock) );
     758        CHECKED( pthread_cond_destroy (&cond) );
     759}
     760
     761#undef CHECKED
     762
    717763#if defined(__CFA_WITH_VERIFY__)
    718764static bool verify_fwd_bck_rng(void) {
Note: See TracChangeset for help on using the changeset viewer.