Ignore:
File:
1 edited

Legend:

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

    r85b1deb r5510027  
    1616//C Includes
    1717#include <stddef.h>
    18 #include <errno.h>
    19 #include <string.h>
    2018extern "C" {
    2119#include <stdio.h>
     
    5149thread_desc * mainThread;
    5250
    53 extern "C" {
    54 struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
    55 }
     51struct { __dllist_t(cluster) list; __spinlock_t lock; } global_clusters;
    5652
    5753//-----------------------------------------------------------------------------
     
    147143        runner.proc = &this;
    148144
    149         idleLock{};
     145        sem_init(&idleLock, 0, 0);
    150146
    151147        start( &this );
     
    153149
    154150void ^?{}(processor & this) with( this ){
    155         if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
     151        if( ! do_terminate ) {
    156152                __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
    157 
    158                 __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
    159                 wake( &this );
    160 
     153                terminate(&this);
     154                verify(this.do_terminate);
     155                verify( kernelTLS.this_processor != &this);
    161156                P( terminated );
    162157                verify( kernelTLS.this_processor != &this);
    163         }
    164 
    165         pthread_join( kernel_thread, NULL );
     158                pthread_join( kernel_thread, NULL );
     159        }
     160
     161        sem_destroy(&idleLock);
    166162}
    167163
     
    202198
    203199                thread_desc * readyThread = NULL;
    204                 for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ )
     200                for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ )
    205201                {
    206202                        readyThread = nextThread( this->cltr );
     
    221217                        else
    222218                        {
    223                                 // spin(this, &spin_count);
    224                                 halt(this);
     219                                spin(this, &spin_count);
    225220                        }
    226221                }
     
    295290}
    296291
     292// Handles spinning logic
     293// TODO : find some strategy to put cores to sleep after some time
     294void spin(processor * this, unsigned int * spin_count) {
     295        // (*spin_count)++;
     296        halt(this);
     297}
     298
    297299// KERNEL_ONLY
    298300// Context invoker for processors
     
    401403                unlock( ready_queue_lock );
    402404
    403                 if(was_empty) {
     405                if( was_empty ) {
    404406                        lock      (proc_list_lock __cfaabi_dbg_ctx2);
    405407                        if(idles) {
    406                                 wake_fast(idles.head);
     408                                wake(idles.head);
    407409                        }
    408410                        unlock    (proc_list_lock);
    409411                }
    410                 else if( struct processor * idle = idles.head ) {
    411                         wake_fast(idle);
    412                 }
    413 
    414412        }
    415413
     
    546544        __cfaabi_dbg_print_safe("Kernel : Starting\n");
    547545
    548         __cfa_dbg_global_clusters.list{ __get };
    549         __cfa_dbg_global_clusters.lock{};
     546        global_clusters.list{ __get };
     547        global_clusters.lock{};
    550548
    551549        // Initialize the main cluster
     
    628626        // When its coroutine terminates, it return control to the mainThread
    629627        // which is currently here
    630         __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
     628        mainProcessor->do_terminate = true;
    631629        returnToKernel();
    632         mainThread->self_cor.state = Halted;
    633630
    634631        // THE SYSTEM IS NOW COMPLETELY STOPPED
     
    646643        ^(mainThread){};
    647644
    648         ^(__cfa_dbg_global_clusters.list){};
    649         ^(__cfa_dbg_global_clusters.lock){};
     645        ^(global_clusters.list){};
     646        ^(global_clusters.lock){};
    650647
    651648        __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
     
    657654
    658655void halt(processor * this) with( *this ) {
    659         // verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
    660 
    661656        with( *cltr ) {
    662657                lock      (proc_list_lock __cfaabi_dbg_ctx2);
     
    668663        __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this);
    669664
    670         wait( idleLock );
     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);
    671668
    672669        __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this);
     
    678675                unlock    (proc_list_lock);
    679676        }
     677}
     678
     679void 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);
    680684}
    681685
     
    793797// Global Queues
    794798void doregister( cluster     & cltr ) {
    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 );
     799        lock      ( global_clusters.lock __cfaabi_dbg_ctx2);
     800        push_front( global_clusters.list, cltr );
     801        unlock    ( global_clusters.lock );
    798802}
    799803
    800804void unregister( cluster     & cltr ) {
    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 );
     805        lock  ( global_clusters.lock __cfaabi_dbg_ctx2);
     806        remove( global_clusters.list, cltr );
     807        unlock( global_clusters.lock );
    804808}
    805809
Note: See TracChangeset for help on using the changeset viewer.