Ignore:
File:
1 edited

Legend:

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

    rc81ebf9 r0c78741  
    3636//CFA Includes
    3737#include "libhdr.h"
    38 #include "preemption.h"
    3938
    4039//Private includes
     
    4847KERNEL_STORAGE(processorCtx_t, systemProcessorCtx);
    4948KERNEL_STORAGE(cluster, systemCluster);
    50 KERNEL_STORAGE(system_proc_t, systemProcessor);
     49KERNEL_STORAGE(processor, systemProcessor);
    5150KERNEL_STORAGE(thread_desc, mainThread);
    5251KERNEL_STORAGE(machine_context_t, mainThread_context);
    5352
    5453cluster * systemCluster;
    55 system_proc_t * systemProcessor;
     54processor * systemProcessor;
    5655thread_desc * mainThread;
    5756
     
    119118// Processor coroutine
    120119void ?{}(processorCtx_t * this, processor * proc) {
    121         (&this->__cor){ "Processor" };
     120        (&this->__cor){};
    122121        this->proc = proc;
    123122        proc->runner = this;
     
    140139        (&this->terminated){};
    141140        this->is_terminated = false;
    142         this->preemption_alarm = NULL;
    143         this->preemption = default_preemption();
    144         this->disable_preempt_count = 1;                //Start with interrupts disabled
    145         this->pending_preemption = false;
    146141
    147142        start( this );
     
    154149        (&this->terminated){};
    155150        this->is_terminated = false;
    156         this->disable_preempt_count = 0;
    157         this->pending_preemption = false;
    158151
    159152        this->runner = runner;
    160153        LIB_DEBUG_PRINT_SAFE("Kernel : constructing processor context %p\n", runner);
    161154        runner{ this };
    162 }
    163 
    164 void ?{}(system_proc_t * this, cluster * cltr, processorCtx_t * runner) {
    165         (&this->alarms){};
    166         (&this->alarm_lock){};
    167         this->pending_alarm = false;
    168 
    169         (&this->proc){ cltr, runner };
    170155}
    171156
     
    193178void main(processorCtx_t * runner) {
    194179        processor * this = runner->proc;
    195 
    196180        LIB_DEBUG_PRINT_SAFE("Kernel : core %p starting\n", this);
    197181
     182        thread_desc * readyThread = NULL;
     183        for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ )
    198184        {
    199                 // Setup preemption data
    200                 preemption_scope scope = { this };
    201 
    202                 LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);
    203 
    204                 thread_desc * readyThread = NULL;
    205                 for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ )
     185                readyThread = nextThread( this->cltr );
     186
     187                if(readyThread)
    206188                {
    207                         readyThread = nextThread( this->cltr );
    208 
    209                         if(readyThread)
    210                         {
    211                                 runThread(this, readyThread);
    212 
    213                                 //Some actions need to be taken from the kernel
    214                                 finishRunning(this);
    215 
    216                                 spin_count = 0;
    217                         }
    218                         else
    219                         {
    220                                 spin(this, &spin_count);
    221                         }
    222                 }
    223 
    224                 LIB_DEBUG_PRINT_SAFE("Kernel : core %p stopping\n", this);
    225         }
    226 
     189                        runThread(this, readyThread);
     190
     191                        //Some actions need to be taken from the kernel
     192                        finishRunning(this);
     193
     194                        spin_count = 0;
     195                }
     196                else
     197                {
     198                        spin(this, &spin_count);
     199                }               
     200        }
     201
     202        LIB_DEBUG_PRINT_SAFE("Kernel : core %p unlocking thread\n", this);
    227203        signal( &this->terminated );
    228204        LIB_DEBUG_PRINT_SAFE("Kernel : core %p terminated\n", this);
     
    323299        LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this);
    324300       
     301        // pthread_attr_t attributes;
     302        // pthread_attr_init( &attributes );
     303
    325304        pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
     305
     306        // pthread_attr_destroy( &attributes );
    326307
    327308        LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);       
     
    335316        assertf( thrd->next == NULL, "Expected null got %p", thrd->next );
    336317       
    337         lock( &systemProcessor->proc.cltr->lock );
    338         append( &systemProcessor->proc.cltr->ready_queue, thrd );
    339         unlock( &systemProcessor->proc.cltr->lock );
     318        lock( &systemProcessor->cltr->lock );
     319        append( &systemProcessor->cltr->ready_queue, thrd );
     320        unlock( &systemProcessor->cltr->lock );
    340321}
    341322
     
    386367}
    387368
    388 //=============================================================================================
    389 // Kernel Setup logic
    390 //=============================================================================================
    391369//-----------------------------------------------------------------------------
    392370// Kernel boot procedures
     
    401379        mainThread{ &info };
    402380
    403         LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n");
    404 
    405         // Enable preemption
    406         kernel_start_preemption();
    407 
    408381        // Initialize the system cluster
    409382        systemCluster = (cluster *)&systemCluster_storage;
    410383        systemCluster{};
    411384
    412         LIB_DEBUG_PRINT_SAFE("Kernel : System cluster ready\n");
    413 
    414385        // Initialize the system processor and the system processor ctx
    415386        // (the coroutine that contains the processing control flow)
    416         systemProcessor = (system_proc_t *)&systemProcessor_storage;
     387        systemProcessor = (processor *)&systemProcessor_storage;
    417388        systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtx_storage };
    418389
    419390        // Add the main thread to the ready queue
    420         // once resume is called on systemProcessor->runner the mainThread needs to be scheduled like any normal thread
     391        // once resume is called on systemProcessor->ctx the mainThread needs to be scheduled like any normal thread
    421392        ScheduleThread(mainThread);
    422393
    423394        //initialize the global state variables
    424         this_processor = &systemProcessor->proc;
     395        this_processor = systemProcessor;
    425396        this_processor->current_thread = mainThread;
    426397        this_processor->current_coroutine = &mainThread->cor;
     
    429400        // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
    430401        // mainThread is on the ready queue when this call is made.
    431         resume( systemProcessor->proc.runner );
     402        resume(systemProcessor->runner);
    432403
    433404
     
    443414        // When its coroutine terminates, it return control to the mainThread
    444415        // which is currently here
    445         systemProcessor->proc.is_terminated = true;
     416        systemProcessor->is_terminated = true;
    446417        suspend();
    447418
     
    450421        // Destroy the system processor and its context in reverse order of construction
    451422        // These were manually constructed so we need manually destroy them
    452         ^(systemProcessor->proc.runner){};
     423        ^(systemProcessor->runner){};
    453424        ^(systemProcessor){};
    454425
     
    513484}
    514485
    515 //=============================================================================================
    516 // Kernel Utilities
    517 //=============================================================================================
    518486//-----------------------------------------------------------------------------
    519487// Locks
     
    523491void ^?{}( spinlock * this ) {
    524492
    525 }
    526 
    527 bool try_lock( spinlock * this ) {
    528         return this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0;
    529493}
    530494
Note: See TracChangeset for help on using the changeset viewer.