Changeset 8f49a54


Ignore:
Timestamp:
Jan 19, 2017, 3:42:29 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:
dcb42b8
Parents:
4a3386b4
Message:

Clean-up thread, kernel and examples

Location:
src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/examples/thread.c

    r4a3386b4 r8f49a54  
    1 // #include <kernel>
     1#include <fstream>
    22#include <stdlib>
    33#include <threads>
    44
    5 // // Start coroutine routines
    6 // extern "C" {
    7 //       forall(dtype T | is_coroutine(T))
    8 //       void CtxInvokeCoroutine(T * this);
     5// Start coroutine routines
     6struct MyThread {
     7        thread_h t;
     8        unsigned id;
     9        unsigned count;
     10};
    911
    10 //       forall(dtype T | is_coroutine(T))
    11 //       void CtxStart(T * this, void ( *invoke)(T *));
     12DECL_THREAD(MyThread)
    1213
    13 //      forall(dtype T | is_coroutine(T))
    14 //       void CtxInvokeThread(T * this);
    15 // }
     14void ?{}( MyThread * this, unsigned id, unsigned count ) {
     15        this->id = id;
     16        this->count = count;
     17}
    1618
    17 // struct MyThread {
    18 //      thread_h t;
    19 //      unsigned id;
    20 //      unsigned count;
    21 // };
     19void main(MyThread* this) {
     20        sout | "Thread" | this->id | " : Suspending" | this->count | "times" | endl;
     21        suspend();
    2222
    23 // void ?{}( MyThread * this ) {
    24 //      this->id = 0;
    25 //      this->count = 10;
    26 // }
     23        for(int i = 0; i < this->count; i++) {
     24                sout | "Thread" | this->id | " : Suspend No." | i + 1 | endl;
     25                suspend();
     26        }
     27}
    2728
    28 // void ?{}( MyThread * this, unsigned id, unsigned count ) {
    29 //      this->id = id;
    30 //      this->count = count;
    31 // }
     29int main(int argc, char* argv[]) {
    3230
    33 // void ^?{}( MyThread * this ) {}
     31        unsigned itterations = 10;
     32        if(argc == 2) {
     33                int val = ato(argv[1]);
     34                assert(val >= 0);
     35                itterations = val;
     36        }
    3437
    35 // void main(MyThread* this) {
    36 //      printf("Main called with %p\n", this);
    37 //      printf("Thread %d : Suspending %d times\n", this->id, this->count);
     38        sout | "User main begin" | endl;
    3839
    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 //      }
    44 // }
     40        {
     41                thread(MyThread) thread1 = { (unsigned)1, itterations };
     42                thread(MyThread) thread2 = { (unsigned)2, itterations };
     43        }
    4544
    46 // thread_h* get_thread(MyThread* this) {
    47 //      return &this->t;
    48 // }
    49 
    50 // coroutine* get_coroutine(MyThread* this) {
    51 //      return &this->t.c;
    52 // }
    53 
    54 int main() {
    55         printf("Main is %p\n", this_coroutine());
    56 
    57         // thread(MyThread) thread1;
    58         // thread(MyThread) thread2;
    59 
    60         // thread2.handle.id = 1;
    61 
    62 
    63         // // kernel_run();
    64 
    65         // printf("Kernel terminated correctly\n");
     45        sout | "User main end" | endl;
    6646
    6747        return 0;
  • src/libcfa/concurrency/coroutines.c

    r4a3386b4 r8f49a54  
    100100// is not inline (We can't inline Cforall in C)
    101101void suspend_no_inline(void) {
    102         LIB_DEBUG_PRINTF("Suspending back : to %p from %p\n", this_coroutine(), this_coroutine() ? this_coroutine()->last : (void*)-1);
    103 
    104102        suspend();
    105103}
  • src/libcfa/concurrency/invoke.c

    r4a3386b4 r8f49a54  
    3333      main( this );
    3434
     35      cor->state = Halt;
     36      cor->notHalted = false;
     37
    3538      //Final suspend, should never return
    3639      __suspend_no_inline__F___1();
     
    5457      main( this );
    5558
     59      cor->state = Halt;
     60      cor->notHalted = false;
    5661      __scheduler_remove__F_P9sthread_h__1(thrd);
    5762
  • src/libcfa/concurrency/kernel.c

    r4a3386b4 r8f49a54  
    119119        // context switch to specified coroutine
    120120        // Which is now the current_coroutine
    121         LIB_DEBUG_PRINTF("Kernel : switching to ctx %p (from %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine);
     121        // LIB_DEBUG_PRINTF("Kernel : switching to ctx %p (from %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine);
    122122        current_coroutine = thrd_ctx;
    123123        CtxSwitch( proc_ctx->stack.context, thrd_ctx->stack.context );
    124124        current_coroutine = proc_ctx;
    125         LIB_DEBUG_PRINTF("Kernel : returned from ctx %p (to %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine);
     125        // LIB_DEBUG_PRINTF("Kernel : returned from ctx %p (to %p, current %p)\n", thrd_ctx, proc_ctx, current_coroutine);
    126126
    127127        // when CtxSwitch returns we are back in the processor coroutine
     
    136136
    137137void scheduler_add( thread_h * thrd ) {
    138         LIB_DEBUG_PRINTF("Kernel : scheduling %p on core %p (%d spots)\n", thrd, systemProcessor, systemProcessor->thread_count);
    139138        for(int i = 0; i < systemProcessor->thread_count; i++) {
    140139                if(systemProcessor->threads[i] == NULL) {
     
    143142                }
    144143        }
    145         assert(false);
     144        assertf(false, "Scheduler full");
    146145}
    147146
    148147void scheduler_remove( thread_h * thrd ) {
    149         LIB_DEBUG_PRINTF("Kernel : unscheduling %p from core %p\n", thrd, systemProcessor);
    150148        for(int i = 0; i < systemProcessor->thread_count; i++) {
    151149                if(systemProcessor->threads[i] == thrd) {
    152150                        systemProcessor->threads[i] = NULL;
    153                         break;
    154                 }
    155         }
    156         for(int i = 0; i < systemProcessor->thread_count; i++) {
    157                 if(systemProcessor->threads[i] != NULL) {
    158151                        return;
    159152                }
    160153        }
    161         LIB_DEBUG_PRINTF("Kernel : terminating core %p\n", systemProcessor);   
    162         systemProcessor->terminated = true;
     154        assertf(false, "Trying to unschedule unkown thread");
    163155}
    164156
     
    229221
    230222        mainThread_info_t ctx;
    231         LIB_DEBUG_PRINTF("Kernel :    base : %p\n", ctx.base );
    232         LIB_DEBUG_PRINTF("Kernel :     top : %p\n", ctx.top );
    233         LIB_DEBUG_PRINTF("Kernel :   limit : %p\n", ctx.limit );
    234         LIB_DEBUG_PRINTF("Kernel :    size : %x\n", ctx.size );
    235         LIB_DEBUG_PRINTF("Kernel : storage : %p\n", ctx.storage );
    236         LIB_DEBUG_PRINTF("Kernel : context : %p\n", ctx.context );
     223        // LIB_DEBUG_PRINTF("Kernel :    base : %p\n", ctx.base );
     224        // LIB_DEBUG_PRINTF("Kernel :     top : %p\n", ctx.top );
     225        // LIB_DEBUG_PRINTF("Kernel :   limit : %p\n", ctx.limit );
     226        // LIB_DEBUG_PRINTF("Kernel :    size : %x\n", ctx.size );
     227        // LIB_DEBUG_PRINTF("Kernel : storage : %p\n", ctx.storage );
     228        // LIB_DEBUG_PRINTF("Kernel : context : %p\n", ctx.context );
    237229
    238230        // Start by initializing the main thread
     
    265257        scheduler_remove(mainThread);
    266258
    267         LIB_DEBUG_PRINTF("Suspending main\n");
     259        LIB_DEBUG_PRINTF("Kernel : Terminating system processor\n");           
     260        systemProcessor->terminated = true;
     261
    268262        suspend();
    269263
    270         LIB_DEBUG_PRINTF("Kernel : Control return to initial process thread\n");
     264        LIB_DEBUG_PRINTF("Kernel : Control returned to initial process thread\n");
    271265
    272266        ^(systemProcessor->ctx){};
  • src/libcfa/concurrency/threads

    r4a3386b4 r8f49a54  
    3333        void ^?{}(T*);*/
    3434};
     35
     36#define DECL_THREAD(X) static inline thread_h* get_thread(X* this) { return &this->t; } void main(X* this);
    3537
    3638forall(otype T | is_thread(T) )
  • src/libcfa/concurrency/threads.c

    r4a3386b4 r8f49a54  
    4646forall(otype T | is_thread(T) )
    4747void ?{}( thread(T)* this ) {
    48         printf("thread() ctor\n");
    4948        (&this->handle){};
    5049        start(this);
     
    8988forall(otype T | is_thread(T) )
    9089void stop( thread(T)* this ) {
    91 
     90        T* handle  = &this->handle;
     91        thread_h*  thrd_h = get_thread   (handle);
     92        while( thrd_h->c.notHalted ) {
     93                suspend();
     94        }
    9295}
    9396
Note: See TracChangeset for help on using the changeset viewer.