Ignore:
File:
1 edited

Legend:

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

    rc84e80a r8118303  
    1919
    2020//C Includes
    21 #include <stddef.h>
     21#include <stdbool.h>
    2222
    2323//CFA Includes
     
    2929#include "invoke.h"
    3030
    31 processor systemProcessorStorage = {};
    32 processor * systemProcessor = &systemProcessorStorage;
    33 
    34 void ?{}(processor * this) {
    35         this->cor = NULL;
    36         this->thread_index = 0;
    37         this->thread_count = 10;
    38         this->terminated = false;
    39 
    40         for(int i = 0; i < 10; i++) {
    41                 this->threads[i] = NULL;
    42         }
    43 
    44         LIB_DEBUG_PRINTF("Processor : ctor for core %p (core spots %d)\n", this, this->thread_count);
    45 }
    46 
    47 void ^?{}(processor * this) {
    48 
    49 }
    50 
    51 //-----------------------------------------------------------------------------
    52 // Processor coroutine
    53 struct proc_coroutine {
    54         processor * proc;
    55         coroutine c;
    56 };
    57 
    58 void ?{}(coroutine * this, processor * proc) {
    59         this{};
    60 }
    61 
    62 DECL_COROUTINE(proc_coroutine)
    63 
    64 void ?{}(proc_coroutine * this, processor * proc) {
    65         (&this->c){proc};
    66         this->proc = proc;
    67         proc->cor = this;
    68 }
    69 
    70 void ^?{}(proc_coroutine * this) {
    71         ^(&this->c){};
    72 }
    73 
    74 void CtxInvokeProcessor(processor * proc) {
    75         proc_coroutine proc_cor_storage = {proc};
    76         resume( &proc_cor_storage );
    77 }
    78 
    79 //-----------------------------------------------------------------------------
    80 // Processor running routines
    81 void main(proc_coroutine * cor);
    82 thread_h * nextThread(processor * this);
    83 void runThread(processor * this, thread_h * dst);
    84 void spin(processor * this, unsigned int * spin_count);
    85 
    86 void main(proc_coroutine * cor) {
    87         processor * this;
    88         this = cor->proc;
    89 
    90         thread_h * readyThread = NULL;
    91         for( unsigned int spin_count = 0; ! this->terminated; spin_count++ ) {
    92                
    93                 readyThread = nextThread(this);
    94 
    95                 if(readyThread) {
    96                         runThread(this, readyThread);
    97                         spin_count = 0;
    98                 } else {
    99                         spin(this, &spin_count);
    100                 }               
    101         }
    102 
    103         LIB_DEBUG_PRINTF("Kernel : core %p terminated\n", this);
    104 }
    105 
    106 thread_h * nextThread(processor * this) {
    107         for(int i = 0; i < this->thread_count; i++) {
    108                 this->thread_index = (this->thread_index + 1) % this->thread_count;     
    109                
    110                 thread_h * thrd = this->threads[this->thread_index];
    111                 if(thrd) return thrd;
    112         }
    113 
    114         return NULL;
    115 }
    116 
    117 void runThread(processor * this, thread_h * dst) {
    118         coroutine * proc_ctx = get_coroutine(this->cor);
    119         coroutine * thrd_ctx = get_coroutine(dst);
    120         thrd_ctx->last = proc_ctx;
    121 
    122         // context switch to specified coroutine
    123         // Which is now the current_coroutine
    124         LIB_DEBUG_PRINTF("Kernel : switching to ctx %p (from %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine);
    125         current_coroutine = thrd_ctx;
    126         CtxSwitch( proc_ctx->stack.context, thrd_ctx->stack.context );
    127         current_coroutine = proc_ctx;
    128         LIB_DEBUG_PRINTF("Kernel : returned from ctx %p (to %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine);
    129 
    130         // when CtxSwitch returns we are back in the processor coroutine
    131 }
    132 
    133 void spin(processor * this, unsigned int * spin_count) {
    134         (*spin_count)++;
    135 }
    136 
    137 //-----------------------------------------------------------------------------
    138 // Kernel runner (Temporary)
    139 
    140 void scheduler_add( struct thread_h * thrd ) {
    141         LIB_DEBUG_PRINTF("Kernel : scheduling %p on core %p (%d spots)\n", thrd, systemProcessor, systemProcessor->thread_count);
    142         for(int i = 0; i < systemProcessor->thread_count; i++) {
    143                 if(systemProcessor->threads[i] == NULL) {
    144                         systemProcessor->threads[i] = thrd;
    145                         return;
    146                 }
    147         }
    148         assert(false);
    149 }
    150 
    151 void scheduler_remove( struct thread_h * thrd ) {
    152         LIB_DEBUG_PRINTF("Kernel : unscheduling %p from core %p\n", thrd, systemProcessor);
    153         for(int i = 0; i < systemProcessor->thread_count; i++) {
    154                 if(systemProcessor->threads[i] == thrd) {
    155                         systemProcessor->threads[i] = NULL;
    156                         break;
    157                 }
    158         }
    159         for(int i = 0; i < systemProcessor->thread_count; i++) {
    160                 if(systemProcessor->threads[i] != NULL) {
    161                         return;
    162                 }
    163         }
    164         LIB_DEBUG_PRINTF("Kernel : terminating core %p\n\n\n", systemProcessor);       
    165         systemProcessor->terminated = true;
    166 }
     31thread_h * the_thread = 0;
    16732
    16833void kernel_run( void ) {
    169         CtxInvokeProcessor(systemProcessor);
     34       
     35        bool done = true;
     36        coroutine* processor_cor = this_coroutine();
     37        LIB_DEBUG_PRINTF("Kernel : processor cor is %p\n", processor_cor);
     38
     39        do {
     40                thread_h * dst = the_thread;
     41
     42                LIB_DEBUG_PRINTF("Kernel : picked thread %p\n", dst);
     43
     44                // set new coroutine that task is executing
     45                current_coroutine = &dst->c;
     46
     47                // context switch to specified coroutine
     48                LIB_DEBUG_PRINTF("Kernel : switching to ctx %p (from %p)\n", current_coroutine, processor_cor);
     49                CtxSwitch( processor_cor->stack.context, current_coroutine->stack.context );
     50                // when CtxSwitch returns we are back in the processor coroutine
     51        } while( ! done );
    17052}
    17153
Note: See TracChangeset for help on using the changeset viewer.