Ignore:
File:
1 edited

Legend:

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

    r5510027 r85b1deb  
    1616//C Includes
    1717#include <stddef.h>
     18#include <errno.h>
     19#include <string.h>
    1820extern "C" {
    1921#include <stdio.h>
     
    4951thread_desc * mainThread;
    5052
    51 struct { __dllist_t(cluster) list; __spinlock_t lock; } global_clusters;
     53extern "C" {
     54struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
     55}
    5256
    5357//-----------------------------------------------------------------------------
     
    143147        runner.proc = &this;
    144148
    145         sem_init(&idleLock, 0, 0);
     149        idleLock{};
    146150
    147151        start( &this );
     
    149153
    150154void ^?{}(processor & this) with( this ){
    151         if( ! do_terminate ) {
     155        if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
    152156                __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
    153                 terminate(&this);
    154                 verify(this.do_terminate);
    155                 verify( kernelTLS.this_processor != &this);
     157
     158                __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
     159                wake( &this );
     160
    156161                P( terminated );
    157162                verify( kernelTLS.this_processor != &this);
    158                 pthread_join( kernel_thread, NULL );
    159         }
    160 
    161         sem_destroy(&idleLock);
     163        }
     164
     165        pthread_join( kernel_thread, NULL );
    162166}
    163167
     
    198202
    199203                thread_desc * readyThread = NULL;
    200                 for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ )
     204                for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ )
    201205                {
    202206                        readyThread = nextThread( this->cltr );
     
    217221                        else
    218222                        {
    219                                 spin(this, &spin_count);
     223                                // spin(this, &spin_count);
     224                                halt(this);
    220225                        }
    221226                }
     
    290295}
    291296
    292 // Handles spinning logic
    293 // TODO : find some strategy to put cores to sleep after some time
    294 void spin(processor * this, unsigned int * spin_count) {
    295         // (*spin_count)++;
    296         halt(this);
    297 }
    298 
    299297// KERNEL_ONLY
    300298// Context invoker for processors
     
    403401                unlock( ready_queue_lock );
    404402
    405                 if( was_empty ) {
     403                if(was_empty) {
    406404                        lock      (proc_list_lock __cfaabi_dbg_ctx2);
    407405                        if(idles) {
    408                                 wake(idles.head);
     406                                wake_fast(idles.head);
    409407                        }
    410408                        unlock    (proc_list_lock);
    411409                }
     410                else if( struct processor * idle = idles.head ) {
     411                        wake_fast(idle);
     412                }
     413
    412414        }
    413415
     
    544546        __cfaabi_dbg_print_safe("Kernel : Starting\n");
    545547
    546         global_clusters.list{ __get };
    547         global_clusters.lock{};
     548        __cfa_dbg_global_clusters.list{ __get };
     549        __cfa_dbg_global_clusters.lock{};
    548550
    549551        // Initialize the main cluster
     
    626628        // When its coroutine terminates, it return control to the mainThread
    627629        // which is currently here
    628         mainProcessor->do_terminate = true;
     630        __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
    629631        returnToKernel();
     632        mainThread->self_cor.state = Halted;
    630633
    631634        // THE SYSTEM IS NOW COMPLETELY STOPPED
     
    643646        ^(mainThread){};
    644647
    645         ^(global_clusters.list){};
    646         ^(global_clusters.lock){};
     648        ^(__cfa_dbg_global_clusters.list){};
     649        ^(__cfa_dbg_global_clusters.lock){};
    647650
    648651        __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
     
    654657
    655658void halt(processor * this) with( *this ) {
     659        // verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
     660
    656661        with( *cltr ) {
    657662                lock      (proc_list_lock __cfaabi_dbg_ctx2);
     
    663668        __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this);
    664669
    665         verify( ({int sval = 0; sem_getvalue(&this->idleLock, &sval); sval; }) < 200);
    666         int __attribute__((unused)) ret = sem_wait(&idleLock);
    667         verify(ret > 0 || errno == EINTR);
     670        wait( idleLock );
    668671
    669672        __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this);
     
    675678                unlock    (proc_list_lock);
    676679        }
    677 }
    678 
    679 void wake(processor * this) {
    680         __cfaabi_dbg_print_safe("Kernel : Waking up processor %p\n", this);
    681         int __attribute__((unused)) ret = sem_post(&this->idleLock);
    682         verify(ret > 0 || errno == EINTR);
    683         verify( ({int sval = 0; sem_getvalue(&this->idleLock, &sval); sval; }) < 200);
    684680}
    685681
     
    797793// Global Queues
    798794void doregister( cluster     & cltr ) {
    799         lock      ( global_clusters.lock __cfaabi_dbg_ctx2);
    800         push_front( global_clusters.list, cltr );
    801         unlock    ( global_clusters.lock );
     795        lock      ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
     796        push_front( __cfa_dbg_global_clusters.list, cltr );
     797        unlock    ( __cfa_dbg_global_clusters.lock );
    802798}
    803799
    804800void unregister( cluster     & cltr ) {
    805         lock  ( global_clusters.lock __cfaabi_dbg_ctx2);
    806         remove( global_clusters.list, cltr );
    807         unlock( global_clusters.lock );
     801        lock  ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
     802        remove( __cfa_dbg_global_clusters.list, cltr );
     803        unlock( __cfa_dbg_global_clusters.lock );
    808804}
    809805
Note: See TracChangeset for help on using the changeset viewer.