Changeset c84e80a


Ignore:
Timestamp:
Jan 18, 2017, 6:12:13 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
68e6031
Parents:
7fbe450
Message:

Kernel now supports [0-9] cfa threads on a single core, using round-robin scheduling and no preemption

Location:
src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/examples/thread.c

    r7fbe450 rc84e80a  
    1717struct MyThread {
    1818        thread_h t;
    19         int value;
     19        unsigned id;
     20        unsigned count;
    2021};
    2122
    22 void ?{}( MyThread * this, int value ) {
    23         this->value = value;
     23void ?{}( MyThread * this ) {
     24        this->id = 0;
     25        this->count = 10;
     26}
     27
     28void ?{}( MyThread * this, unsigned id, unsigned count ) {
     29        this->id = id;
     30        this->count = count;
    2431}
    2532
     
    2835void main(MyThread* this) {
    2936        printf("Main called with %p\n", this);
    30         printf("Thread value is %d\n", this->value);
     37        printf("Thread %d : Suspending %d times\n", this->id, this->count);
     38
     39        for(int i = 0; i < this->count; i++) {
     40                printf("Thread %d : Suspend No. %d\n", this->id, i + 1);
     41                printf("Back to %p\n", &this->t.c);
     42                suspend();
     43        }
    3144}
    3245
     
    3952}
    4053
    41 void ?{}( MyThread * this ) {
    42         this->value = 1;
    43         printf("MyThread created\n");
    44         printf("Address %p\n", this);
    45         printf("handle %p\n", get_thread(this));
    46         printf("main %p\n", main);
    47         printf("get_t %p\n", get_thread);
    48         printf("invoke %p\n", CtxInvokeThread);
    49 }
    50 
    5154int main() {
    5255
    53         printf("Main is %p\n", this_coroutine());
     56        thread(MyThread) thread1;
     57        thread(MyThread) thread2;
    5458
    55         printf("Creating thread\n");
     59        thread2.handle.id = 1;
    5660
    57         thread(MyThread) thread1;
    58 
    59         printf("Main is %p\n", this_coroutine());
    60 
    61         printf("First thread created\n");
     61        printf("\n\nMain is %p\n", this_coroutine());
    6262
    6363        kernel_run();
    6464
     65        printf("Kernel terminated correctly\n");
     66
    6567        return 0;
    6668}
  • src/libcfa/concurrency/coroutines

    r7fbe450 rc84e80a  
    2929      coroutine * get_coroutine(T * this);
    3030};
     31
     32#define DECL_COROUTINE(X) static inline coroutine* get_coroutine(X* this) { return &this->c; } void main(X* this);
    3133
    3234//-----------------------------------------------------------------------------
  • src/libcfa/concurrency/invoke.c

    r7fbe450 rc84e80a  
    1414
    1515extern void __suspend_no_inline__F___1(void);
     16extern void __scheduler_remove__F_P9sthread_h__1(struct thread_h*);
    1617
    1718void CtxInvokeCoroutine(
     
    2021      void *this
    2122) {
    22       LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine);
     23      // LIB_DEBUG_PRINTF("Invoke Coroutine : Received %p (main %p, get_c %p)\n", this, main, get_coroutine);
    2324
    2425      struct coroutine* cor = get_coroutine( this );
     
    4243      void *this
    4344) {
    44       LIB_DEBUG_PRINTF("Invoke Thread : Received %p (main %p, get_t %p)\n", this, main, get_thread);
     45      // LIB_DEBUG_PRINTF("Invoke Thread : Received %p (main %p, get_t %p)\n", this, main, get_thread);
    4546
    4647      __suspend_no_inline__F___1();
    4748
    48       struct coroutine* cor = &get_thread( this )->c;
     49      struct thread_h* thrd = get_thread( this );
     50      struct coroutine* cor = &thrd->c;
    4951      cor->state = Active;
    5052
    51       LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this);
     53      // LIB_DEBUG_PRINTF("Invoke Thread : invoking main %p (args %p)\n", main, this);
    5254      main( this );
     55
     56      __scheduler_remove__F_P9sthread_h__1(thrd);
    5357
    5458      //Final suspend, should never return
     
    6468      void (*invoke)(void *)
    6569) {
    66       LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);
     70      // LIB_DEBUG_PRINTF("StartCoroutine : Passing in %p (main %p) to invoke (%p) from start (%p)\n", this, main, invoke, CtxStart);
    6771
    6872      struct coStack_t* stack = &get_coroutine( this )->stack;
  • src/libcfa/concurrency/kernel

    r7fbe450 rc84e80a  
    1818#define KERNEL_H
    1919
    20 extern struct thread_h * the_thread;
     20#include <stdbool.h>
    2121
     22struct processor {
     23        struct proc_coroutine * cor;
     24        unsigned int thread_index;
     25        unsigned int thread_count;
     26        struct thread_h * threads[10];
     27        bool terminated;
     28};
     29
     30void ?{}(processor * this);
     31void ^?{}(processor * this);
     32
     33void scheduler_add( struct thread_h * thrd );
     34void scheduler_remove( struct thread_h * thrd );
    2235void kernel_run( void );
    2336
  • src/libcfa/concurrency/kernel.c

    r7fbe450 rc84e80a  
    1919
    2020//C Includes
    21 #include <stdbool.h>
     21#include <stddef.h>
    2222
    2323//CFA Includes
     
    2929#include "invoke.h"
    3030
    31 thread_h * the_thread = 0;
     31processor systemProcessorStorage = {};
     32processor * systemProcessor = &systemProcessorStorage;
     33
     34void ?{}(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
     47void ^?{}(processor * this) {
     48
     49}
     50
     51//-----------------------------------------------------------------------------
     52// Processor coroutine
     53struct proc_coroutine {
     54        processor * proc;
     55        coroutine c;
     56};
     57
     58void ?{}(coroutine * this, processor * proc) {
     59        this{};
     60}
     61
     62DECL_COROUTINE(proc_coroutine)
     63
     64void ?{}(proc_coroutine * this, processor * proc) {
     65        (&this->c){proc};
     66        this->proc = proc;
     67        proc->cor = this;
     68}
     69
     70void ^?{}(proc_coroutine * this) {
     71        ^(&this->c){};
     72}
     73
     74void CtxInvokeProcessor(processor * proc) {
     75        proc_coroutine proc_cor_storage = {proc};
     76        resume( &proc_cor_storage );
     77}
     78
     79//-----------------------------------------------------------------------------
     80// Processor running routines
     81void main(proc_coroutine * cor);
     82thread_h * nextThread(processor * this);
     83void runThread(processor * this, thread_h * dst);
     84void spin(processor * this, unsigned int * spin_count);
     85
     86void 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
     106thread_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
     117void 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
     133void spin(processor * this, unsigned int * spin_count) {
     134        (*spin_count)++;
     135}
     136
     137//-----------------------------------------------------------------------------
     138// Kernel runner (Temporary)
     139
     140void 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
     151void 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}
    32167
    33168void kernel_run( void ) {
    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 );
     169        CtxInvokeProcessor(systemProcessor);
    52170}
    53171
  • src/libcfa/concurrency/threads

    r7fbe450 rc84e80a  
    3939}
    4040
     41static inline coroutine* get_coroutine(thread_h* this) {
     42        return &this->c;
     43}
     44
    4145//-----------------------------------------------------------------------------
    4246// Ctors and dtors
  • src/libcfa/concurrency/threads.c

    r7fbe450 rc84e80a  
    7878        current_coroutine = thrd_c;
    7979
    80         LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", handle, thrd_c, thrd_h);
     80        // LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", handle, thrd_c, thrd_h);
    8181
    8282        create_stack(&thrd_c->stack, thrd_c->stack.size);
     
    8484        CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
    8585
    86         the_thread = thrd_h;
     86        scheduler_add(thrd_h);
    8787}
    8888
Note: See TracChangeset for help on using the changeset viewer.